Search in sources :

Example 1 with ValueMapperWithKey

use of org.apache.kafka.streams.kstream.ValueMapperWithKey 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 2 with ValueMapperWithKey

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

the class KStreamMapValuesTest method testMapValuesWithKeys.

@Test
public void testMapValuesWithKeys() {
    StreamsBuilder builder = new StreamsBuilder();
    ValueMapperWithKey<Integer, CharSequence, Integer> mapper = new ValueMapperWithKey<Integer, CharSequence, Integer>() {

        @Override
        public Integer apply(final Integer readOnlyKey, final CharSequence value) {
            return value.length() + readOnlyKey;
        }
    };
    final int[] expectedKeys = { 1, 10, 100, 1000 };
    KStream<Integer, String> stream;
    MockProcessorSupplier<Integer, Integer> processor = new MockProcessorSupplier<>();
    stream = builder.stream(topicName, Consumed.with(intSerde, stringSerde));
    stream.mapValues(mapper).process(processor);
    driver.setUp(builder);
    for (int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, Integer.toString(expectedKey));
    }
    String[] expected = { "1:2", "10:12", "100:103", "1000:1004" };
    assertArrayEquals(expected, processor.processed.toArray());
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) ValueMapperWithKey(org.apache.kafka.streams.kstream.ValueMapperWithKey) Test(org.junit.Test)

Aggregations

StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)2 ValueMapperWithKey (org.apache.kafka.streams.kstream.ValueMapperWithKey)2 MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1