首页 > 解决方案 > 从命令提示符启动 Kafka 消费者时,我收到 Class Not Found Exception。我该如何解决这个问题?

问题描述

我正在使用 Kafka-streams 执行字数统计问题。Kafka 使用的版本是 2.3.1,创建了 word-count-input 和 word-count-output 两个主题。相同的代码是

public static void main( String[] args )
{
    Properties properties = new Properties();
    properties.put(StreamsConfig.APPLICATION_ID_CONFIG, "word-count");
    properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
    properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    properties.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
    properties.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

    StreamsBuilder builder = new StreamsBuilder();

    KStream<String, String> textLines = builder.stream("word-count-input");

    KTable<String, Long> wordCounts = textLines.mapValues(value -> value.toLowerCase())
                                               .flatMapValues(value -> Arrays.asList(value.split(" ")))
                                               .selectKey((key,value) -> value)
                                               .groupByKey()
                                               .count();

    // Writing back to the kafka topic
    wordCounts.toStream().to("word-count-output", Produced.with(Serdes.String(), Serdes.Long()));

    KafkaStreams streams = new KafkaStreams(builder.build(), properties);
    streams.start();


    //printing the topology
    System.out.println(streams.toString());

    // shutdown hook to correctly close the stream application
    Runtime.getRuntime().addShutdownHook(new Thread(streams::close));

}   

在使用命令提示符启动主题字数输出的消费者时,它会抛出 Class Not Found Exception 以设置 key.deserializer 和 value.deserializer 的属性。

您可以在此处查看命令提示符。

标签: apache-kafkakafka-consumer-api

解决方案


问题在于 Kafka 版本的不同。系统上安装的版本是2.3.1,maven里安装的是2.1.1。希望它可以帮助某人。


推荐阅读