Java Kafka Streams-为每个记录的对象列表提取时间戳
我想要实现的是根据消息中存在的时间戳获取记录中存在的每条消息的计数。每个记录由Java Kafka Streams-为每个记录的对象列表提取时间戳,java,apache-kafka,apache-kafka-streams,Java,Apache Kafka,Apache Kafka Streams,我想要实现的是根据消息中存在的时间戳获取记录中存在的每条消息的计数。每个记录由列表对象组成。我想提取每个度量的时间戳,并根据度量名称聚合度量 度量标准 public class Metric { String metric; Long timestamp; Double value; } 自定义时间戳提取器 我已经实现了这个时间戳提取器,它将记录转换为列表对象。它当前获取第一个时间戳,该时间戳对该ArrayList进行窗口处理 public class EventTi
列表
对象组成。我想提取每个度量的时间戳,并根据度量名称聚合度量
度量标准
public class Metric {
String metric;
Long timestamp;
Double value;
}
自定义时间戳提取器
我已经实现了这个时间戳提取器,它将记录转换为列表对象。它当前获取第一个时间戳,该时间戳对该ArrayList进行窗口处理
public class EventTimestampExtractor implements TimestampExtractor {
public long extract(ConsumerRecord<Object, Object> record, long previousTimeStamp) {
try {
// Have a ListSerde in place to deserialize the record to a List<Metric> object.
final List<Metric> value = (List<Metric>) record.value();
final Metric metric = value.get(0); // Returning the first timestamp from the metric list.
return metric.getTimestamp();
}
catch (Exception e) {
// If there is an exception, return back the event time.
return record.timestamp();
}
}
}
我的时间窗口是10秒,我想做一个测试,获取度量的计数。我的当前结果看起来像-
Window{startMs=0, endMs=10} and Value metric: metric1.count value: 3 aggregator: count interval: "10s"}
预期结果-
Window{startMs=0, endMs=10} and Value metric: metric1.count value: 2 aggregator: count interval: "10s"}
Window{startMs=10, endMs=20} and Value metric: metric1.count value: 1 aggregator: count interval: "10s"}
很抱歉问了这么长的问题,但是有没有办法从包含消息集合的单个记录中提取多个时间戳
Kafka Streams版本-2.4.1时间戳提取器对您的用例没有帮助,因为它只能给您一个时间戳。使用
flatMap()
所有输出记录继承输入记录的时间戳
如果需要动态修改时间戳,则需要使用transform()
实现“平面映射”。对于每个输入记录,您可以多次调用context.forward()
来执行实际的平面映射(您可以在末尾返回null;
以不发出任何其他记录)。在每个forward()
调用中,您可以通过将新的时间戳设置为.all()。withTimestamp(…)
:
公钥值转换(K键,V值){
对于(…){
context.forward(newKey、newValue、To.all(),带时间戳(newTimestamp);
}
返回null;
}
你太棒了:)
Window{startMs=0, endMs=10} and Value metric: metric1.count value: 3 aggregator: count interval: "10s"}
Window{startMs=0, endMs=10} and Value metric: metric1.count value: 2 aggregator: count interval: "10s"}
Window{startMs=10, endMs=20} and Value metric: metric1.count value: 1 aggregator: count interval: "10s"}