【发布时间】:2021-07-05 16:43:38
【问题描述】:
我正在尝试将最新出现的值组合存储在表中。 因此,正在向患者和传感器发送消息:
{ patient: "A", sensor: "X1", measurementTime: 1625501773},
{ patient: "A", sensor: "Y1", measurementTime: 1625501773},
{ patient: "B", sensor: "X2", measurementTime: 1625501772}
我想更新每个患者的列表,每个传感器的最新测量时间。有人知道如何使用 Kafka Streams API 做到这一点吗?
我在想类似以下的事情:
KeyValueBytesStoreSupplier PatientSensorSupplier = Stores.inMemoryKeyValueStore("latest-patient-sensor-occurrence");
KStream<String, SensorData> messagesStream = builder.stream(
Topic.Message.getKafkaName(),
Consumed.with(Serdes.String(), new NoSqlSerde())
.withTimestampExtractor((k, partitionTime) -> {
Long measurementTime = ((SensorDataPojo) k.value()).getMeasurementTime();
if (measurementTime != null) {
return measurementTime;
}
return partitionTime;
})
);
KStream<String, String> patientsWithSensors = messagesStream.map(
(key, sensorDataPojo) ->
new KeyValue<>(
noSqlSensorDataPojo.getPatientId(),
noSqlSensorDataPojo.getSensorId()
)
);
KTable<String, SensorListSerde> patientsWithSensorsTable = patientsWithSensors.toTable(
Named.as("patients-sensors-occurrences-table"),
Materialized.<String, String>as(storeSupplier)
.withKeySerde(Serdes.String())
.withValueSerde(sensorListSerde)
);
但是使用它我无法弄清楚如何在表格中正确存储每个患者每个传感器的最新记录的时间戳。我假设必须有更好的方法来解决这个问题。
【问题讨论】:
标签: apache-kafka apache-kafka-streams