Java 如何计算卡夫卡主题中每个ID的频率
我有一个卡夫卡主题Transfer_History,在这个主题中我上传了一个文件。现在我想计算每个派对ID的出现次数。然后,在我想应用转换之后:如果计数小于20,则将其放入新主题搅动,如果大于20,则将其放入主题忠诚 #我正在使用JAVA 公共类第一过滤器{Java 如何计算卡夫卡主题中每个ID的频率,java,apache-kafka,apache-kafka-streams,Java,Apache Kafka,Apache Kafka Streams,我有一个卡夫卡主题Transfer_History,在这个主题中我上传了一个文件。现在我想计算每个派对ID的出现次数。然后,在我想应用转换之后:如果计数小于20,则将其放入新主题搅动,如果大于20,则将其放入主题忠诚 #我正在使用JAVA 公共类第一过滤器{ public static void main(String[] args) { final StreamsBuilder builder = new StreamsBuilder(); /*input messages
public static void main(String[] args) {
final StreamsBuilder builder = new StreamsBuilder();
/*input messages example
{"155555","11.11.2016 11:12}
{"155555","11.11.2016 13:12}
{"155556","11.11.2016 13:12}
result to be achived:
{"155555","2"}
{"155556","1"}
*/
builder.stream("test_topic_3")
// .map()
.groupByKey()
// .windowedBy(Window) // This may or may not be required
.count()
.toStream()
.map(
(key,count) -> new KeyValue<>(key.toString(),count)
)
.to("test_output_filtered_3");//this topic is empty after the run
publicstaticvoidmain(字符串[]args){
最终StreamsBuilder生成器=新StreamsBuilder();
/*输入消息示例
{"155555","11.11.2016 11:12}
{"155555","11.11.2016 13:12}
{"155556","11.11.2016 13:12}
要取得的成果:
{"155555","2"}
{"155556","1"}
*/
builder.stream(“测试主题3”)
//.map()
.groupByKey()
//.windowedBy(Window)//这可能是必需的,也可能不是必需的
.count()
.toStream()
.地图(
(key,count)->新的KeyValue(key.toString(),count)
)
.to(“test_output_filtered_3”);//此主题在运行后为空
我对卡夫卡不太了解,请帮我一下
这可以通过Kafka Streams轻松实现。首先,请确保您拥有KStream&KTable的背景。您需要遵循以下步骤
builder.<Keytype, ValueType>stream(YourInputTopic))
.map()
.groupByKey()
.windowedBy(TimeWindows.of(Duration.ofSeconds(10))) // This may or may not be required
in your case
.count()
.toStream()
.map((Windowed<String> key, Long count) -> new KeyValue<>(key.key(),count.toString()))
.filter((k,v)-> Long.parseLong(v) > 20) // This is the filter
.to(TopicName));
builder.stream(YourInputTopic))
.map()
.groupByKey()
.windowedBy(TimeWindows.of(Duration.of seconds(10))//这可能是必需的,也可能不是必需的
就你而言
.count()
.toStream()
.map((带窗口的键,长计数)->新的键值(key.key(),count.toString())
.filter((k,v)->Long.parseLong(v)>20)//这是过滤器
。致(主题名称));
注意:这只是一个伪代码,它会给你一个如何实现这一壮举的想法谢谢你的帮助……这次你像天使一样来到我身边……我更新了我的问题……plz@sammy help me out我真的建议你学习Kstreams和Ktable。快捷方式不会长期帮助你。这里inputOpic=transfer\u History and.到(搅动)