[英]Kafka streams KTable creation from a given topic
I'm doing a project and I'm stuck on the KTable. 我正在做一个项目,并且卡在KTable上。
I want to take records from a topic and put them in a KTable(store), so that I have 1 record for 1 key. 我想从一个主题中获取记录,并将其放入KTable(存储)中,这样我就有1条键的1条记录。
static KafkaStreams streams;
final Serde<Long> longSerde = Serdes.Long();
final Serde<byte[]> byteSerde = Serdes.ByteArray();
static String topicName;
static String storeName;
final StreamsBuilder builder = new StreamsBuilder();
KStream<Long, byte[]> streamed = builder.stream(topicName, Consumed.with(longSerde, byteSerde));
KTable<Long, byte[]> records = streamed.groupByKey().reduce(
new Reducer<Long>() {
@Override
public Long apply(Long aggValue, Long newValue) {
return newValue;
}
},
storeName);
This is the closest I got to the answer I think. 这是我认为最接近的答案。
Your approach is correct, but you need to use the correct serdes. 您的方法是正确的,但是您需要使用正确的Serdes。
In .reduce() function, value type should be byte[]
. 在.reduce()函数中,值类型应为
byte[]
。
KStream<Long, byte[]> streamed = builder.stream(topicName, Consumed.with(longSerde, byteSerde));
KTable<Long, byte[]> records = streamed.groupByKey().reduce(
new Reducer<byte[]>() {
@Override
public byte[] apply(byte[] aggValue, byte[] newValue) {
return newValue;
}
},
Materialized.as(storename).with(longSerde,byteSerde));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.