Search in sources :

Example 26 with Predicate

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

the class KTableFilterTest method testQueryableSkipNullOnMaterialization.

@Test
public void testQueryableSkipNullOnMaterialization() {
    // Do not explicitly set enableSendingOldValues. Let a further downstream stateful operator trigger it instead.
    StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    final Consumed<String, String> consumed = Consumed.with(stringSerde, stringSerde);
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, consumed);
    KTableImpl<String, String, String> table2 = (KTableImpl<String, String, String>) table1.filter(new Predicate<String, String>() {

        @Override
        public boolean test(String key, String value) {
            return value.equalsIgnoreCase("accept");
        }
    }, "anyStoreNameFilter").groupBy(MockMapper.<String, String>noOpKeyValueMapper()).reduce(MockReducer.STRING_ADDER, MockReducer.STRING_REMOVER, "mock-result");
    doTestSkipNullOnMaterialization(builder, table1, table2, topic1);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Predicate(org.apache.kafka.streams.kstream.Predicate) Test(org.junit.Test)

Example 27 with Predicate

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

the class KTableFilterTest method testQueryableNotSendingOldValue.

@Test
public void testQueryableNotSendingOldValue() {
    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");
    doTestNotSendingOldValue(builder, table1, table2, topic1);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Predicate(org.apache.kafka.streams.kstream.Predicate) Test(org.junit.Test)

Example 28 with Predicate

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

the class KTableFilterTest method testSkipNullOnMaterialization.

@Test
public void testSkipNullOnMaterialization() {
    // Do not explicitly set enableSendingOldValues. Let a further downstream stateful operator trigger it instead.
    StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    final Consumed<String, String> consumed = Consumed.with(stringSerde, stringSerde);
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, consumed);
    KTableImpl<String, String, String> table2 = (KTableImpl<String, String, String>) table1.filter(new Predicate<String, String>() {

        @Override
        public boolean test(String key, String value) {
            return value.equalsIgnoreCase("accept");
        }
    }).groupBy(MockMapper.<String, String>noOpKeyValueMapper()).reduce(MockReducer.STRING_ADDER, MockReducer.STRING_REMOVER, "mock-result");
    doTestSkipNullOnMaterialization(builder, table1, table2, topic1);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Predicate(org.apache.kafka.streams.kstream.Predicate) Test(org.junit.Test)

Example 29 with Predicate

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

the class KTableImplTest method testStateStore.

@Test
public void testStateStore() {
    String topic1 = "topic1";
    String topic2 = "topic2";
    final StreamsBuilder builder = new StreamsBuilder();
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, consumed);
    KTableImpl<String, String, String> table2 = (KTableImpl<String, String, String>) builder.table(topic2, consumed);
    KTableImpl<String, String, Integer> table1Mapped = (KTableImpl<String, String, Integer>) table1.mapValues(new ValueMapper<String, Integer>() {

        @Override
        public Integer apply(String value) {
            return new Integer(value);
        }
    });
    KTableImpl<String, Integer, Integer> table1MappedFiltered = (KTableImpl<String, Integer, Integer>) table1Mapped.filter(new Predicate<String, Integer>() {

        @Override
        public boolean test(String key, Integer value) {
            return (value % 2) == 0;
        }
    });
    table2.join(table1MappedFiltered, new ValueJoiner<String, Integer, String>() {

        @Override
        public String apply(String v1, Integer v2) {
            return v1 + v2;
        }
    });
    driver.setUp(builder, stateDir, null, null);
    driver.setTime(0L);
    // two state store should be created
    assertEquals(2, driver.allStateStores().size());
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) ValueMapper(org.apache.kafka.streams.kstream.ValueMapper) Predicate(org.apache.kafka.streams.kstream.Predicate) Test(org.junit.Test)

Example 30 with Predicate

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

the class KTableMapValuesTest method testValueGetter.

@Test
public void testValueGetter() {
    StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    String topic2 = "topic2";
    String storeName2 = "storeName2";
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, consumed);
    KTableImpl<String, String, Integer> table2 = (KTableImpl<String, String, Integer>) table1.mapValues(new ValueMapper<String, Integer>() {

        @Override
        public Integer apply(String value) {
            return new Integer(value);
        }
    });
    KTableImpl<String, Integer, Integer> table3 = (KTableImpl<String, Integer, Integer>) table2.filter(new Predicate<String, Integer>() {

        @Override
        public boolean test(String key, Integer value) {
            return (value % 2) == 0;
        }
    });
    KTableImpl<String, String, String> table4 = (KTableImpl<String, String, String>) table1.through(stringSerde, stringSerde, topic2, storeName2);
    doTestValueGetter(builder, topic1, table1, table2, table3, table4);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) ValueMapper(org.apache.kafka.streams.kstream.ValueMapper) 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