Java 在kstream中实现Monthwise窗口存储
我需要为kstream实施每月一次的windows。我尝试实现相同的功能,因为我无法创建不同大小的窗口存储,因为每个月的天数不同。如果有人有想法实施每月窗口,请帮助我这一点 这是我已经实现的一段代码Java 在kstream中实现Monthwise窗口存储,java,apache-kafka,apache-kafka-streams,Java,Apache Kafka,Apache Kafka Streams,我需要为kstream实施每月一次的windows。我尝试实现相同的功能,因为我无法创建不同大小的窗口存储,因为每个月的天数不同。如果有人有想法实施每月窗口,请帮助我这一点 这是我已经实现的一段代码 KTable<Windowed<String>, Customer> tableStream = input.groupByKey().windowedBy(TimeWindows.of(Duration.ofDays(calendar.getActualMaximum(
KTable<Windowed<String>, Customer> tableStream = input.groupByKey().windowedBy(TimeWindows.of(Duration.ofDays(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)))).aggregate(() -> new Customer()
.withMsisdn(null)
.withCustName(null)
.withRechargeAmount(0)
.withCreatedTime(null),
//Aggregator
(k, v, aggV) -> new Customer()
.withMsisdn(v.getMsisdn())
.withCustName(v.getCustName())
.withRechargeAmount(aggV.getRechargeAmount() + v.getRechargeAmount())
.withCreatedTime(v.getCreatedTime()),
//Serializer
Materialized.<String, Customer, WindowStore<Bytes, byte[]>>as("cust-store2").withValueSerde(AppSerdes.Customer()));
tableStream.toStream().foreach(
(wKey, value) -> log.info("MONTH-WISE "+
"Store ID: " + wKey.key() + " Window ID: " + wKey.window().hashCode() +
" Window start: " + Instant.ofEpochMilli(wKey.window().start()).atOffset(ZoneOffset.UTC) +
" Window end: " + Instant.ofEpochMilli(wKey.window().end()).atOffset(ZoneOffset.UTC) +
" Count: " + value
)
);
KTable tableStream=input.groupByKey().windowedBy(TimeWindows.of(Duration.of Days(calendar.getactualmax(calendar.DAY/u/u MONTH)))).aggregate(()->new Customer()
.withMsisdn(空)
.withCustName(空)
.金额(0)
.withCreatedTime(空),
//聚合器
(k,v,aggV)->新客户()
.withMsisdn(v.getMsisdn())
.withCustName(v.getCustName())
.withRechargeAmount(累计getRechargeAmount()+累计getRechargeAmount())
.withCreatedTime(v.getCreatedTime()),
//序列化程序
使用valueserde(AppSerdes.Customer())实现.as(“cust-store2”);
tableStream.toStream().foreach(
(wKey,value)->log.info(“按月”+
“存储ID:+wKey.key()+”窗口ID:+wKey.Window().hashCode()+
“窗口开始:”+Instant.ofEpochMilli(wKey.Window().start()).atOffset(ZoneOffset.UTC)+
“窗口结束:”+Instant.ofEpochMilli(wKey.Window().end()).atOffset(ZoneOffset.UTC)+
“计数:”+值
)
);
谢谢不幸的是,卡夫卡流中没有现成的基于日历的窗口。有一个服务器请求此功能。 Kafka Streams中没有基于日历的窗口的根本原因是窗口长度如何在窗口存储区的键中编码
但是,我可以想象,您可以在一个自定义的
KStream#transform()
中使用连接的键值存储实现每月一次的窗口。非常感谢您的回复,will tryGlad将提供帮助!如果你认为这有用的话,你能接受我的回答吗?如果别人发了一个更好的帖子,你可以随时改变主意。