Search in sources :

Example 11 with KStreamBuilder

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

the class KStreamBranchTest method testKStreamBranch.

@SuppressWarnings("unchecked")
@Test
public void testKStreamBranch() {
    KStreamBuilder builder = new KStreamBuilder();
    builder.setApplicationId("X");
    Predicate<Integer, String> isEven = new Predicate<Integer, String>() {

        @Override
        public boolean test(Integer key, String value) {
            return (key % 2) == 0;
        }
    };
    Predicate<Integer, String> isMultipleOfThree = new Predicate<Integer, String>() {

        @Override
        public boolean test(Integer key, String value) {
            return (key % 3) == 0;
        }
    };
    Predicate<Integer, String> isOdd = new Predicate<Integer, String>() {

        @Override
        public boolean test(Integer key, String value) {
            return (key % 2) != 0;
        }
    };
    final int[] expectedKeys = new int[] { 1, 2, 3, 4, 5, 6 };
    KStream<Integer, String> stream;
    KStream<Integer, String>[] branches;
    MockProcessorSupplier<Integer, String>[] processors;
    stream = builder.stream(Serdes.Integer(), Serdes.String(), topicName);
    branches = stream.branch(isEven, isMultipleOfThree, isOdd);
    assertEquals(3, branches.length);
    processors = (MockProcessorSupplier<Integer, String>[]) Array.newInstance(MockProcessorSupplier.class, branches.length);
    for (int i = 0; i < branches.length; i++) {
        processors[i] = new MockProcessorSupplier<>();
        branches[i].process(processors[i]);
    }
    driver = new KStreamTestDriver(builder);
    for (int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, "V" + expectedKey);
    }
    assertEquals(3, processors[0].processed.size());
    assertEquals(1, processors[1].processed.size());
    assertEquals(2, processors[2].processed.size());
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KStream(org.apache.kafka.streams.kstream.KStream) Predicate(org.apache.kafka.streams.kstream.Predicate) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 12 with KStreamBuilder

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

the class KStreamAggregationDedupIntegrationTest method before.

@Before
public void before() throws InterruptedException {
    testNo++;
    builder = new KStreamBuilder();
    createTopics();
    streamsConfiguration = new Properties();
    String applicationId = "kgrouped-stream-test-" + testNo;
    streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
    streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers());
    streamsConfiguration.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getPath());
    streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, COMMIT_INTERVAL_MS);
    streamsConfiguration.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 10 * 1024 * 1024L);
    KeyValueMapper<Integer, String, String> mapper = MockKeyValueMapper.<Integer, String>SelectValueMapper();
    stream = builder.stream(Serdes.Integer(), Serdes.String(), streamOneInput);
    groupedStream = stream.groupBy(mapper, Serdes.String(), Serdes.String());
    reducer = new Reducer<String>() {

        @Override
        public String apply(String value1, String value2) {
            return value1 + ":" + value2;
        }
    };
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) Properties(java.util.Properties) Before(org.junit.Before)

Example 13 with KStreamBuilder

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

the class KStreamAggregationIntegrationTest method before.

@Before
public void before() throws InterruptedException {
    testNo++;
    builder = new KStreamBuilder();
    createTopics();
    streamsConfiguration = new Properties();
    final String applicationId = "kgrouped-stream-test-" + testNo;
    streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
    streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers());
    streamsConfiguration.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getPath());
    streamsConfiguration.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);
    final KeyValueMapper<Integer, String, String> mapper = MockKeyValueMapper.SelectValueMapper();
    stream = builder.stream(Serdes.Integer(), Serdes.String(), streamOneInput);
    groupedStream = stream.groupBy(mapper, Serdes.String(), Serdes.String());
    reducer = new Reducer<String>() {

        @Override
        public String apply(final String value1, final String value2) {
            return value1 + ":" + value2;
        }
    };
    initializer = new Initializer<Integer>() {

        @Override
        public Integer apply() {
            return 0;
        }
    };
    aggregator = new Aggregator<String, String, Integer>() {

        @Override
        public Integer apply(final String aggKey, final String value, final Integer aggregate) {
            return aggregate + value.length();
        }
    };
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) Properties(java.util.Properties) Before(org.junit.Before)

Example 14 with KStreamBuilder

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

the class KafkaStreamsTest method createKafkaStreams.

private KafkaStreams createKafkaStreams() {
    final Properties props = new Properties();
    props.setProperty(StreamsConfig.APPLICATION_ID_CONFIG, "appId");
    props.setProperty(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers());
    final KStreamBuilder builder = new KStreamBuilder();
    return new KafkaStreams(builder, props);
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) Properties(java.util.Properties)

Example 15 with KStreamBuilder

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

the class KStreamRepartitionJoinTest method before.

@Before
public void before() throws InterruptedException {
    testNo++;
    String applicationId = "kstream-repartition-join-test-" + testNo;
    builder = new KStreamBuilder();
    createTopics();
    streamsConfiguration = new Properties();
    streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
    streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers());
    streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, COMMIT_INTERVAL_MS);
    streamsConfiguration.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getPath());
    streamsConfiguration.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, 3);
    streamsConfiguration.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, cacheSizeBytes);
    streamOne = builder.stream(Serdes.Long(), Serdes.Integer(), streamOneInput);
    streamTwo = builder.stream(Serdes.Integer(), Serdes.String(), streamTwoInput);
    streamFour = builder.stream(Serdes.Integer(), Serdes.String(), streamFourInput);
    keyMapper = MockKeyValueMapper.SelectValueKeyValueMapper();
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) Properties(java.util.Properties) Before(org.junit.Before)

Aggregations

KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)122 Test (org.junit.Test)95 KStreamTestDriver (org.apache.kafka.test.KStreamTestDriver)60 Properties (java.util.Properties)31 MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)25 KafkaStreams (org.apache.kafka.streams.KafkaStreams)23 HashSet (java.util.HashSet)21 Set (java.util.Set)19 KeyValue (org.apache.kafka.streams.KeyValue)19 HashMap (java.util.HashMap)14 Metrics (org.apache.kafka.common.metrics.Metrics)13 StreamsConfig (org.apache.kafka.streams.StreamsConfig)13 KeyValueMapper (org.apache.kafka.streams.kstream.KeyValueMapper)13 ValueMapper (org.apache.kafka.streams.kstream.ValueMapper)13 TopicPartition (org.apache.kafka.common.TopicPartition)11 Predicate (org.apache.kafka.streams.kstream.Predicate)10 TaskId (org.apache.kafka.streams.processor.TaskId)9 MockKeyValueMapper (org.apache.kafka.test.MockKeyValueMapper)9 ArrayList (java.util.ArrayList)8 Before (org.junit.Before)8