Search in sources :

Example 56 with StreamsBuilder

use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.

the class KStreamKStreamLeftJoinTest method testLeftJoin.

@Test
public void testLeftJoin() {
    final StreamsBuilder builder = new StreamsBuilder();
    final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
    final KStream<Integer, String> stream1;
    final KStream<Integer, String> stream2;
    final KStream<Integer, String> joined;
    final MockProcessorSupplier<Integer, String> processor;
    processor = new MockProcessorSupplier<>();
    stream1 = builder.stream(topic1, consumed);
    stream2 = builder.stream(topic2, consumed);
    joined = stream1.leftJoin(stream2, MockValueJoiner.TOSTRING_JOINER, JoinWindows.of(100), Joined.with(intSerde, stringSerde, stringSerde));
    joined.process(processor);
    final Collection<Set<String>> copartitionGroups = StreamsBuilderTest.getCopartitionedGroups(builder);
    assertEquals(1, copartitionGroups.size());
    assertEquals(new HashSet<>(Arrays.asList(topic1, topic2)), copartitionGroups.iterator().next());
    driver.setUp(builder, stateDir, Serdes.Integer(), Serdes.String());
    driver.setTime(0L);
    for (int i = 0; i < 2; i++) {
        driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
    }
    driver.flushState();
    processor.checkAndClearProcessResult("0:X0+null", "1:X1+null");
    for (int i = 0; i < 2; i++) {
        driver.process(topic2, expectedKeys[i], "Y" + expectedKeys[i]);
    }
    driver.flushState();
    processor.checkAndClearProcessResult("0:X0+Y0", "1:X1+Y1");
    for (int i = 0; i < 3; i++) {
        driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
    }
    driver.flushState();
    processor.checkAndClearProcessResult("0:X0+Y0", "1:X1+Y1", "2:X2+null");
    for (int expectedKey : expectedKeys) {
        driver.process(topic2, expectedKey, "YY" + expectedKey);
    }
    driver.flushState();
    processor.checkAndClearProcessResult("0:X0+YY0", "0:X0+YY0", "1:X1+YY1", "1:X1+YY1", "2:X2+YY2");
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "XX" + expectedKey);
    }
    driver.flushState();
    processor.checkAndClearProcessResult("0:XX0+Y0", "0:XX0+YY0", "1:XX1+Y1", "1:XX1+YY1", "2:XX2+YY2", "3:XX3+YY3");
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Set(java.util.Set) HashSet(java.util.HashSet) StreamsBuilderTest(org.apache.kafka.streams.StreamsBuilderTest) Test(org.junit.Test)

Example 57 with StreamsBuilder

use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.

the class KStreamKTableJoinTest method setUp.

