Search in sources :

Example 1 with ValueTransformerWithKey

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

the class KStreamTransformValuesTest method testTransformWithKey.

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

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

                private int total = 0;

                @Override
                public void init(final ProcessorContext context) {
                }

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

                @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:11", "10:121", "100:1221", "1000:12221" };
    assertArrayEquals(expected, processor.processed.toArray());
}
Also used : ValueTransformerWithKey(org.apache.kafka.streams.kstream.ValueTransformerWithKey) ValueTransformerWithKeySupplier(org.apache.kafka.streams.kstream.ValueTransformerWithKeySupplier) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 2 with ValueTransformerWithKey

use of org.apache.kafka.streams.kstream.ValueTransformerWithKey in project kafka by apache.

the class KTableImplTest method shouldPreserveSerdesForOperators.

@Test
public void shouldPreserveSerdesForOperators() {
    final StreamsBuilder builder = new StreamsBuilder();
    final KTable<String, String> table1 = builder.table("topic-2", stringConsumed);
    final ConsumedInternal<String, String> consumedInternal = new ConsumedInternal<>(stringConsumed);
    final KeyValueMapper<String, String, String> selector = (key, value) -> key;
    final ValueMapper<String, String> mapper = value -> value;
    final ValueJoiner<String, String, String> joiner = (value1, value2) -> value1;
    final ValueTransformerWithKeySupplier<String, String, String> valueTransformerWithKeySupplier = () -> new ValueTransformerWithKey<String, String, String>() {

        @Override
        public void init(final ProcessorContext context) {
        }

        @Override
        public String transform(final String key, final String value) {
            return value;
        }

        @Override
        public void close() {
        }
    };
    assertEquals(((AbstractStream) table1.filter((key, value) -> false)).keySerde(), consumedInternal.keySerde());
    assertEquals(((AbstractStream) table1.filter((key, value) -> false)).valueSerde(), consumedInternal.valueSerde());
    assertEquals(((AbstractStream) table1.filter((key, value) -> false, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
    assertEquals(((AbstractStream) table1.filter((key, value) -> false, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
    assertEquals(((AbstractStream) table1.filterNot((key, value) -> false)).keySerde(), consumedInternal.keySerde());
    assertEquals(((AbstractStream) table1.filterNot((key, value) -> false)).valueSerde(), consumedInternal.valueSerde());
    assertEquals(((AbstractStream) table1.filterNot((key, value) -> false, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
    assertEquals(((AbstractStream) table1.filterNot((key, value) -> false, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
    assertEquals(((AbstractStream) table1.mapValues(mapper)).keySerde(), consumedInternal.keySerde());
    assertNull(((AbstractStream) table1.mapValues(mapper)).valueSerde());
    assertEquals(((AbstractStream) table1.mapValues(mapper, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
    assertEquals(((AbstractStream) table1.mapValues(mapper, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
    assertEquals(((AbstractStream) table1.toStream()).keySerde(), consumedInternal.keySerde());
    assertEquals(((AbstractStream) table1.toStream()).valueSerde(), consumedInternal.valueSerde());
    assertNull(((AbstractStream) table1.toStream(selector)).keySerde());
    assertEquals(((AbstractStream) table1.toStream(selector)).valueSerde(), consumedInternal.valueSerde());
    assertEquals(((AbstractStream) table1.transformValues(valueTransformerWithKeySupplier)).keySerde(), consumedInternal.keySerde());
    assertNull(((AbstractStream) table1.transformValues(valueTransformerWithKeySupplier)).valueSerde());
    assertEquals(((AbstractStream) table1.transformValues(valueTransformerWithKeySupplier, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
    assertEquals(((AbstractStream) table1.transformValues(valueTransformerWithKeySupplier, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
    assertNull(((AbstractStream) table1.groupBy(KeyValue::new)).keySerde());
    assertNull(((AbstractStream) table1.groupBy(KeyValue::new)).valueSerde());
    assertEquals(((AbstractStream) table1.groupBy(KeyValue::new, Grouped.with(mySerde, mySerde))).keySerde(), mySerde);
    assertEquals(((AbstractStream) table1.groupBy(KeyValue::new, Grouped.with(mySerde, mySerde))).valueSerde(), mySerde);
    assertEquals(((AbstractStream) table1.join(table1, joiner)).keySerde(), consumedInternal.keySerde());
    assertNull(((AbstractStream) table1.join(table1, joiner)).valueSerde());
    assertEquals(((AbstractStream) table1.join(table1, joiner, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
    assertEquals(((AbstractStream) table1.join(table1, joiner, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
    assertEquals(((AbstractStream) table1.leftJoin(table1, joiner)).keySerde(), consumedInternal.keySerde());
    assertNull(((AbstractStream) table1.leftJoin(table1, joiner)).valueSerde());
    assertEquals(((AbstractStream) table1.leftJoin(table1, joiner, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
    assertEquals(((AbstractStream) table1.leftJoin(table1, joiner, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
    assertEquals(((AbstractStream) table1.outerJoin(table1, joiner)).keySerde(), consumedInternal.keySerde());
    assertNull(((AbstractStream) table1.outerJoin(table1, joiner)).valueSerde());
    assertEquals(((AbstractStream) table1.outerJoin(table1, joiner, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
    assertEquals(((AbstractStream) table1.outerJoin(table1, joiner, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
}
Also used : Produced(org.apache.kafka.streams.kstream.Produced) MockReducer(org.apache.kafka.test.MockReducer) MockApiProcessor(org.apache.kafka.test.MockApiProcessor) MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) Serde(org.apache.kafka.common.serialization.Serde) Arrays.asList(java.util.Arrays.asList) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) SourceNode(org.apache.kafka.streams.processor.internals.SourceNode) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) ValueMapperWithKey(org.apache.kafka.streams.kstream.ValueMapperWithKey) MockValueJoiner(org.apache.kafka.test.MockValueJoiner) TopologyTestDriverWrapper(org.apache.kafka.streams.TopologyTestDriverWrapper) MockMapper(org.apache.kafka.test.MockMapper) KeyValue(org.apache.kafka.streams.KeyValue) Bytes(org.apache.kafka.common.utils.Bytes) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) List(java.util.List) ValueJoiner(org.apache.kafka.streams.kstream.ValueJoiner) Materialized(org.apache.kafka.streams.kstream.Materialized) Matchers.is(org.hamcrest.Matchers.is) Topology(org.apache.kafka.streams.Topology) MockInitializer(org.apache.kafka.test.MockInitializer) Assert.assertThrows(org.junit.Assert.assertThrows) EasyMock.mock(org.easymock.EasyMock.mock) TopologyDescription(org.apache.kafka.streams.TopologyDescription) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) ValueMapper(org.apache.kafka.streams.kstream.ValueMapper) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KTable(org.apache.kafka.streams.kstream.KTable) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) Properties(java.util.Properties) Consumed(org.apache.kafka.streams.kstream.Consumed) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Field(java.lang.reflect.Field) ValueTransformerWithKeySupplier(org.apache.kafka.streams.kstream.ValueTransformerWithKeySupplier) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) Grouped(org.apache.kafka.streams.kstream.Grouped) MockAggregator(org.apache.kafka.test.MockAggregator) Assert.assertNull(org.junit.Assert.assertNull) Subtopology(org.apache.kafka.streams.TopologyDescription.Subtopology) ValueTransformerWithKey(org.apache.kafka.streams.kstream.ValueTransformerWithKey) TestInputTopic(org.apache.kafka.streams.TestInputTopic) SinkNode(org.apache.kafka.streams.processor.internals.SinkNode) StreamsTestUtils(org.apache.kafka.test.StreamsTestUtils) Assert.assertEquals(org.junit.Assert.assertEquals) KeyValue(org.apache.kafka.streams.KeyValue) ValueTransformerWithKey(org.apache.kafka.streams.kstream.ValueTransformerWithKey) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Test(org.junit.Test)

Example 3 with ValueTransformerWithKey

use of org.apache.kafka.streams.kstream.ValueTransformerWithKey in project kafka by apache.

the class KStreamTransformValuesTest method testTransformWithKey.

// Old PAPI. Needs to be migrated.
@SuppressWarnings("deprecation")
@Test
public void testTransformWithKey() {
    final StreamsBuilder builder = new StreamsBuilder();
    final ValueTransformerWithKeySupplier<Integer, Number, Integer> valueTransformerSupplier = () -> new ValueTransformerWithKey<Integer, Number, Integer>() {

        private int total = 0;

        @Override
        public void init(final org.apache.kafka.streams.processor.ProcessorContext context) {
        }

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

        @Override
        public void close() {
        }
    };
    final int[] expectedKeys = { 1, 10, 100, 1000 };
    final KStream<Integer, Integer> stream;
    stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.Integer()));
    stream.transformValues(valueTransformerSupplier).process(supplier);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<Integer, Integer> inputTopic = driver.createInputTopic(topicName, new IntegerSerializer(), new IntegerSerializer());
        for (final int expectedKey : expectedKeys) {
            inputTopic.pipeInput(expectedKey, expectedKey * 10, expectedKey / 2L);
        }
    }
    final KeyValueTimestamp[] expected = { new KeyValueTimestamp<>(1, 11, 0), new KeyValueTimestamp<>(10, 121, 5), new KeyValueTimestamp<>(100, 1221, 50), new KeyValueTimestamp<>(1000, 12221, 500) };
    assertArrayEquals(expected, supplier.theCapturedProcessor().processed().toArray());
}
Also used : ValueTransformerWithKey(org.apache.kafka.streams.kstream.ValueTransformerWithKey) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) ForwardingDisabledProcessorContext(org.apache.kafka.streams.processor.internals.ForwardingDisabledProcessorContext) InternalProcessorContext(org.apache.kafka.streams.processor.internals.InternalProcessorContext) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) Test(org.junit.Test)

Aggregations

StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)3 ValueTransformerWithKey (org.apache.kafka.streams.kstream.ValueTransformerWithKey)3 Test (org.junit.Test)3 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)2 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)2 ValueTransformerWithKeySupplier (org.apache.kafka.streams.kstream.ValueTransformerWithKeySupplier)2 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)2 Field (java.lang.reflect.Field)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Properties (java.util.Properties)1 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)1 Serde (org.apache.kafka.common.serialization.Serde)1 Serdes (org.apache.kafka.common.serialization.Serdes)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 Bytes (org.apache.kafka.common.utils.Bytes)1 KeyValue (org.apache.kafka.streams.KeyValue)1 TestInputTopic (org.apache.kafka.streams.TestInputTopic)1 Topology (org.apache.kafka.streams.Topology)1 TopologyDescription (org.apache.kafka.streams.TopologyDescription)1