我在用Kafka流计算字数。Kafka使用的版本是2.3.1,创建了两个主题:字数输入和字数输出。相同的代码是
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的属性。
您可以在这里查看命令提示符。
1条答案
按热度按时间idfiyjo81#
问题在于Kafka版本的不同。系统上安装的版本是2.3.1,maven中安装的版本是2.1.1。希望它能帮助别人。