use of org.apache.kafka.streams.state.KeyValueStore in project kafka by apache.
the class KTableMapValuesTest method shouldNotEnableSendingOldValuesOnParentIfMapValuesMaterialized.
@Test
public void shouldNotEnableSendingOldValuesOnParentIfMapValuesMaterialized() {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
final KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, consumed);
final KTableImpl<String, String, Integer> table2 = (KTableImpl<String, String, Integer>) table1.mapValues(s -> Integer.valueOf(s), Materialized.<String, Integer, KeyValueStore<Bytes, byte[]>>as("bob").withValueSerde(Serdes.Integer()));
table2.enableSendingOldValues(true);
assertThat(table1.sendingOldValueEnabled(), is(false));
assertThat(table2.sendingOldValueEnabled(), is(true));
testSendingOldValues(builder, topic1, table2);
}
use of org.apache.kafka.streams.state.KeyValueStore in project kafka by apache.
the class KTableAggregateTest method testAggRepartition.
@Test
public void testAggRepartition() {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
final KTable<String, String> table1 = builder.table(topic1, consumed);
final KTable<String, String> table2 = table1.groupBy((key, value) -> {
switch(key) {
case "null":
return KeyValue.pair(null, value);
case "NULL":
return null;
default:
return KeyValue.pair(value, value);
}
}, stringSerialized).aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, MockAggregator.TOSTRING_REMOVER, Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as("topic1-Canonized").withValueSerde(stringSerde));
table2.toStream().process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), CONFIG, Instant.ofEpochMilli(0L))) {
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(topic1, new StringSerializer(), new StringSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
inputTopic.pipeInput("A", "1", 10L);
inputTopic.pipeInput("A", (String) null, 15L);
inputTopic.pipeInput("A", "1", 12L);
inputTopic.pipeInput("B", "2", 20L);
inputTopic.pipeInput("null", "3", 25L);
inputTopic.pipeInput("B", "4", 23L);
inputTopic.pipeInput("NULL", "5", 24L);
inputTopic.pipeInput("B", "7", 22L);
assertEquals(asList(new KeyValueTimestamp<>("1", "0+1", 10), new KeyValueTimestamp<>("1", "0+1-1", 15), new KeyValueTimestamp<>("1", "0+1-1+1", 15), new KeyValueTimestamp<>("2", "0+2", 20), new KeyValueTimestamp<>("2", "0+2-2", 23), new KeyValueTimestamp<>("4", "0+4", 23), new KeyValueTimestamp<>("4", "0+4-4", 23), new KeyValueTimestamp<>("7", "0+7", 22)), supplier.theCapturedProcessor().processed());
}
}
use of org.apache.kafka.streams.state.KeyValueStore in project kafka by apache.
the class KTableImplTest method shouldCreateSourceAndSinkNodesForRepartitioningTopic.
@Test
public void shouldCreateSourceAndSinkNodesForRepartitioningTopic() throws Exception {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
final String storeName1 = "storeName1";
final KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, consumed, Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as(storeName1).withKeySerde(Serdes.String()).withValueSerde(Serdes.String()));
table1.groupBy(MockMapper.noOpKeyValueMapper()).aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, MockAggregator.TOSTRING_REMOVER, Materialized.as("mock-result1"));
table1.groupBy(MockMapper.noOpKeyValueMapper()).reduce(MockReducer.STRING_ADDER, MockReducer.STRING_REMOVER, Materialized.as("mock-result2"));
final Topology topology = builder.build();
try (final TopologyTestDriverWrapper driver = new TopologyTestDriverWrapper(topology, props)) {
assertEquals(3, driver.getAllStateStores().size());
assertTopologyContainsProcessor(topology, "KSTREAM-SINK-0000000003");
assertTopologyContainsProcessor(topology, "KSTREAM-SOURCE-0000000004");
assertTopologyContainsProcessor(topology, "KSTREAM-SINK-0000000007");
assertTopologyContainsProcessor(topology, "KSTREAM-SOURCE-0000000008");
final Field valSerializerField = ((SinkNode) driver.getProcessor("KSTREAM-SINK-0000000003")).getClass().getDeclaredField("valSerializer");
final Field valDeserializerField = ((SourceNode) driver.getProcessor("KSTREAM-SOURCE-0000000004")).getClass().getDeclaredField("valDeserializer");
valSerializerField.setAccessible(true);
valDeserializerField.setAccessible(true);
assertNotNull(((ChangedSerializer) valSerializerField.get(driver.getProcessor("KSTREAM-SINK-0000000003"))).inner());
assertNotNull(((ChangedDeserializer) valDeserializerField.get(driver.getProcessor("KSTREAM-SOURCE-0000000004"))).inner());
assertNotNull(((ChangedSerializer) valSerializerField.get(driver.getProcessor("KSTREAM-SINK-0000000007"))).inner());
assertNotNull(((ChangedDeserializer) valDeserializerField.get(driver.getProcessor("KSTREAM-SOURCE-0000000008"))).inner());
}
}
use of org.apache.kafka.streams.state.KeyValueStore in project kafka by apache.
the class KTableAggregateTest method testAggBasic.
@Test
public void testAggBasic() {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
final KTable<String, String> table1 = builder.table(topic1, consumed);
final KTable<String, String> table2 = table1.groupBy(MockMapper.noOpKeyValueMapper(), stringSerialized).aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, MockAggregator.TOSTRING_REMOVER, Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as("topic1-Canonized").withValueSerde(stringSerde));
table2.toStream().process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), CONFIG, Instant.ofEpochMilli(0L))) {
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(topic1, new StringSerializer(), new StringSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
inputTopic.pipeInput("A", "1", 10L);
inputTopic.pipeInput("B", "2", 15L);
inputTopic.pipeInput("A", "3", 20L);
inputTopic.pipeInput("B", "4", 18L);
inputTopic.pipeInput("C", "5", 5L);
inputTopic.pipeInput("D", "6", 25L);
inputTopic.pipeInput("B", "7", 15L);
inputTopic.pipeInput("C", "8", 10L);
assertEquals(asList(new KeyValueTimestamp<>("A", "0+1", 10L), new KeyValueTimestamp<>("B", "0+2", 15L), new KeyValueTimestamp<>("A", "0+1-1", 20L), new KeyValueTimestamp<>("A", "0+1-1+3", 20L), new KeyValueTimestamp<>("B", "0+2-2", 18L), new KeyValueTimestamp<>("B", "0+2-2+4", 18L), new KeyValueTimestamp<>("C", "0+5", 5L), new KeyValueTimestamp<>("D", "0+6", 25L), new KeyValueTimestamp<>("B", "0+2-2+4-4", 18L), new KeyValueTimestamp<>("B", "0+2-2+4-4+7", 18L), new KeyValueTimestamp<>("C", "0+5-5", 10L), new KeyValueTimestamp<>("C", "0+5-5+8", 10L)), supplier.theCapturedProcessor().processed());
}
}
use of org.apache.kafka.streams.state.KeyValueStore in project kafka by apache.
the class SuppressScenarioTest method shouldSuppressIntermediateEventsWithRecordLimit.
@Test
public void shouldSuppressIntermediateEventsWithRecordLimit() {
final StreamsBuilder builder = new StreamsBuilder();
final KTable<String, Long> valueCounts = builder.table("input", Consumed.with(STRING_SERDE, STRING_SERDE), Materialized.<String, String, KeyValueStore<Bytes, byte[]>>with(STRING_SERDE, STRING_SERDE).withCachingDisabled().withLoggingDisabled()).groupBy((k, v) -> new KeyValue<>(v, k), Grouped.with(STRING_SERDE, STRING_SERDE)).count(Materialized.with(STRING_SERDE, Serdes.Long()));
valueCounts.suppress(untilTimeLimit(ofMillis(Long.MAX_VALUE), maxRecords(1L).emitEarlyWhenFull())).toStream().to("output-suppressed", Produced.with(STRING_SERDE, Serdes.Long()));
valueCounts.toStream().to("output-raw", Produced.with(STRING_SERDE, Serdes.Long()));
final Topology topology = builder.build();
System.out.println(topology.describe());
try (final TopologyTestDriver driver = new TopologyTestDriver(topology, config)) {
final TestInputTopic<String, String> inputTopic = driver.createInputTopic("input", STRING_SERIALIZER, STRING_SERIALIZER);
inputTopic.pipeInput("k1", "v1", 0L);
inputTopic.pipeInput("k1", "v2", 1L);
inputTopic.pipeInput("k2", "v1", 2L);
verify(drainProducerRecords(driver, "output-raw", STRING_DESERIALIZER, LONG_DESERIALIZER), asList(new KeyValueTimestamp<>("v1", 1L, 0L), new KeyValueTimestamp<>("v1", 0L, 1L), new KeyValueTimestamp<>("v2", 1L, 1L), new KeyValueTimestamp<>("v1", 1L, 2L)));
verify(drainProducerRecords(driver, "output-suppressed", STRING_DESERIALIZER, LONG_DESERIALIZER), asList(// consecutive updates to v1 get suppressed into only the latter.
new KeyValueTimestamp<>("v1", 0L, 1L), new KeyValueTimestamp<>("v2", 1L, 1L)));
inputTopic.pipeInput("x", "x", 3L);
verify(drainProducerRecords(driver, "output-raw", STRING_DESERIALIZER, LONG_DESERIALIZER), singletonList(new KeyValueTimestamp<>("x", 1L, 3L)));
verify(drainProducerRecords(driver, "output-suppressed", STRING_DESERIALIZER, LONG_DESERIALIZER), singletonList(// now we see that last update to v1, but we won't see the update to x until it gets evicted
new KeyValueTimestamp<>("v1", 1L, 2L)));
}
}
Aggregations