Search in sources :

Example 21 with Predicate

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

the class KStreamBranchTest method testKStreamBranch.

@SuppressWarnings("unchecked")
@Test
public void testKStreamBranch() {
    final StreamsBuilder builder = new StreamsBuilder();
    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(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
    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.setUp(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 : KStream(org.apache.kafka.streams.kstream.KStream) Predicate(org.apache.kafka.streams.kstream.Predicate) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 22 with Predicate

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

the class KStreamImpl method createRepartitionedSource.

static <K1, V1> String createRepartitionedSource(final InternalStreamsBuilder builder, final Serde<K1> keySerde, final Serde<V1> valSerde, final String topicNamePrefix, final String name) {
    Serializer<K1> keySerializer = keySerde != null ? keySerde.serializer() : null;
    Serializer<V1> valSerializer = valSerde != null ? valSerde.serializer() : null;
    Deserializer<K1> keyDeserializer = keySerde != null ? keySerde.deserializer() : null;
    Deserializer<V1> valDeserializer = valSerde != null ? valSerde.deserializer() : null;
    String baseName = topicNamePrefix != null ? topicNamePrefix : name;
    String repartitionTopic = baseName + REPARTITION_TOPIC_SUFFIX;
    String sinkName = builder.newProcessorName(SINK_NAME);
    String filterName = builder.newProcessorName(FILTER_NAME);
    String sourceName = builder.newProcessorName(SOURCE_NAME);
    builder.internalTopologyBuilder.addInternalTopic(repartitionTopic);
    builder.internalTopologyBuilder.addProcessor(filterName, new KStreamFilter<>(new Predicate<K1, V1>() {

        @Override
        public boolean test(final K1 key, final V1 value) {
            return key != null;
        }
    }, false), name);
    builder.internalTopologyBuilder.addSink(sinkName, repartitionTopic, keySerializer, valSerializer, null, filterName);
    builder.internalTopologyBuilder.addSource(null, sourceName, new FailOnInvalidTimestamp(), keyDeserializer, valDeserializer, repartitionTopic);
    return sourceName;
}
Also used : FailOnInvalidTimestamp(org.apache.kafka.streams.processor.FailOnInvalidTimestamp) Predicate(org.apache.kafka.streams.kstream.Predicate)

Example 23 with Predicate

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

the class StreamsMetadataStateTest method shouldGetAllStreamsInstancesWithNoStores.

@Test
public void shouldGetAllStreamsInstancesWithNoStores() {
    builder.stream("topic-five").filter(new Predicate<Object, Object>() {

        @Override
        public boolean test(final Object key, final Object value) {
            return true;
        }
    }).to("some-other-topic");
    final TopicPartition tp5 = new TopicPartition("topic-five", 1);
    final HostInfo hostFour = new HostInfo("host-four", 8080);
    hostToPartitions.put(hostFour, Utils.mkSet(tp5));
    discovery.onChange(hostToPartitions, cluster.withPartitions(Collections.singletonMap(tp5, new PartitionInfo("topic-five", 1, null, null, null))));
    final StreamsMetadata expected = new StreamsMetadata(hostFour, Collections.singleton(globalTable), Collections.singleton(tp5));
    final Collection<StreamsMetadata> actual = discovery.getAllMetadata();
    assertTrue("expected " + actual + " to contain " + expected, actual.contains(expected));
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) StreamsMetadata(org.apache.kafka.streams.state.StreamsMetadata) PartitionInfo(org.apache.kafka.common.PartitionInfo) HostInfo(org.apache.kafka.streams.state.HostInfo) Predicate(org.apache.kafka.streams.kstream.Predicate) StreamsBuilderTest(org.apache.kafka.streams.StreamsBuilderTest) Test(org.junit.Test)

Example 24 with Predicate

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

the class KTableFilterTest method testSendingOldValue.

@Test
public void testSendingOldValue() {
    StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    KTableImpl<String, Integer, Integer> table1 = (KTableImpl<String, Integer, Integer>) builder.table(topic1, consumed);
    KTableImpl<String, Integer, Integer> table2 = (KTableImpl<String, Integer, Integer>) table1.filter(new Predicate<String, Integer>() {

        @Override
        public boolean test(String key, Integer value) {
            return (value % 2) == 0;
        }
    });
    doTestSendingOldValue(builder, table1, table2, topic1);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Predicate(org.apache.kafka.streams.kstream.Predicate) Test(org.junit.Test)

Example 25 with Predicate

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

the class KTableFilterTest method testQueryableSendingOldValue.

@Test
public void testQueryableSendingOldValue() {
    StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    KTableImpl<String, Integer, Integer> table1 = (KTableImpl<String, Integer, Integer>) builder.table(topic1, consumed);
    KTableImpl<String, Integer, Integer> table2 = (KTableImpl<String, Integer, Integer>) table1.filter(new Predicate<String, Integer>() {

        @Override
        public boolean test(String key, Integer value) {
            return (value % 2) == 0;
        }
    }, "anyStoreNameFilter");
    doTestSendingOldValue(builder, table1, table2, topic1);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Predicate(org.apache.kafka.streams.kstream.Predicate) Test(org.junit.Test)

Aggregations

Predicate (org.apache.kafka.streams.kstream.Predicate)32 Test (org.junit.Test)27 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)22 ValueMapper (org.apache.kafka.streams.kstream.ValueMapper)8 Properties (java.util.Properties)7 KafkaStreams (org.apache.kafka.streams.KafkaStreams)7 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)7 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)6 KStreamTestDriver (org.apache.kafka.test.KStreamTestDriver)6 Bytes (org.apache.kafka.common.utils.Bytes)5 KeyValue (org.apache.kafka.streams.KeyValue)5 KStream (org.apache.kafka.streams.kstream.KStream)5 MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)5 HashSet (java.util.HashSet)4 List (java.util.List)4 Serdes (org.apache.kafka.common.serialization.Serdes)4 KafkaStreamsTest (org.apache.kafka.streams.KafkaStreamsTest)4 Consumed (org.apache.kafka.streams.kstream.Consumed)4 IntegrationTest (org.apache.kafka.test.IntegrationTest)4 Assert.assertEquals (org.junit.Assert.assertEquals)4