Search in sources :

Example 1 with Branched

use of org.apache.kafka.streams.kstream.Branched 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)

Aggregations

Arrays (java.util.Arrays)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Consumer (java.util.function.Consumer)1 IntegerDeserializer (org.apache.kafka.common.serialization.IntegerDeserializer)1 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)1 Serdes (org.apache.kafka.common.serialization.Serdes)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)1 TestInputTopic (org.apache.kafka.streams.TestInputTopic)1 TestOutputTopic (org.apache.kafka.streams.TestOutputTopic)1 Topology (org.apache.kafka.streams.Topology)1 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)1 Branched (org.apache.kafka.streams.kstream.Branched)1 Consumed (org.apache.kafka.streams.kstream.Consumed)1 KStream (org.apache.kafka.streams.kstream.KStream)1 Named (org.apache.kafka.streams.kstream.Named)1 Predicate (org.apache.kafka.streams.kstream.Predicate)1 StreamsTestUtils (org.apache.kafka.test.StreamsTestUtils)1