Search in sources :

Example 1 with AbstractProcessor

use of org.apache.kafka.streams.processor.AbstractProcessor in project kafka by apache.

the class SimpleBenchmark method createKafkaStreamsWithStateStore.

private KafkaStreams createKafkaStreamsWithStateStore(String topic, final CountDownLatch latch, boolean enableCaching) {
    Properties props = setStreamProperties("simple-benchmark-streams-with-store" + enableCaching);
    KStreamBuilder builder = new KStreamBuilder();
    if (enableCaching) {
        builder.addStateStore(Stores.create("store").withIntegerKeys().withByteArrayValues().persistent().enableCaching().build());
    } else {
        builder.addStateStore(Stores.create("store").withIntegerKeys().withByteArrayValues().persistent().build());
    }
    KStream<Integer, byte[]> source = builder.stream(INTEGER_SERDE, BYTE_SERDE, topic);
    source.process(new ProcessorSupplier<Integer, byte[]>() {

        @Override
        public Processor<Integer, byte[]> get() {
            return new AbstractProcessor<Integer, byte[]>() {

                KeyValueStore<Integer, byte[]> store;

                @SuppressWarnings("unchecked")
                @Override
                public void init(ProcessorContext context) {
                    store = (KeyValueStore<Integer, byte[]>) context.getStateStore("store");
                }

                @Override
                public void process(Integer key, byte[] value) {
                    store.put(key, value);
                    processedRecords++;
                    processedBytes += value.length + Integer.SIZE;
                    if (processedRecords == numRecords) {
                        latch.countDown();
                    }
                }

                @Override
                public void punctuate(long timestamp) {
                }

                @Override
                public void close() {
                }
            };
        }
    }, "store");
    return createKafkaStreamsWithExceptionHandler(builder, props);
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) Processor(org.apache.kafka.streams.processor.Processor) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Properties(java.util.Properties) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext)

Example 2 with AbstractProcessor

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

the class MockProcessorContextTest method shouldCaptureRecordsOutputToChildByName.

@Test
public void shouldCaptureRecordsOutputToChildByName() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        private int count = 0;

        @Override
        public void process(final String key, final Long value) {
            if (count == 0) {
                // broadcast
                context().forward("start", -1L, To.all());
            }
            final To toChild = count % 2 == 0 ? To.child("george") : To.child("pete");
            context().forward(key + value, key.length() + value, toChild);
            count++;
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    {
        final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
        final CapturedForward forward1 = forwarded.next();
        assertEquals(new KeyValue<>("start", -1L), forward1.keyValue());
        assertEquals(null, forward1.childName());
        final CapturedForward forward2 = forwarded.next();
        assertEquals(new KeyValue<>("foo5", 8L), forward2.keyValue());
        assertEquals("george", forward2.childName());
        final CapturedForward forward3 = forwarded.next();
        assertEquals(new KeyValue<>("barbaz50", 56L), forward3.keyValue());
        assertEquals("pete", forward3.childName());
        assertFalse(forwarded.hasNext());
    }
    {
        final Iterator<CapturedForward> forwarded = context.forwarded("george").iterator();
        assertEquals(new KeyValue<>("start", -1L), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("foo5", 8L), forwarded.next().keyValue());
        assertFalse(forwarded.hasNext());
    }
    {
        final Iterator<CapturedForward> forwarded = context.forwarded("pete").iterator();
        assertEquals(new KeyValue<>("start", -1L), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("barbaz50", 56L), forwarded.next().keyValue());
        assertFalse(forwarded.hasNext());
    }
    {
        final Iterator<CapturedForward> forwarded = context.forwarded("steve").iterator();
        assertEquals(new KeyValue<>("start", -1L), forwarded.next().keyValue());
        assertFalse(forwarded.hasNext());
    }
}
Also used : CapturedForward(org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward) Iterator(java.util.Iterator) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) To(org.apache.kafka.streams.processor.To) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 3 with AbstractProcessor

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

the class MockProcessorContextTest method shouldThrowIfForwardedWithDeprecatedChildIndex.

@Test
public void shouldThrowIfForwardedWithDeprecatedChildIndex() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        @Override
        public void process(final String key, final Long value) {
            // noinspection deprecation
            context().forward(key, value, 0);
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    try {
        processor.process("foo", 5L);
        fail("Should have thrown an UnsupportedOperationException.");
    } catch (final UnsupportedOperationException expected) {
    // expected
    }
}
Also used : AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 4 with AbstractProcessor

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

the class MockProcessorContextTest method shouldCaptureCommitsAndAllowReset.

@Test
public void shouldCaptureCommitsAndAllowReset() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        private int count = 0;

        @Override
        public void process(final String key, final Long value) {
            if (++count > 2)
                context().commit();
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    assertFalse(context.committed());
    processor.process("foobar", 500L);
    assertTrue(context.committed());
    context.resetCommit();
    assertFalse(context.committed());
}
Also used : AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 5 with AbstractProcessor

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

the class MockProcessorContextTest method shouldThrowIfForwardedWithDeprecatedChildName.

@Test
public void shouldThrowIfForwardedWithDeprecatedChildName() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        @Override
        public void process(final String key, final Long value) {
            // noinspection deprecation
            context().forward(key, value, "child1");
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    try {
        processor.process("foo", 5L);
        fail("Should have thrown an UnsupportedOperationException.");
    } catch (final UnsupportedOperationException expected) {
    // expected
    }
}
Also used : AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Aggregations

AbstractProcessor (org.apache.kafka.streams.processor.AbstractProcessor)15 Test (org.junit.Test)9 MockProcessorContext (org.apache.kafka.streams.processor.MockProcessorContext)8 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)7 Processor (org.apache.kafka.streams.processor.Processor)6 Properties (java.util.Properties)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)3 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)3 CapturedForward (org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward)3 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)3 Iterator (java.util.Iterator)2 KafkaException (org.apache.kafka.common.KafkaException)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1 TaskId (org.apache.kafka.streams.processor.TaskId)1 To (org.apache.kafka.streams.processor.To)1 InMemoryKeyValueStore (org.apache.kafka.streams.state.internals.InMemoryKeyValueStore)1 MockProcessorNode (org.apache.kafka.test.MockProcessorNode)1 NoOpProcessorContext (org.apache.kafka.test.NoOpProcessorContext)1