Search in sources :

Example 36 with StreamsBuilder

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

the class KTableKTableInnerJoinTest method testSendingOldValues.

@Test
public void testSendingOldValues() {
    final StreamsBuilder builder = new StreamsBuilder();
    final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
    final KTable<Integer, String> table1;
    final KTable<Integer, String> table2;
    final KTable<Integer, String> joined;
    final MockProcessorSupplier<Integer, String> proc;
    table1 = builder.table(topic1, consumed);
    table2 = builder.table(topic2, consumed);
    joined = table1.join(table2, MockValueJoiner.TOSTRING_JOINER);
    proc = new MockProcessorSupplier<>();
    builder.build().addProcessor("proc", proc, ((KTableImpl<?, ?, ?>) joined).name);
    doTestSendingOldValues(builder, expectedKeys, table1, table2, proc, joined, true);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StreamsBuilderTest(org.apache.kafka.streams.StreamsBuilderTest) Test(org.junit.Test)

Example 37 with StreamsBuilder

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

the class KTableKTableLeftJoinTest method testNotSendingOldValue.

@Test
public void testNotSendingOldValue() {
    final StreamsBuilder builder = new StreamsBuilder();
    final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
    final KTable<Integer, String> table1;
    final KTable<Integer, String> table2;
    final KTable<Integer, String> joined;
    final MockProcessorSupplier<Integer, String> proc;
    table1 = builder.table(topic1, consumed);
    table2 = builder.table(topic2, consumed);
    joined = table1.leftJoin(table2, MockValueJoiner.TOSTRING_JOINER);
    proc = new MockProcessorSupplier<>();
    builder.build().addProcessor("proc", proc, ((KTableImpl<?, ?, ?>) joined).name);
    driver.setUp(builder, stateDir);
    driver.setTime(0L);
    assertTrue(((KTableImpl<?, ?, ?>) table1).sendingOldValueEnabled());
    assertFalse(((KTableImpl<?, ?, ?>) table2).sendingOldValueEnabled());
    assertFalse(((KTableImpl<?, ?, ?>) joined).sendingOldValueEnabled());
    for (int i = 0; i < 2; i++) {
        driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+null<-null)", "1:(X1+null<-null)");
    for (int i = 0; i < 2; i++) {
        driver.process(topic2, expectedKeys[i], "Y" + expectedKeys[i]);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+Y0<-null)", "1:(X1+Y1<-null)");
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "X" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+Y0<-null)", "1:(X1+Y1<-null)", "2:(X2+null<-null)", "3:(X3+null<-null)");
    // push all items to the other stream. this should produce four items.
    for (int expectedKey : expectedKeys) {
        driver.process(topic2, expectedKey, "YY" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+YY0<-null)", "1:(X1+YY1<-null)", "2:(X2+YY2<-null)", "3:(X3+YY3<-null)");
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "X" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+YY0<-null)", "1:(X1+YY1<-null)", "2:(X2+YY2<-null)", "3:(X3+YY3<-null)");
    for (int i = 0; i < 2; i++) {
        driver.process(topic2, expectedKeys[i], null);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+null<-null)", "1:(X1+null<-null)");
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "XX" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(XX0+null<-null)", "1:(XX1+null<-null)", "2:(XX2+YY2<-null)", "3:(XX3+YY3<-null)");
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StreamsBuilderTest(org.apache.kafka.streams.StreamsBuilderTest) Test(org.junit.Test)

Example 38 with StreamsBuilder

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

the class KTableKTableLeftJoinTest method testJoin.

@Test
public void testJoin() {
    final StreamsBuilder builder = new StreamsBuilder();
    final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
    KTable<Integer, String> table1 = builder.table(topic1, consumed);
    KTable<Integer, String> table2 = builder.table(topic2, consumed);
    KTable<Integer, String> joined = table1.leftJoin(table2, MockValueJoiner.TOSTRING_JOINER);
    MockProcessorSupplier<Integer, String> processor;
    processor = new MockProcessorSupplier<>();
    joined.toStream().process(processor);
    Collection<Set<String>> copartitionGroups = StreamsBuilderTest.getCopartitionedGroups(builder);
    assertEquals(1, copartitionGroups.size());
    assertEquals(new HashSet<>(Arrays.asList(topic1, topic2)), copartitionGroups.iterator().next());
    KTableValueGetterSupplier<Integer, String> getterSupplier = ((KTableImpl<Integer, String, String>) joined).valueGetterSupplier();
    driver.setUp(builder, stateDir);
    driver.setTime(0L);
    KTableValueGetter<Integer, String> getter = getterSupplier.get();
    getter.init(driver.context());
    for (int i = 0; i < 2; i++) {
        driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
    }
    // pass tuple with null key, it will be discarded in join process
    driver.process(topic1, null, "SomeVal");
    driver.flushState();
    processor.checkAndClearProcessResult("0:X0+null", "1:X1+null");
    checkJoinedValues(getter, kv(0, "X0+null"), kv(1, "X1+null"), kv(2, null), kv(3, null));
    for (int i = 0; i < 2; i++) {
        driver.process(topic2, expectedKeys[i], "Y" + expectedKeys[i]);
    }
    // pass tuple with null key, it will be discarded in join process
    driver.process(topic2, null, "AnotherVal");
    driver.flushState();
    processor.checkAndClearProcessResult("0:X0+Y0", "1:X1+Y1");
    checkJoinedValues(getter, kv(0, "X0+Y0"), kv(1, "X1+Y1"), kv(2, null), kv(3, null));
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "X" + expectedKey);
    }
    driver.flushState();
    processor.checkAndClearProcessResult("0:X0+Y0", "1:X1+Y1", "2:X2+null", "3:X3+null");
    checkJoinedValues(getter, kv(0, "X0+Y0"), kv(1, "X1+Y1"), kv(2, "X2+null"), kv(3, "X3+null"));
    // push all items to the other stream. this should produce four items.
    for (int expectedKey : expectedKeys) {
        driver.process(topic2, expectedKey, "YY" + expectedKey);
    }
    driver.flushState();
    processor.checkAndClearProcessResult("0:X0+YY0", "1:X1+YY1", "2:X2+YY2", "3:X3+YY3");
    checkJoinedValues(getter, kv(0, "X0+YY0"), kv(1, "X1+YY1"), kv(2, "X2+YY2"), kv(3, "X3+YY3"));
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "X" + expectedKey);
    }
    driver.flushState();
    processor.checkAndClearProcessResult("0:X0+YY0", "1:X1+YY1", "2:X2+YY2", "3:X3+YY3");
    checkJoinedValues(getter, kv(0, "X0+YY0"), kv(1, "X1+YY1"), kv(2, "X2+YY2"), kv(3, "X3+YY3"));
    for (int i = 0; i < 2; i++) {
        driver.process(topic2, expectedKeys[i], null);
    }
    driver.flushState();
    processor.checkAndClearProcessResult("0:X0+null", "1:X1+null");
    checkJoinedValues(getter, kv(0, "X0+null"), kv(1, "X1+null"), kv(2, "X2+YY2"), kv(3, "X3+YY3"));
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "XX" + expectedKey);
    }
    driver.flushState();
    processor.checkAndClearProcessResult("0:XX0+null", "1:XX1+null", "2:XX2+YY2", "3:XX3+YY3");
    checkJoinedValues(getter, kv(0, "XX0+null"), kv(1, "XX1+null"), kv(2, "XX2+YY2"), kv(3, "XX3+YY3"));
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StreamsBuilderTest(org.apache.kafka.streams.StreamsBuilderTest) Test(org.junit.Test)

