Search in sources :

Example 46 with KeyValueTimestamp

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());
}
Also used : MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) MockApiProcessor(org.apache.kafka.test.MockApiProcessor) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) Test(org.junit.Test)

Example 47 with KeyValueTimestamp

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));
    }
}
Also used : MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) HashMap(java.util.HashMap) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Example 48 with KeyValueTimestamp

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)));
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) TimestampedKeyValueStore(org.apache.kafka.streams.state.TimestampedKeyValueStore) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) Test(org.junit.Test)

Example 49 with KeyValueTimestamp

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));
}
Also used : TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) Test(org.junit.Test)

Example 50 with KeyValueTimestamp

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));
    }
}
Also used : TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Properties(java.util.Properties) Consumed(org.apache.kafka.streams.kstream.Consumed) Test(org.junit.Test) HashMap(java.util.HashMap) KStream(org.apache.kafka.streams.kstream.KStream) Instant(java.time.Instant) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) Duration(java.time.Duration) Map(java.util.Map) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) TestInputTopic(org.apache.kafka.streams.TestInputTopic) StreamsTestUtils(org.apache.kafka.test.StreamsTestUtils) Assert.assertEquals(org.junit.Assert.assertEquals) MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) HashMap(java.util.HashMap) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Aggregations

KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)71 Test (org.junit.Test)65 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)46 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)44 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)36 Properties (java.util.Properties)26 Windowed (org.apache.kafka.streams.kstream.Windowed)22 Serdes (org.apache.kafka.common.serialization.Serdes)21 IntegrationTest (org.apache.kafka.test.IntegrationTest)20 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)19 KeyValue (org.apache.kafka.streams.KeyValue)19 Consumed (org.apache.kafka.streams.kstream.Consumed)17 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)16 MockApiProcessorSupplier (org.apache.kafka.test.MockApiProcessorSupplier)16 Bytes (org.apache.kafka.common.utils.Bytes)15 KStream (org.apache.kafka.streams.kstream.KStream)14 Duration (java.time.Duration)13 KafkaStreams (org.apache.kafka.streams.KafkaStreams)13 TestInputTopic (org.apache.kafka.streams.TestInputTopic)13 Utils.mkProperties (org.apache.kafka.common.utils.Utils.mkProperties)12