Search in sources :

Example 46 with StreamsBuilder

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

the class KStreamFlatMapValuesTest method testFlatMapValuesWithKeys.

@Test
public void testFlatMapValuesWithKeys() {
    StreamsBuilder builder = new StreamsBuilder();
    ValueMapperWithKey<Integer, Number, Iterable<String>> mapper = new ValueMapperWithKey<Integer, Number, Iterable<String>>() {

        @Override
        public Iterable<String> apply(final Integer readOnlyKey, final Number value) {
            ArrayList<String> result = new ArrayList<>();
            result.add("v" + value);
            result.add("k" + readOnlyKey);
            return result;
        }
    };
    final int[] expectedKeys = { 0, 1, 2, 3 };
    final KStream<Integer, Integer> stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.Integer()));
    final MockProcessorSupplier<Integer, String> processor = new MockProcessorSupplier<>();
    stream.flatMapValues(mapper).process(processor);
    driver.setUp(builder);
    for (final int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, expectedKey);
    }
    String[] expected = { "0:v0", "0:k0", "1:v1", "1:k1", "2:v2", "2:k2", "3:v3", "3:k3" };
    assertArrayEquals(expected, processor.processed.toArray());
}
Also used : ArrayList(java.util.ArrayList) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) ValueMapperWithKey(org.apache.kafka.streams.kstream.ValueMapperWithKey) Test(org.junit.Test)

Example 47 with StreamsBuilder

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

the class KStreamFlatMapValuesTest method testFlatMapValues.

@Test
public void testFlatMapValues() {
    StreamsBuilder builder = new StreamsBuilder();
    ValueMapper<Number, Iterable<String>> mapper = new ValueMapper<Number, Iterable<String>>() {

        @Override
        public Iterable<String> apply(Number value) {
            ArrayList<String> result = new ArrayList<String>();
            result.add("v" + value);
            result.add("V" + value);
            return result;
        }
    };
    final int[] expectedKeys = { 0, 1, 2, 3 };
    final KStream<Integer, Integer> stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.Integer()));
    final MockProcessorSupplier<Integer, String> processor = new MockProcessorSupplier<>();
    stream.flatMapValues(mapper).process(processor);
    driver.setUp(builder);
    for (final int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, expectedKey);
    }
    String[] expected = { "0:v0", "0:V0", "1:v1", "1:V1", "2:v2", "2:V2", "3:v3", "3:V3" };
    assertArrayEquals(expected, processor.processed.toArray());
}
Also used : ValueMapper(org.apache.kafka.streams.kstream.ValueMapper) ArrayList(java.util.ArrayList) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 48 with StreamsBuilder

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

the class KStreamForeachTest method testForeach.

@Test
public void testForeach() {
    // Given
    List<KeyValue<Integer, String>> inputRecords = Arrays.asList(new KeyValue<>(0, "zero"), new KeyValue<>(1, "one"), new KeyValue<>(2, "two"), new KeyValue<>(3, "three"));
    List<KeyValue<Integer, String>> expectedRecords = Arrays.asList(new KeyValue<>(0, "ZERO"), new KeyValue<>(2, "ONE"), new KeyValue<>(4, "TWO"), new KeyValue<>(6, "THREE"));
    final List<KeyValue<Integer, String>> actualRecords = new ArrayList<>();
    ForeachAction<Integer, String> action = new ForeachAction<Integer, String>() {

        @Override
        public void apply(Integer key, String value) {
            actualRecords.add(new KeyValue<>(key * 2, value.toUpperCase(Locale.ROOT)));
        }
    };
    // When
    StreamsBuilder builder = new StreamsBuilder();
    KStream<Integer, String> stream = builder.stream(topicName, Consumed.with(intSerde, stringSerde));
    stream.foreach(action);
    // Then
    driver.setUp(builder);
    for (KeyValue<Integer, String> record : inputRecords) {
        driver.process(topicName, record.key, record.value);
    }
    assertEquals(expectedRecords.size(), actualRecords.size());
    for (int i = 0; i < expectedRecords.size(); i++) {
        KeyValue<Integer, String> expectedRecord = expectedRecords.get(i);
        KeyValue<Integer, String> actualRecord = actualRecords.get(i);
        assertEquals(expectedRecord, actualRecord);
    }
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) ForeachAction(org.apache.kafka.streams.kstream.ForeachAction) Test(org.junit.Test)

Example 49 with StreamsBuilder

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

the class KStreamGlobalKTableJoinTest method setUp.

@Before
public void setUp() throws IOException {
    stateDir = TestUtils.tempDirectory("kafka-test");
    builder = new StreamsBuilder();
    final KStream<Integer, String> stream;
    // value of stream optionally contains key of table
    final GlobalKTable<String, String> table;
    final KeyValueMapper<Integer, String, String> keyMapper;
    processor = new MockProcessorSupplier<>();
    final Consumed<Integer, String> streamConsumed = Consumed.with(intSerde, stringSerde);
    final Consumed<String, String> tableConsumed = Consumed.with(stringSerde, stringSerde);
    stream = builder.stream(streamTopic, streamConsumed);
    table = builder.globalTable(globalTableTopic, tableConsumed);
    keyMapper = new KeyValueMapper<Integer, String, String>() {

        @Override
        public String apply(final Integer key, final String value) {
            final String[] tokens = value.split(",");
            // If not present, use null to indicate no match
            return tokens.length > 1 ? tokens[1] : null;
        }
    };
    stream.join(table, keyMapper, 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 50 with StreamsBuilder

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

the class KStreamImplTest method shouldSendDataThroughTopicUsingProduced.

@Test
public void shouldSendDataThroughTopicUsingProduced() {
    final StreamsBuilder builder = new StreamsBuilder();
    final String input = "topic";
    final KStream<String, String> stream = builder.stream(input, consumed);
    final MockProcessorSupplier<String, String> processorSupplier = new MockProcessorSupplier<>();
    stream.through("through-topic", Produced.with(stringSerde, stringSerde)).process(processorSupplier);
    driver.setUp(builder);
    driver.process(input, "a", "b");
    assertThat(processorSupplier.processed, equalTo(Collections.singletonList("a:b")));
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) StreamsBuilderTest(org.apache.kafka.streams.StreamsBuilderTest) 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