Java 卡夫卡流式传输消息中的总字数

Java 卡夫卡流式传输消息中的总字数,java,apache-kafka,apache-kafka-streams,Java,Apache Kafka,Apache Kafka Streams,我有一个关于使用卡夫卡流计算消息中的单词的问题。基本上,我想计算单词的总数,而不是计算单词的每个实例。 因此,与其 all 1 streams 1 lead 1 to 1 kafka 1 我需要 totalWordCount 5 或者类似的东西 我尝试了多种方法来解决这部分代码: KTable<String, Long> wordCounts = textLines .flatMapValues(value -> Arrays.asL

我有一个关于使用卡夫卡流计算消息中的单词的问题。基本上,我想计算单词的总数,而不是计算单词的每个实例。 因此,与其

all     1
streams 1
lead    1
to      1
kafka   1
我需要

totalWordCount   5
或者类似的东西

我尝试了多种方法来解决这部分代码:

KTable<String, Long> wordCounts = textLines
    .flatMapValues(value -> Arrays.asList(value.toLowerCase().split("\\W+")))
    .groupBy((key, value) -> value)
    .count();
KTable wordCounts=文本行
.flatMapValues(值->数组.asList(值.toLowerCase().split(\\W+)))
.groupBy((键,值)->value)
.count();
例如添加
。选择key((key,value)->“totalWordCount”)
尝试将每个键(all,streams等)更改为totalWordCount,认为它会自动递增 我还尝试使用编辑我的代码,以尝试实现总字数


我没有成功,在做了更多的工作之后,现在我认为我的做法是错误的。似乎我需要做的是有3个主题(我只与2个主题合作)和2个制作人,其中最后一个制作人以某种方式从第一个制作人那里获取数据(显示每个实例的字数),并基本上将数字相加以输出总字数,但我不完全确定如何处理。非常感谢您的帮助/指导。谢谢。

你把
selectKey()放在哪里了?这个想法基本上是正确的,但是请注意,
groupBy()
也设置了键

KTable wordCounts=文本行
.flatMapValues(值->数组.asList(值.toLowerCase().split(\\W+)))
.groupBy((键,值)->“totalWordCount”)
.count();
或者(使用
groupByKey()
在聚合之前不更改键)

KTable wordCounts=文本行
.选择键((键,值)->“totalWordCount”)
.flatMapValues(值->数组.asList(值.toLowerCase().split(\\W+)))
.groupByKey()
.count();

您把
selectKey()放在哪里了?这个想法基本上是正确的,但是请注意,
groupBy()
也设置了键

KTable wordCounts=文本行
.flatMapValues(值->数组.asList(值.toLowerCase().split(\\W+)))
.groupBy((键,值)->“totalWordCount”)
.count();
或者(使用
groupByKey()
在聚合之前不更改键)

KTable wordCounts=文本行
.选择键((键,值)->“totalWordCount”)
.flatMapValues(值->数组.asList(值.toLowerCase().split(\\W+)))
.groupByKey()
.count();
@配置
@使能卡夫卡团队
公共类FirstStreamApp{
@豆子
公共KStream流程(StreamsBuilder builder){
KStream inputStream=builder.stream(“streamIn”,consumered.with(Serdes.String(),Serdes.String());
KStream upperCaseStream=inputStream.mapValues(value->value.toUpperCase());
upperCaseStream.to(“outTopic”,producted.with(Serdes.String(),Serdes.String());
KTable wordCounts=upperCaseStream.flatMapValues(v->array.asList(v.split(“”))。选择key((k,v)->v)。groupByKey()。
计数(具体化为(“计数存储”);
toStream().to(“wordCountTopic”,产生于.with(Serdes.String(),Serdes.Long());
返回上游;
}
}
@配置
@使能卡夫卡团队
公共类FirstStreamApp{
@豆子
公共KStream流程(StreamsBuilder builder){
KStream inputStream=builder.stream(“streamIn”,consumered.with(Serdes.String(),Serdes.String());
KStream upperCaseStream=inputStream.mapValues(value->value.toUpperCase());
upperCaseStream.to(“outTopic”,producted.with(Serdes.String(),Serdes.String());
KTable wordCounts=upperCaseStream.flatMapValues(v->array.asList(v.split(“”))。选择key((k,v)->v)。groupByKey()。
计数(具体化为(“计数存储”);
toStream().to(“wordCountTopic”,产生于.with(Serdes.String(),Serdes.Long());
返回上游;
}
}

aha!我把
selectKey()
放在
flatMapValues()之后。
谢谢!实际的解决方法是将
groupBy()
更改为
groupByKey()
。将
selectKey()
放在
flapMapValue()
之后同样有效--
flatMapValues()
不会修改该键,因此在此之前或之后更改它是相同的。啊哈!我把
selectKey()
放在
flatMapValues()之后。
谢谢!实际的解决方法是将
groupBy()
更改为
groupByKey()
。将
selectKey()
放在
flapMapValue()
之后同样有效--
flatMapValues()
不会修改该键,因此在此之前或之后更改它是相同的。
@Configuration
@EnableKafkaStreams
public class FirstStreamApp {

@Bean
public KStream<String,String> process(StreamsBuilder builder){
    KStream<String,String> inputStream = builder.stream("streamIn", Consumed.with(Serdes.String(),Serdes.String()));
    KStream<String,String> upperCaseStream = inputStream.mapValues(value->value.toUpperCase());
   upperCaseStream.to("outTopic", Produced.with(Serdes.String(),Serdes.String()));

    KTable<String, Long> wordCounts = upperCaseStream.flatMapValues(v-> Arrays.asList(v.split(" "))).selectKey((k, v) -> v).groupByKey().
           count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as("counts-store"));
    wordCounts.toStream().to("wordCountTopic", Produced.with(Serdes.String(),Serdes.Long()));

    return upperCaseStream;
}

}