Search in sources :

Example 1 with ValueTransformer

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

the class KStreamTransformValuesTest method testTransform.

@Test
public void testTransform() {
    KStreamBuilder builder = new KStreamBuilder();
    ValueTransformerSupplier<Number, Integer> valueTransformerSupplier = new ValueTransformerSupplier<Number, Integer>() {

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

                private int total = 0;

                @Override
                public void init(ProcessorContext context) {
                }

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

                @Override
                public Integer punctuate(long timestamp) {
                    return null;
                }

                @Override
                public void close() {
                }
            };
        }
    };
    final int[] expectedKeys = { 1, 10, 100, 1000 };
    KStream<Integer, Integer> stream;
    MockProcessorSupplier<Integer, Integer> processor = new MockProcessorSupplier<>();
    stream = builder.stream(intSerde, intSerde, topicName);
    stream.transformValues(valueTransformerSupplier).process(processor);
    driver = new KStreamTestDriver(builder);
    for (int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, expectedKey * 10);
    }
    assertEquals(4, processor.processed.size());
    String[] expected = { "1:10", "10:110", "100:1110", "1000:11110" };
    for (int i = 0; i < expected.length; i++) {
        assertEquals(expected[i], processor.processed.get(i));
    }
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) ValueTransformer(org.apache.kafka.streams.kstream.ValueTransformer) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) ValueTransformerSupplier(org.apache.kafka.streams.kstream.ValueTransformerSupplier) Test(org.junit.Test)

Example 2 with ValueTransformer

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

the class KStreamTransformValuesTest method shouldNotAllowValueTransformerToCallInternalProcessorContextMethods.

@Test
public void shouldNotAllowValueTransformerToCallInternalProcessorContextMethods() {
    final KStreamTransformValues<Integer, Integer, Integer> transformValue = new KStreamTransformValues<>(new ValueTransformerSupplier<Integer, Integer>() {

        @Override
        public ValueTransformer<Integer, Integer> get() {
            return new BadValueTransformer();
        }
    });
    final Processor transformValueProcessor = transformValue.get();
    transformValueProcessor.init(null);
    try {
        transformValueProcessor.process(null, 0);
        fail("should not allow call to context.forward() within ValueTransformer");
    } catch (final StreamsException e) {
    // expected
    }
    try {
        transformValueProcessor.process(null, 1);
        fail("should not allow call to context.forward() within ValueTransformer");
    } catch (final StreamsException e) {
    // expected
    }
    try {
        transformValueProcessor.process(null, 2);
        fail("should not allow call to context.forward() within ValueTransformer");
    } catch (final StreamsException e) {
    // expected
    }
    try {
        transformValueProcessor.punctuate(0);
        fail("should not allow ValueTransformer#puntuate() to return not-null value");
    } catch (final StreamsException e) {
    // expected
    }
}
Also used : ValueTransformer(org.apache.kafka.streams.kstream.ValueTransformer) Processor(org.apache.kafka.streams.processor.Processor) StreamsException(org.apache.kafka.streams.errors.StreamsException) Test(org.junit.Test)

Aggregations

ValueTransformer (org.apache.kafka.streams.kstream.ValueTransformer)2 Test (org.junit.Test)2 StreamsException (org.apache.kafka.streams.errors.StreamsException)1 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)1 ValueTransformerSupplier (org.apache.kafka.streams.kstream.ValueTransformerSupplier)1 Processor (org.apache.kafka.streams.processor.Processor)1 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)1 KStreamTestDriver (org.apache.kafka.test.KStreamTestDriver)1 MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)1