Search in sources :

Example 11 with AbstractProcessor

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

the class MockProcessorContextTest method shouldCaptureOutputRecords.

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

        @Override
        public void process(final String key, final Long value) {
            context().forward(key + value, key.length() + value);
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
    assertEquals(new KeyValue<>("foo5", 8L), forwarded.next().keyValue());
    assertEquals(new KeyValue<>("barbaz50", 56L), forwarded.next().keyValue());
    assertFalse(forwarded.hasNext());
    context.resetForwards();
    assertEquals(0, context.forwarded().size());
}
Also used : CapturedForward(org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 12 with AbstractProcessor

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

the class MockProcessorContextTest method shouldStoreAndReturnStateStores.

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

        @Override
        public void process(final String key, final Long value) {
            // noinspection unchecked
            final KeyValueStore<String, Long> stateStore = (KeyValueStore<String, Long>) context().getStateStore("my-state");
            stateStore.put(key, (stateStore.get(key) == null ? 0 : stateStore.get(key)) + value);
            stateStore.put("all", (stateStore.get("all") == null ? 0 : stateStore.get("all")) + value);
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    final KeyValueStore<String, Long> store = new InMemoryKeyValueStore<>("my-state", Serdes.String(), Serdes.Long());
    context.register(store, false, null);
    store.init(context, store);
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("bar", 50L);
    assertEquals(5L, (long) store.get("foo"));
    assertEquals(50L, (long) store.get("bar"));
    assertEquals(55L, (long) store.get("all"));
}
Also used : AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) Test(org.junit.Test)

Example 13 with AbstractProcessor

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

the class MockProcessorContextTest method shouldCaptureOutputRecordsUsingTo.

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

        @Override
        public void process(final String key, final Long value) {
            context().forward(key + value, key.length() + value, To.all());
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
    assertEquals(new KeyValue<>("foo5", 8L), forwarded.next().keyValue());
    assertEquals(new KeyValue<>("barbaz50", 56L), forwarded.next().keyValue());
    assertFalse(forwarded.hasNext());
    context.resetForwards();
    assertEquals(0, context.forwarded().size());
}
Also used : CapturedForward(org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 14 with AbstractProcessor

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

the class SimpleBenchmark method createKafkaStreamsWithSink.

private KafkaStreams createKafkaStreamsWithSink(String topic, final CountDownLatch latch) {
    setStreamProperties("simple-benchmark-streams-with-sink");
    StreamsBuilder builder = new StreamsBuilder();
    KStream<Integer, byte[]> source = builder.stream(topic, Consumed.with(INTEGER_SERDE, BYTE_SERDE));
    source.to(INTEGER_SERDE, BYTE_SERDE, SINK_TOPIC);
    source.process(new ProcessorSupplier<Integer, byte[]>() {

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

                @Override
                public void init(ProcessorContext context) {
                }

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

                @Override
                public void punctuate(long timestamp) {
                }

                @Override
                public void close() {
                }
            };
        }
    });
    return createKafkaStreamsWithExceptionHandler(builder, props);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Processor(org.apache.kafka.streams.processor.Processor) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext)

Example 15 with AbstractProcessor

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

the class SimpleBenchmark method createKafkaStreams.

private KafkaStreams createKafkaStreams(String topic, final CountDownLatch latch) {
    setStreamProperties("simple-benchmark-streams");
    StreamsBuilder builder = new StreamsBuilder();
    KStream<Integer, byte[]> source = builder.stream(topic, Consumed.with(INTEGER_SERDE, BYTE_SERDE));
    source.process(new ProcessorSupplier<Integer, byte[]>() {

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

                @Override
                public void init(ProcessorContext context) {
                }

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

                @Override
                public void punctuate(long timestamp) {
                }

                @Override
                public void close() {
                }
            };
        }
    });
    return createKafkaStreamsWithExceptionHandler(builder, props);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Processor(org.apache.kafka.streams.processor.Processor) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext)

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