Search in sources :

Example 61 with StringDeserializer

use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.

the class KStreamSplitTest method testResultingMap.

@Test
public void testResultingMap() {
    final Map<String, KStream<Integer, String>> branches = source.split(Named.as("foo-")).branch(isEven, Branched.as("bar")).branch(isMultipleOfThree, Branched.withConsumer(ks -> {
    })).branch(isMultipleOfFive, Branched.withFunction(ks -> null)).branch(isNegative, Branched.withFunction(ks -> ks)).branch(isMultipleOfSeven).defaultBranch();
    assertEquals(4, branches.size());
    // direct the branched streams into different topics named with branch name
    for (final Map.Entry<String, KStream<Integer, String>> branch : branches.entrySet()) {
        branch.getValue().to(branch.getKey());
    }
    builder.build();
    withDriver(driver -> {
        final TestOutputTopic<Integer, String> even = driver.createOutputTopic("foo-bar", new IntegerDeserializer(), new StringDeserializer());
        final TestOutputTopic<Integer, String> negative = driver.createOutputTopic("foo-4", new IntegerDeserializer(), new StringDeserializer());
        final TestOutputTopic<Integer, String> x7 = driver.createOutputTopic("foo-5", new IntegerDeserializer(), new StringDeserializer());
        final TestOutputTopic<Integer, String> defaultBranch = driver.createOutputTopic("foo-0", new IntegerDeserializer(), new StringDeserializer());
        assertEquals(Arrays.asList("V0", "V2", "V4", "V6"), even.readValuesToList());
        assertEquals(Arrays.asList("V-1"), negative.readValuesToList());
        assertEquals(Arrays.asList("V7"), x7.readValuesToList());
        assertEquals(Arrays.asList("V1"), defaultBranch.readValuesToList());
    });
}
Also used : TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Arrays(java.util.Arrays) TestOutputTopic(org.apache.kafka.streams.TestOutputTopic) Properties(java.util.Properties) Consumed(org.apache.kafka.streams.kstream.Consumed) Test(org.junit.Test) Branched(org.apache.kafka.streams.kstream.Branched) KStream(org.apache.kafka.streams.kstream.KStream) Consumer(java.util.function.Consumer) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Predicate(org.apache.kafka.streams.kstream.Predicate) Named(org.apache.kafka.streams.kstream.Named) Map(java.util.Map) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) 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) Topology(org.apache.kafka.streams.Topology) Assert.assertEquals(org.junit.Assert.assertEquals) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) KStream(org.apache.kafka.streams.kstream.KStream) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Map(java.util.Map) Test(org.junit.Test)

Example 62 with StringDeserializer

use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.

the class SessionWindowedCogroupedKStreamImplTest method sessionWindowMixAggregatorsTest.

@Test
public void sessionWindowMixAggregatorsTest() {
    final KTable<Windowed<String>, String> customers = windowedCogroupedStream.aggregate(MockInitializer.STRING_INIT, sessionMerger, Materialized.with(Serdes.String(), Serdes.String()));
    customers.toStream().to(OUTPUT);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, String> testInputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
        final TestInputTopic<String, String> testInputTopic2 = driver.createInputTopic(TOPIC2, new StringSerializer(), new StringSerializer());
        final TestOutputTopic<Windowed<String>, String> testOutputTopic = driver.createOutputTopic(OUTPUT, new SessionWindowedDeserializer<>(new StringDeserializer()), new StringDeserializer());
        testInputTopic.pipeInput("k1", "A", 0);
        testInputTopic.pipeInput("k2", "A", 0);
        testInputTopic.pipeInput("k2", "A", 1);
        testInputTopic.pipeInput("k1", "A", 2);
        testInputTopic2.pipeInput("k1", "B", 3);
        testInputTopic2.pipeInput("k2", "B", 3);
        testInputTopic2.pipeInput("k2", "B", 444);
        testInputTopic2.pipeInput("k1", "B", 444);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A", 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A", 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", null, 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+0+A+A", 1);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", null, 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+0+A+A", 2);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", null, 2);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+0+0+A+A-B", 3);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", null, 1);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+0+0+A+A-B", 3);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0-B", 444);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0-B", 444);
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Example 63 with StringDeserializer

use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.

the class SessionWindowedCogroupedKStreamImplTest method sessionWindowAggregate2Test.

@Test
public void sessionWindowAggregate2Test() {
    final KTable<Windowed<String>, String> customers = groupedStream.cogroup(MockAggregator.TOSTRING_ADDER).windowedBy(SessionWindows.with(ofMillis(500))).aggregate(MockInitializer.STRING_INIT, sessionMerger, Materialized.with(Serdes.String(), Serdes.String()));
    customers.toStream().to(OUTPUT);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, String> testInputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
        final TestOutputTopic<Windowed<String>, String> testOutputTopic = driver.createOutputTopic(OUTPUT, new SessionWindowedDeserializer<>(new StringDeserializer()), new StringDeserializer());
        testInputTopic.pipeInput("k1", "A", 0);
        testInputTopic.pipeInput("k1", "A", 0);
        testInputTopic.pipeInput("k2", "B", 599);
        testInputTopic.pipeInput("k1", "B", 607);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A", 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+0+A+A", 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+B", 599);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+B", 607);
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Example 64 with StringDeserializer

