Search in sources :

Example 41 with StreamsBuilder

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

the class KTableMapValuesTest method testSendingOldValue.

@Test
public void testSendingOldValue() {
    StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    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);
        }
    });
    table2.enableSendingOldValues();
    MockProcessorSupplier<String, Integer> proc = new MockProcessorSupplier<>();
    builder.build().addProcessor("proc", proc, table2.name);
    driver.setUp(builder, stateDir);
    assertTrue(table1.sendingOldValueEnabled());
    assertTrue(table2.sendingOldValueEnabled());
    driver.process(topic1, "A", "01");
    driver.process(topic1, "B", "01");
    driver.process(topic1, "C", "01");
    driver.flushState();
    proc.checkAndClearProcessResult("A:(1<-null)", "B:(1<-null)", "C:(1<-null)");
    driver.process(topic1, "A", "02");
    driver.process(topic1, "B", "02");
    driver.flushState();
    proc.checkAndClearProcessResult("A:(2<-1)", "B:(2<-1)");
    driver.process(topic1, "A", "03");
    driver.flushState();
    proc.checkAndClearProcessResult("A:(3<-2)");
    driver.process(topic1, "A", null);
    driver.flushState();
    proc.checkAndClearProcessResult("A:(null<-3)");
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) ValueMapper(org.apache.kafka.streams.kstream.ValueMapper) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 42 with StreamsBuilder

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

the class KTableSourceTest method testKTable.

@Test
public void testKTable() {
    final StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    KTable<String, Integer> table1 = builder.table(topic1, Consumed.with(stringSerde, intSerde));
    MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
    table1.toStream().process(proc1);
    driver.setUp(builder, stateDir);
    driver.process(topic1, "A", 1);
    driver.process(topic1, "B", 2);
    driver.process(topic1, "C", 3);
    driver.process(topic1, "D", 4);
    driver.flushState();
    driver.process(topic1, "A", null);
    driver.process(topic1, "B", null);
    driver.flushState();
    assertEquals(Utils.mkList("A:1", "B:2", "C:3", "D:4", "A:null", "B:null"), proc1.processed);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 43 with StreamsBuilder

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

the class KTableSourceTest method testSendingOldValue.

@Test
public void testSendingOldValue() {
    final StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, stringConsumed);
    table1.enableSendingOldValues();
    assertTrue(table1.sendingOldValueEnabled());
    MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
    builder.build().addProcessor("proc1", proc1, table1.name);
    driver.setUp(builder, stateDir);
    driver.process(topic1, "A", "01");
    driver.process(topic1, "B", "01");
    driver.process(topic1, "C", "01");
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(01<-null)", "B:(01<-null)", "C:(01<-null)");
    driver.process(topic1, "A", "02");
    driver.process(topic1, "B", "02");
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(02<-01)", "B:(02<-01)");
    driver.process(topic1, "A", "03");
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(03<-02)");
    driver.process(topic1, "A", null);
    driver.process(topic1, "B", null);
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(null<-03)", "B:(null<-02)");
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 44 with StreamsBuilder

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

the class KStreamFilterTest method testFilter.

@Test
public void testFilter() {
    StreamsBuilder builder = new StreamsBuilder();
    final int[] expectedKeys = new int[] { 1, 2, 3, 4, 5, 6, 7 };
    KStream<Integer, String> stream;
    MockProcessorSupplier<Integer, String> processor;
    processor = new MockProcessorSupplier<>();
    stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
    stream.filter(isMultipleOfThree).process(processor);
    driver.setUp(builder);
    for (int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, "V" + expectedKey);
    }
    assertEquals(2, processor.processed.size());
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Test(org.junit.Test)

Example 45 with StreamsBuilder

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

the class KStreamFlatMapTest method testFlatMap.

@Test
public void testFlatMap() {
    StreamsBuilder builder = new StreamsBuilder();
    KeyValueMapper<Number, Object, Iterable<KeyValue<String, String>>> mapper = new KeyValueMapper<Number, Object, Iterable<KeyValue<String, String>>>() {

        @Override
        public Iterable<KeyValue<String, String>> apply(Number key, Object value) {
            ArrayList<KeyValue<String, String>> result = new ArrayList<>();
            for (int i = 0; i < key.intValue(); i++) {
                result.add(KeyValue.pair(Integer.toString(key.intValue() * 10 + i), value.toString()));
            }
            return result;
        }
    };
    final int[] expectedKeys = { 0, 1, 2, 3 };
    KStream<Integer, String> stream;
    MockProcessorSupplier<String, String> processor;
    processor = new MockProcessorSupplier<>();
    stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
    stream.flatMap(mapper).process(processor);
    driver.setUp(builder);
    for (int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, "V" + expectedKey);
    }
    assertEquals(6, processor.processed.size());
    String[] expected = { "10:V1", "20:V2", "21:V2", "30:V3", "31:V3", "32:V3" };
    for (int i = 0; i < expected.length; i++) {
        assertEquals(expected[i], processor.processed.get(i));
    }
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) 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