use of org.apache.kafka.streams.KeyValueTimestamp in project kafka by apache.
the class KTableImplTest method testKTable.
@Test
public void testKTable() {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
final String topic2 = "topic2";
final KTable<String, String> table1 = builder.table(topic1, consumed);
final MockApiProcessorSupplier<String, Object, Void, Void> supplier = new MockApiProcessorSupplier<>();
table1.toStream().process(supplier);
final KTable<String, Integer> table2 = table1.mapValues(s -> Integer.valueOf(s));
table2.toStream().process(supplier);
final KTable<String, Integer> table3 = table2.filter((key, value) -> (value % 2) == 0);
table3.toStream().process(supplier);
table1.toStream().to(topic2, produced);
final KTable<String, String> table4 = builder.table(topic2, consumed);
table4.toStream().process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(topic1, new StringSerializer(), new StringSerializer());
inputTopic.pipeInput("A", "01", 5L);
inputTopic.pipeInput("B", "02", 100L);
inputTopic.pipeInput("C", "03", 0L);
inputTopic.pipeInput("D", "04", 0L);
inputTopic.pipeInput("A", "05", 10L);
inputTopic.pipeInput("A", "06", 8L);
}
final List<MockApiProcessor<String, Object, Void, Void>> processors = supplier.capturedProcessors(4);
assertEquals(asList(new KeyValueTimestamp<>("A", "01", 5), new KeyValueTimestamp<>("B", "02", 100), new KeyValueTimestamp<>("C", "03", 0), new KeyValueTimestamp<>("D", "04", 0), new KeyValueTimestamp<>("A", "05", 10), new KeyValueTimestamp<>("A", "06", 8)), processors.get(0).processed());
assertEquals(asList(new KeyValueTimestamp<>("A", 1, 5), new KeyValueTimestamp<>("B", 2, 100), new KeyValueTimestamp<>("C", 3, 0), new KeyValueTimestamp<>("D", 4, 0), new KeyValueTimestamp<>("A", 5, 10), new KeyValueTimestamp<>("A", 6, 8)), processors.get(1).processed());
assertEquals(asList(new KeyValueTimestamp<>("A", null, 5), new KeyValueTimestamp<>("B", 2, 100), new KeyValueTimestamp<>("C", null, 0), new KeyValueTimestamp<>("D", 4, 0), new KeyValueTimestamp<>("A", null, 10), new KeyValueTimestamp<>("A", 6, 8)), processors.get(2).processed());
assertEquals(asList(new KeyValueTimestamp<>("A", "01", 5), new KeyValueTimestamp<>("B", "02", 100), new KeyValueTimestamp<>("C", "03", 0), new KeyValueTimestamp<>("D", "04", 0), new KeyValueTimestamp<>("A", "05", 10), new KeyValueTimestamp<>("A", "06", 8)), processors.get(3).processed());
}
use of org.apache.kafka.streams.KeyValueTimestamp in project kafka by apache.
the class KTableMapKeysTest method testMapKeysConvertingToStream.
@Test
public void testMapKeysConvertingToStream() {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic_map_keys";
final KTable<Integer, String> table1 = builder.table(topic1, Consumed.with(Serdes.Integer(), Serdes.String()));
final Map<Integer, String> keyMap = new HashMap<>();
keyMap.put(1, "ONE");
keyMap.put(2, "TWO");
keyMap.put(3, "THREE");
final KStream<String, String> convertedStream = table1.toStream((key, value) -> keyMap.get(key));
final KeyValueTimestamp[] expected = new KeyValueTimestamp[] { new KeyValueTimestamp<>("ONE", "V_ONE", 5), new KeyValueTimestamp<>("TWO", "V_TWO", 10), new KeyValueTimestamp<>("THREE", "V_THREE", 15) };
final int[] originalKeys = new int[] { 1, 2, 3 };
final String[] values = new String[] { "V_ONE", "V_TWO", "V_THREE" };
final MockApiProcessorSupplier<String, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
convertedStream.process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
for (int i = 0; i < originalKeys.length; i++) {
final TestInputTopic<Integer, String> inputTopic = driver.createInputTopic(topic1, new IntegerSerializer(), new StringSerializer());
inputTopic.pipeInput(originalKeys[i], values[i], 5 + i * 5);
}
}
assertEquals(3, supplier.theCapturedProcessor().processed().size());
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], supplier.theCapturedProcessor().processed().get(i));
}
}
use of org.apache.kafka.streams.KeyValueTimestamp in project kafka by apache.
the class KTableTransformValuesTest method shouldTransformValuesWithKeyAndMaterialize.
@Test
public void shouldTransformValuesWithKeyAndMaterialize() {
builder.addStateStore(storeBuilder(STORE_NAME)).table(INPUT_TOPIC, CONSUMED).transformValues(new ExclamationValueTransformerSupplier(STORE_NAME, QUERYABLE_NAME), Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as(QUERYABLE_NAME).withKeySerde(Serdes.String()).withValueSerde(Serdes.String()), STORE_NAME).toStream().process(capture);
driver = new TopologyTestDriver(builder.build(), props());
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(INPUT_TOPIC, new StringSerializer(), new StringSerializer());
inputTopic.pipeInput("A", "a", 5L);
inputTopic.pipeInput("B", "b", 10L);
inputTopic.pipeInput("C", null, 15L);
assertThat(output(), hasItems(new KeyValueTimestamp<>("A", "A->a!", 5), new KeyValueTimestamp<>("B", "B->b!", 10), new KeyValueTimestamp<>("C", "C->null!", 15)));
{
final KeyValueStore<String, String> keyValueStore = driver.getKeyValueStore(QUERYABLE_NAME);
assertThat(keyValueStore.get("A"), is("A->a!"));
assertThat(keyValueStore.get("B"), is("B->b!"));
assertThat(keyValueStore.get("C"), is("C->null!"));
}
{
final KeyValueStore<String, ValueAndTimestamp<String>> keyValueStore = driver.getTimestampedKeyValueStore(QUERYABLE_NAME);
assertThat(keyValueStore.get("A"), is(ValueAndTimestamp.make("A->a!", 5L)));
assertThat(keyValueStore.get("B"), is(ValueAndTimestamp.make("B->b!", 10L)));
assertThat(keyValueStore.get("C"), is(ValueAndTimestamp.make("C->null!", 15L)));
}
}
use of org.apache.kafka.streams.KeyValueTimestamp in project kafka by apache.
the class KTableTransformValuesTest method shouldTransformValuesWithKey.
@Test
public void shouldTransformValuesWithKey() {
builder.addStateStore(storeBuilder(STORE_NAME)).addStateStore(storeBuilder(OTHER_STORE_NAME)).table(INPUT_TOPIC, CONSUMED).transformValues(new ExclamationValueTransformerSupplier(STORE_NAME, OTHER_STORE_NAME), STORE_NAME, OTHER_STORE_NAME).toStream().process(capture);
driver = new TopologyTestDriver(builder.build(), props());
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(INPUT_TOPIC, new StringSerializer(), new StringSerializer());
inputTopic.pipeInput("A", "a", 5L);
inputTopic.pipeInput("B", "b", 10L);
inputTopic.pipeInput("D", null, 15L);
assertThat(output(), hasItems(new KeyValueTimestamp<>("A", "A->a!", 5), new KeyValueTimestamp<>("B", "B->b!", 10), new KeyValueTimestamp<>("D", "D->null!", 15)));
assertNull("Store should not be materialized", driver.getKeyValueStore(QUERYABLE_NAME));
}
use of org.apache.kafka.streams.KeyValueTimestamp in project kafka by apache.
the class KStreamSelectKeyTest method testSelectKey.
@Test
public void testSelectKey() {
final StreamsBuilder builder = new StreamsBuilder();
final Map<Number, String> keyMap = new HashMap<>();
keyMap.put(1, "ONE");
keyMap.put(2, "TWO");
keyMap.put(3, "THREE");
final KeyValueTimestamp[] expected = new KeyValueTimestamp[] { new KeyValueTimestamp<>("ONE", 1, 0), new KeyValueTimestamp<>("TWO", 2, 0), new KeyValueTimestamp<>("THREE", 3, 0) };
final int[] expectedValues = new int[] { 1, 2, 3 };
final KStream<String, Integer> stream = builder.stream(topicName, Consumed.with(Serdes.String(), Serdes.Integer()));
final MockApiProcessorSupplier<String, Integer, Void, Void> supplier = new MockApiProcessorSupplier<>();
stream.selectKey((key, value) -> keyMap.get(value)).process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, Integer> inputTopic = driver.createInputTopic(topicName, new StringSerializer(), new IntegerSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
for (final int expectedValue : expectedValues) {
inputTopic.pipeInput(expectedValue);
}
}
assertEquals(3, supplier.theCapturedProcessor().processed().size());
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], supplier.theCapturedProcessor().processed().get(i));
}
}
Aggregations