I have a KStream<String, X>
which I essentially want to convert to a KTable<String, Y>
The only way I could find to achieve this using the DSL is with a map, group then reduce.
val stream: KStream<String, X> = ...
val table: KTable<String, Y> = stream
.mapValues({ value -> toYOrNull(value)})
.groupByKey(Grouped.with(Serdes.String(), ySerde))
.reduce(
{old: Y?, updated: Y? -> updated},
Materialized.`as`<String, Y, KeyValueStore<Bytes, ByteArray>>("y-store")
.withKeySerde(Serdes.String()
.withValueSerde(ySerde)
)
I would expect this to handle the case when the value of updated
in the reduce
is null
however when I inspect the store using the TopologyTestDriver
it still seems to have the old version. What am I doing wrong?
This is my test:
@Test
fun shouldDeleteFromTableWhenNull() {
val store = testDriver.getKeyValueStore<String, Y?>("y-store")
store.put("key", Y())
inputTopic.pipeInput("key", anXThatMapsToANullY)
assertThat(store.get("key")).isNull() // Fails as the old entry is still there
}