use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.

the class SlidingWindowedCogroupedKStreamImplTest method slidingWindowAggregateStreamsTest.

@Test
public void slidingWindowAggregateStreamsTest() {
    final KTable<Windowed<String>, String> customers = windowedCogroupedStream.aggregate(MockInitializer.STRING_INIT, Materialized.with(Serdes.String(), Serdes.String()));
    customers.toStream().to(OUTPUT);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, String> testInputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
        final TestOutputTopic<Windowed<String>, String> testOutputTopic = driver.createOutputTopic(OUTPUT, new TimeWindowedDeserializer<>(new StringDeserializer(), WINDOW_SIZE_MS), new StringDeserializer());
        testInputTopic.pipeInput("k1", "A", 500);
        testInputTopic.pipeInput("k2", "A", 500);
        testInputTopic.pipeInput("k2", "A", 501);
        testInputTopic.pipeInput("k1", "A", 502);
        testInputTopic.pipeInput("k1", "B", 503);
        testInputTopic.pipeInput("k2", "B", 503);
        testInputTopic.pipeInput("k2", "B", 504);
        testInputTopic.pipeInput("k1", "B", 504);
        final List<TestRecord<Windowed<String>, String>> results = testOutputTopic.readRecordsToList();
        final List<TestRecord<Windowed<String>, String>> expected = new LinkedList<>();
        // k1-A-500
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(0L, 500L)), "0+A", null, 500L));
        // k2-A-500
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(0L, 500L)), "0+A", null, 500L));
        // k2-A-501
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(501L, 1001L)), "0+A", null, 501L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(1L, 501L)), "0+A+A", null, 501L));
        // k1-A-502
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(501L, 1001L)), "0+A", null, 502L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(2L, 502L)), "0+A+A", null, 502L));
        // k1-B-503
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(501L, 1001L)), "0+A+B", null, 503L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(503L, 1003L)), "0+B", null, 503L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(3L, 503L)), "0+A+A+B", null, 503L));
        // k2-B-503
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(501L, 1001L)), "0+A+B", null, 503L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(502L, 1002)), "0+B", null, 503L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(3L, 503L)), "0+A+A+B", null, 503L));
        // k2-B-504
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(502L, 1002L)), "0+B+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(501L, 1001L)), "0+A+B+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(504L, 1004L)), "0+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(4L, 504L)), "0+A+A+B+B", null, 504L));
        // k1-B-504
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(503L, 1003L)), "0+B+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(501L, 1001L)), "0+A+B+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(504L, 1004L)), "0+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(4L, 504L)), "0+A+A+B+B", null, 504L));
        assertEquals(expected, results);
    }
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) LinkedList(java.util.LinkedList) Windowed(org.apache.kafka.streams.kstream.Windowed) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) TestRecord(org.apache.kafka.streams.test.TestRecord) Test(org.junit.Test)

Example 65 with StringDeserializer

use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.

the class TimeWindowedCogroupedKStreamImplTest method timeWindowAggregateOverlappingWindowsTest.

@Test
public void timeWindowAggregateOverlappingWindowsTest() {
    final KTable<Windowed<String>, String> customers = groupedStream.cogroup(MockAggregator.TOSTRING_ADDER).windowedBy(TimeWindows.of(ofMillis(500L)).advanceBy(ofMillis(200L))).aggregate(MockInitializer.STRING_INIT, Materialized.with(Serdes.String(), Serdes.String()));
    customers.toStream().to(OUTPUT);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, String> testInputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
        final TestOutputTopic<Windowed<String>, String> testOutputTopic = driver.createOutputTopic(OUTPUT, new TimeWindowedDeserializer<>(new StringDeserializer(), WINDOW_SIZE), new StringDeserializer());
        testInputTopic.pipeInput("k1", "A", 0);
        testInputTopic.pipeInput("k2", "A", 0);
        testInputTopic.pipeInput("k1", "B", 250);
        testInputTopic.pipeInput("k2", "B", 250);
        testInputTopic.pipeInput("k2", "A", 500L);
        testInputTopic.pipeInput("k1", "A", 500L);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A", 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A", 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A+B", 250);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+B", 250);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A+B", 250);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+B", 250);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+B+A", 500);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A", 500);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+B+A", 500);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A", 500);
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Aggregations

StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)152 Test (org.junit.Test)91 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)59 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)46 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)35 HashMap (java.util.HashMap)33 Properties (java.util.Properties)32 IntegerDeserializer (org.apache.kafka.common.serialization.IntegerDeserializer)31 Windowed (org.apache.kafka.streams.kstream.Windowed)31 List (java.util.List)29 KeyValue (org.apache.kafka.streams.KeyValue)29 IntegrationTest (org.apache.kafka.test.IntegrationTest)27 ArrayList (java.util.ArrayList)26 LongDeserializer (org.apache.kafka.common.serialization.LongDeserializer)25 Map (java.util.Map)20 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)20 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)17 Serdes (org.apache.kafka.common.serialization.Serdes)17 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)17 KStream (org.apache.kafka.streams.kstream.KStream)17