Example 39 with StreamsBuilder

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

the class KTableKTableOuterJoinTest method testSendingOldValue.

@Test
public void testSendingOldValue() {
    final StreamsBuilder builder = new StreamsBuilder();
    final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
    KTable<Integer, String> table1;
    KTable<Integer, String> table2;
    KTable<Integer, String> joined;
    MockProcessorSupplier<Integer, String> proc;
    table1 = builder.table(topic1, consumed);
    table2 = builder.table(topic2, consumed);
    joined = table1.outerJoin(table2, MockValueJoiner.TOSTRING_JOINER);
    ((KTableImpl<?, ?, ?>) joined).enableSendingOldValues();
    proc = new MockProcessorSupplier<>();
    builder.build().addProcessor("proc", proc, ((KTableImpl<?, ?, ?>) joined).name);
    driver.setUp(builder, stateDir);
    assertTrue(((KTableImpl<?, ?, ?>) table1).sendingOldValueEnabled());
    assertTrue(((KTableImpl<?, ?, ?>) table2).sendingOldValueEnabled());
    assertTrue(((KTableImpl<?, ?, ?>) joined).sendingOldValueEnabled());
    for (int i = 0; i < 2; i++) {
        driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+null<-null)", "1:(X1+null<-null)");
    for (int i = 0; i < 2; i++) {
        driver.process(topic2, expectedKeys[i], "Y" + expectedKeys[i]);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+Y0<-X0+null)", "1:(X1+Y1<-X1+null)");
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "X" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+Y0<-X0+Y0)", "1:(X1+Y1<-X1+Y1)", "2:(X2+null<-null)", "3:(X3+null<-null)");
    // push all items to the other stream. this should produce four items.
    for (int expectedKey : expectedKeys) {
        driver.process(topic2, expectedKey, "YY" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+YY0<-X0+Y0)", "1:(X1+YY1<-X1+Y1)", "2:(X2+YY2<-X2+null)", "3:(X3+YY3<-X3+null)");
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "X" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+YY0<-X0+YY0)", "1:(X1+YY1<-X1+YY1)", "2:(X2+YY2<-X2+YY2)", "3:(X3+YY3<-X3+YY3)");
    for (int i = 0; i < 2; i++) {
        driver.process(topic2, expectedKeys[i], null);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+null<-X0+YY0)", "1:(X1+null<-X1+YY1)");
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "XX" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(XX0+null<-X0+null)", "1:(XX1+null<-X1+null)", "2:(XX2+YY2<-X2+YY2)", "3:(XX3+YY3<-X3+YY3)");
    for (int i = 1; i < 3; i++) {
        driver.process(topic1, expectedKeys[i], null);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("1:(null<-XX1+null)", "2:(null+YY2<-XX2+YY2)");
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StreamsBuilderTest(org.apache.kafka.streams.StreamsBuilderTest) Test(org.junit.Test)

Example 40 with StreamsBuilder

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

the class KTableMapValuesTest method testQueryableValueGetter.

@Test
public void testQueryableValueGetter() {
    StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    String topic2 = "topic2";
    String storeName2 = "storeName2";
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, consumed);
    KTableImpl<String, String, Integer> table2 = (KTableImpl<String, String, Integer>) table1.mapValues(new ValueMapper<String, Integer>() {

        @Override
        public Integer apply(String value) {
            return new Integer(value);
        }
    }, Materialized.<String, Integer, KeyValueStore<Bytes, byte[]>>as("anyMapName").withValueSerde(Serdes.Integer()));
    KTableImpl<String, Integer, Integer> table3 = (KTableImpl<String, Integer, Integer>) table2.filter(new Predicate<String, Integer>() {

        @Override
        public boolean test(String key, Integer value) {
            return (value % 2) == 0;
        }
    }, Materialized.<String, Integer, KeyValueStore<Bytes, byte[]>>as("anyFilterName").withValueSerde(Serdes.Integer()));
    KTableImpl<String, String, String> table4 = (KTableImpl<String, String, String>) table1.through(stringSerde, stringSerde, topic2, storeName2);
    doTestValueGetter(builder, topic1, table1, table2, table3, table4);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Bytes(org.apache.kafka.common.utils.Bytes) ValueMapper(org.apache.kafka.streams.kstream.ValueMapper) Predicate(org.apache.kafka.streams.kstream.Predicate) Test(org.junit.Test)

Aggregations

StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)561 Test (org.junit.Test)430 Properties (java.util.Properties)238 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)173 KafkaStreams (org.apache.kafka.streams.KafkaStreams)171 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)157 KeyValue (org.apache.kafka.streams.KeyValue)156 Serdes (org.apache.kafka.common.serialization.Serdes)134 StreamsConfig (org.apache.kafka.streams.StreamsConfig)107 IntegrationTest (org.apache.kafka.test.IntegrationTest)104 MockApiProcessorSupplier (org.apache.kafka.test.MockApiProcessorSupplier)95 Before (org.junit.Before)93 KStream (org.apache.kafka.streams.kstream.KStream)89 Topology (org.apache.kafka.streams.Topology)88 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)86 Consumed (org.apache.kafka.streams.kstream.Consumed)80 Duration (java.time.Duration)77 KTable (org.apache.kafka.streams.kstream.KTable)76 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)75 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)75