我们有一个kTable,其中我们使用以下代码映射值并实现到KeyValueStore:
@Bean
public KTable<String, CancelEvent> kTable(StreamsBuilder kStreamBuilder,
ValueMapper<CancelEvent, CancelEvent> mapper,
SpecificAvroSerde<CancelEvent> serde) {
return kStreamBuilder
.table(properties.getProperty("topics.cancel"), Consumed.with(Serdes.String(), serde))
.mapValues(mapper, Materialized.as("cancel-event-store"));
}
以及映射器代码:
@Override
public CancelEvent apply(CancelEvent value) {
if (value.getData().getCancelled()) {
return null;
}
return value;
}
我们使用Avro进行序列化和反序列化。当我们尝试使用键查询state store
时,它总是返回null
。
用于查询状态存储的代码:
ReadOnlyKeyValueStore<String, CancelEvent> store = streamsFactory.getKafkaStreams()
.store("cancel-event-store", QueryableStoreTypes.keyValueStore());
CancelEvent event = store.get(queryEvent.getMeta().getId().toString());
event
始终为null
。在调试时,我遍历了keyValuestore
,并意识到密钥上有某些内容(魔术字节可能是!)。请参考下图
所以,问题是如何用键查询?
更新1
private Map<String, Object> kStreamsConfigsProperties() {
final Map<String, Object> config = new HashMap<>();
config.put(APPLICATION_ID_CONFIG, "app.name" + new Date().getTime());
config.put(BOOTSTRAP_SERVERS_CONFIG, properties.getProperty("spring.kafka.bootstrap-servers"));
config.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, properties.getProperty("spring.kafka.schema-registry"));
config.put(DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
config.put(DEFAULT_VALUE_SERDE_CLASS_CONFIG, SpecificAvroSerde.class.getName());
config.put(DEFAULT_TIMESTAMP_EXTRACTOR_CLASS_CONFIG, WallclockTimestampExtractor.class.getName());
return config;
}