消息通过Kafka Streams API时的类强制转换异常

时间:2017-09-26 15:40:18

标签: apache-kafka apache-kafka-streams

当消息通过Kafka Streams API时,我正在获得类强制转换异常

例外是:

  

java.lank / ClassCastException:[B无法强制转换为com.fasterxml.jackson.databind.JsonNode

我的流代码是:

public static void main(String[] args) {
    Properties config = new Properties();
    config.put(StreamsConfig.APPLICATION_ID_CONFIG, "BranchingTopics-API");
    config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG,
            "samsmembershipkafka.dev.cloud.wal-mart.com:9092");
    config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
    final Serde<String> stringSerde = Serdes.String();
    final Serializer<JsonNode> jsonSerializer = new JsonSerializer();
    final Deserializer<JsonNode> jsonDeserializer = new JsonDeserializer();
    final Serde<JsonNode> jsonSerde = Serdes.serdeFrom(jsonSerializer,
            jsonDeserializer);

    /*
     * config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, jsonSerde);
     * config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG,
     * jsonSerde);
     */

    // Building Stream
    KStreamBuilder builder = new KStreamBuilder();

    KStream<String, JsonNode> textlines = builder.stream("MainTopic");

    Predicate<String, JsonNode> isAddComment = (key, value) -> value
            .get("header").toString().contains("/addComment");
    Predicate<String, JsonNode> is1M1C = (key, value) -> value
            .get("header").toString().contains("/1Member1Account");
    Predicate<String, JsonNode> isLostOrStolen = (key, value) -> (value
            .get("header").toString()
            .contains("/changeCardStatus?action=STOLEN") || value
            .get("header").toString()
            .contains("/changeCardStatus?action=LOST"));

    KStream<String, JsonNode>[] topicTypes = textlines.branch(isAddComment,
            is1M1C, isLostOrStolen);

    topicTypes[0].to(stringSerde, jsonSerde, "CommentsTopic");
    topicTypes[1].to(stringSerde, jsonSerde, "OneMemberOneAccountTopic");
    topicTypes[2].to(stringSerde, jsonSerde, "LostOrStolenTopic");

    KafkaStreams streams = new KafkaStreams(builder, config);

    streams.start();
}

1 个答案:

答案 0 :(得分:0)

似乎你在某个地方使用了错误的Serde。从您的代码片段中,我假设您在阅读主题时需要指定JSON-Serde:

builder.stream(stringSerde, jsonSerde, "MainTopic");

我猜你也需要为其他操作指定正确的Serde s。仔细查看堆栈跟踪以找出哪个运算符抛出异常