@Before
public void setUp() throws IOException {
    stateDir = TestUtils.tempDirectory("kafka-test");
    builder = new StreamsBuilder();
    final KStream<Integer, String> stream;
    final KTable<Integer, String> table;
    processor = new MockProcessorSupplier<>();
    final Consumed<Integer, String> consumed = Consumed.with(intSerde, stringSerde);
    stream = builder.stream(streamTopic, consumed);
    table = builder.table(tableTopic, consumed);
    stream.join(table, MockValueJoiner.TOSTRING_JOINER).process(processor);
    driver.setUp(builder, stateDir);
    driver.setTime(0L);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Before(org.junit.Before)

Example 58 with StreamsBuilder

use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.

the class KStreamKTableLeftJoinTest method setUp.

@Before
public void setUp() throws IOException {
    stateDir = TestUtils.tempDirectory("kafka-test");
    builder = new StreamsBuilder();
    final KStream<Integer, String> stream;
    final KTable<Integer, String> table;
    processor = new MockProcessorSupplier<>();
    final Consumed<Integer, String> consumed = Consumed.with(intSerde, stringSerde);
    stream = builder.stream(streamTopic, consumed);
    table = builder.table(tableTopic, consumed);
    stream.leftJoin(table, MockValueJoiner.TOSTRING_JOINER).process(processor);
    driver.setUp(builder, stateDir);
    driver.setTime(0L);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Before(org.junit.Before)

Example 59 with StreamsBuilder

use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.

the class KStreamPeekTest method shouldObserveStreamElements.

@Test
public void shouldObserveStreamElements() {
    final StreamsBuilder builder = new StreamsBuilder();
    final KStream<Integer, String> stream = builder.stream(topicName, Consumed.with(intSerd, stringSerd));
    final List<KeyValue<Integer, String>> peekObserved = new ArrayList<>(), streamObserved = new ArrayList<>();
    stream.peek(collect(peekObserved)).foreach(collect(streamObserved));
    driver.setUp(builder);
    final List<KeyValue<Integer, String>> expected = new ArrayList<>();
    for (int key = 0; key < 32; key++) {
        final String value = "V" + key;
        driver.process(topicName, key, value);
        expected.add(new KeyValue<>(key, value));
    }
    assertEquals(expected, peekObserved);
    assertEquals(expected, streamObserved);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 60 with StreamsBuilder

use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.

the class KStreamTransformValuesTest method testTransform.

@Test
public void testTransform() {
    StreamsBuilder builder = new StreamsBuilder();
    ValueTransformerSupplier<Number, Integer> valueTransformerSupplier = new ValueTransformerSupplier<Number, Integer>() {

        public ValueTransformer<Number, Integer> get() {
            return new ValueTransformer<Number, Integer>() {

                private int total = 0;

                @Override
                public void init(ProcessorContext context) {
                }

                @Override
                public Integer transform(Number value) {
                    total += value.intValue();
                    return total;
                }

                @Override
                public Integer punctuate(long timestamp) {
                    return null;
                }

                @Override
                public void close() {
                }
            };
        }
    };
    final int[] expectedKeys = { 1, 10, 100, 1000 };
    KStream<Integer, Integer> stream;
    MockProcessorSupplier<Integer, Integer> processor = new MockProcessorSupplier<>();
    stream = builder.stream(topicName, Consumed.with(intSerde, intSerde));
    stream.transformValues(valueTransformerSupplier).process(processor);
    driver.setUp(builder);
    for (int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, expectedKey * 10);
    }
    String[] expected = { "1:10", "10:110", "100:1110", "1000:11110" };
    assertArrayEquals(expected, processor.processed.toArray());
}
Also used : ValueTransformer(org.apache.kafka.streams.kstream.ValueTransformer) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) ValueTransformerSupplier(org.apache.kafka.streams.kstream.ValueTransformerSupplier) Test(org.junit.Test)

Aggregations

StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)189 Test (org.junit.Test)121 KafkaStreams (org.apache.kafka.streams.KafkaStreams)72 Properties (java.util.Properties)61 KeyValue (org.apache.kafka.streams.KeyValue)42 MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)30 StreamsBuilderTest (org.apache.kafka.streams.StreamsBuilderTest)27 Serdes (org.apache.kafka.common.serialization.Serdes)21 KeyValueMapper (org.apache.kafka.streams.kstream.KeyValueMapper)21 Before (org.junit.Before)19 StreamsConfig (org.apache.kafka.streams.StreamsConfig)18 KStream (org.apache.kafka.streams.kstream.KStream)18 Predicate (org.apache.kafka.streams.kstream.Predicate)18 IntegrationTest (org.apache.kafka.test.IntegrationTest)18 Bytes (org.apache.kafka.common.utils.Bytes)16 HashSet (java.util.HashSet)15 ValueMapper (org.apache.kafka.streams.kstream.ValueMapper)14 HashMap (java.util.HashMap)13 KTable (org.apache.kafka.streams.kstream.KTable)13 Produced (org.apache.kafka.streams.kstream.Produced)13