Search in sources :

Example 1 with Predicate

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

the class KTableImplTest method testValueGetter.

@Test
public void testValueGetter() throws IOException {
    final KStreamBuilder builder = new KStreamBuilder();
    String topic1 = "topic1";
    String topic2 = "topic2";
    String storeName1 = "storeName1";
    String storeName2 = "storeName2";
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(stringSerde, stringSerde, topic1, storeName1);
    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);
    KTableValueGetterSupplier<String, String> getterSupplier1 = table1.valueGetterSupplier();
    KTableValueGetterSupplier<String, Integer> getterSupplier2 = table2.valueGetterSupplier();
    KTableValueGetterSupplier<String, Integer> getterSupplier3 = table3.valueGetterSupplier();
    KTableValueGetterSupplier<String, String> getterSupplier4 = table4.valueGetterSupplier();
    driver = new KStreamTestDriver(builder, stateDir, null, null);
    // two state store should be created
    assertEquals(2, driver.allStateStores().size());
    KTableValueGetter<String, String> getter1 = getterSupplier1.get();
    getter1.init(driver.context());
    KTableValueGetter<String, Integer> getter2 = getterSupplier2.get();
    getter2.init(driver.context());
    KTableValueGetter<String, Integer> getter3 = getterSupplier3.get();
    getter3.init(driver.context());
    KTableValueGetter<String, String> getter4 = getterSupplier4.get();
    getter4.init(driver.context());
    driver.process(topic1, "A", "01");
    driver.process(topic1, "B", "01");
    driver.process(topic1, "C", "01");
    driver.flushState();
    assertEquals("01", getter1.get("A"));
    assertEquals("01", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertEquals(new Integer(1), getter2.get("A"));
    assertEquals(new Integer(1), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertNull(getter3.get("A"));
    assertNull(getter3.get("B"));
    assertNull(getter3.get("C"));
    assertEquals("01", getter4.get("A"));
    assertEquals("01", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
    driver.process(topic1, "A", "02");
    driver.process(topic1, "B", "02");
    driver.flushState();
    assertEquals("02", getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertEquals(new Integer(2), getter2.get("A"));
    assertEquals(new Integer(2), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertEquals(new Integer(2), getter3.get("A"));
    assertEquals(new Integer(2), getter3.get("B"));
    assertNull(getter3.get("C"));
    assertEquals("02", getter4.get("A"));
    assertEquals("02", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
    driver.process(topic1, "A", "03");
    driver.flushState();
    assertEquals("03", getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertEquals(new Integer(3), getter2.get("A"));
    assertEquals(new Integer(2), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertNull(getter3.get("A"));
    assertEquals(new Integer(2), getter3.get("B"));
    assertNull(getter3.get("C"));
    assertEquals("03", getter4.get("A"));
    assertEquals("02", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
    driver.process(topic1, "A", null);
    driver.flushState();
    assertNull(getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertNull(getter2.get("A"));
    assertEquals(new Integer(2), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertNull(getter3.get("A"));
    assertEquals(new Integer(2), getter3.get("B"));
    assertNull(getter3.get("C"));
    assertNull(getter4.get("A"));
    assertEquals("02", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) MockKeyValueMapper(org.apache.kafka.test.MockKeyValueMapper) ValueMapper(org.apache.kafka.streams.kstream.ValueMapper) Predicate(org.apache.kafka.streams.kstream.Predicate) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) Test(org.junit.Test)

Example 2 with Predicate

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

the class KTableMapValuesTest method testValueGetter.

@Test
public void testValueGetter() throws IOException {
    KStreamBuilder builder = new KStreamBuilder();
    String topic1 = "topic1";
    String topic2 = "topic2";
    String storeName1 = "storeName1";
    String storeName2 = "storeName2";
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(stringSerde, stringSerde, topic1, storeName1);
    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);
    KTableValueGetterSupplier<String, String> getterSupplier1 = table1.valueGetterSupplier();
    KTableValueGetterSupplier<String, Integer> getterSupplier2 = table2.valueGetterSupplier();
    KTableValueGetterSupplier<String, Integer> getterSupplier3 = table3.valueGetterSupplier();
    KTableValueGetterSupplier<String, String> getterSupplier4 = table4.valueGetterSupplier();
    driver = new KStreamTestDriver(builder, stateDir, null, null);
    KTableValueGetter<String, String> getter1 = getterSupplier1.get();
    getter1.init(driver.context());
    KTableValueGetter<String, Integer> getter2 = getterSupplier2.get();
    getter2.init(driver.context());
    KTableValueGetter<String, Integer> getter3 = getterSupplier3.get();
    getter3.init(driver.context());
    KTableValueGetter<String, String> getter4 = getterSupplier4.get();
    getter4.init(driver.context());
    driver.process(topic1, "A", "01");
    driver.process(topic1, "B", "01");
    driver.process(topic1, "C", "01");
    driver.flushState();
    assertEquals("01", getter1.get("A"));
    assertEquals("01", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertEquals(new Integer(1), getter2.get("A"));
    assertEquals(new Integer(1), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertNull(getter3.get("A"));
    assertNull(getter3.get("B"));
    assertNull(getter3.get("C"));
    assertEquals("01", getter4.get("A"));
    assertEquals("01", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
    driver.process(topic1, "A", "02");
    driver.process(topic1, "B", "02");
    driver.flushState();
    assertEquals("02", getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertEquals(new Integer(2), getter2.get("A"));
    assertEquals(new Integer(2), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertEquals(new Integer(2), getter3.get("A"));
    assertEquals(new Integer(2), getter3.get("B"));
    assertNull(getter3.get("C"));
    assertEquals("02", getter4.get("A"));
    assertEquals("02", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
    driver.process(topic1, "A", "03");
    driver.flushState();
    assertEquals("03", getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertEquals(new Integer(3), getter2.get("A"));
    assertEquals(new Integer(2), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertNull(getter3.get("A"));
    assertEquals(new Integer(2), getter3.get("B"));
    assertNull(getter3.get("C"));
    assertEquals("03", getter4.get("A"));
    assertEquals("02", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
    driver.process(topic1, "A", null);
    driver.flushState();
    assertNull(getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertNull(getter2.get("A"));
    assertEquals(new Integer(2), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertNull(getter3.get("A"));
    assertEquals(new Integer(2), getter3.get("B"));
    assertNull(getter3.get("C"));
    assertNull(getter4.get("A"));
    assertEquals("02", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) ValueMapper(org.apache.kafka.streams.kstream.ValueMapper) Predicate(org.apache.kafka.streams.kstream.Predicate) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) Test(org.junit.Test)

Example 3 with Predicate

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

the class KTableFilterTest method testSkipNullOnMaterialization.

@Test
public void testSkipNullOnMaterialization() throws IOException {
    // Do not explicitly set enableSendingOldValues. Let a further downstream stateful operator trigger it instead.
    KStreamBuilder builder = new KStreamBuilder();
    String topic1 = "topic1";
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(stringSerde, stringSerde, topic1, "anyStoreName");
    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(MockKeyValueMapper.<String, String>NoOpKeyValueMapper()).reduce(MockReducer.STRING_ADDER, MockReducer.STRING_REMOVER, "mock-result");
    MockProcessorSupplier<String, String> proc1 = new MockProcessorSupplier<>();
    MockProcessorSupplier<String, String> proc2 = new MockProcessorSupplier<>();
    builder.addProcessor("proc1", proc1, table1.name);
    builder.addProcessor("proc2", proc2, table2.name);
    driver = new KStreamTestDriver(builder, stateDir, stringSerde, stringSerde);
    driver.process(topic1, "A", "reject");
    driver.process(topic1, "B", "reject");
    driver.process(topic1, "C", "reject");
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(reject<-null)", "B:(reject<-null)", "C:(reject<-null)");
    proc2.checkEmptyAndClearProcessResult();
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Predicate(org.apache.kafka.streams.kstream.Predicate) Test(org.junit.Test)

Example 4 with Predicate

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

the class KTableFilterTest method testNotSendingOldValue.

@Test
public void testNotSendingOldValue() throws IOException {
    KStreamBuilder builder = new KStreamBuilder();
    String topic1 = "topic1";
    KTableImpl<String, Integer, Integer> table1 = (KTableImpl<String, Integer, Integer>) builder.table(stringSerde, intSerde, topic1, "anyStoreName");
    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;
        }
    });
    MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
    MockProcessorSupplier<String, Integer> proc2 = new MockProcessorSupplier<>();
    builder.addProcessor("proc1", proc1, table1.name);
    builder.addProcessor("proc2", proc2, table2.name);
    driver = new KStreamTestDriver(builder, stateDir, null, null);
    driver.process(topic1, "A", 1);
    driver.process(topic1, "B", 1);
    driver.process(topic1, "C", 1);
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(1<-null)", "B:(1<-null)", "C:(1<-null)");
    proc2.checkAndClearProcessResult("A:(null<-null)", "B:(null<-null)", "C:(null<-null)");
    driver.process(topic1, "A", 2);
    driver.process(topic1, "B", 2);
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(2<-null)", "B:(2<-null)");
    proc2.checkAndClearProcessResult("A:(2<-null)", "B:(2<-null)");
    driver.process(topic1, "A", 3);
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(3<-null)");
    proc2.checkAndClearProcessResult("A:(null<-null)");
    driver.process(topic1, "A", null);
    driver.process(topic1, "B", null);
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(null<-null)", "B:(null<-null)");
    proc2.checkAndClearProcessResult("A:(null<-null)", "B:(null<-null)");
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Predicate(org.apache.kafka.streams.kstream.Predicate) Test(org.junit.Test)

Example 5 with Predicate

use of org.apache.kafka.streams.kstream.Predicate in project ksql by confluentinc.

the class SqlPredicate method getWindowedKeyPredicate.

private Predicate getWindowedKeyPredicate() {
    final ExpressionMetadata expressionEvaluator = createExpressionMetadata();
    return (Predicate<Windowed<String>, GenericRow>) (key, row) -> {
        try {
            Kudf[] kudfs = expressionEvaluator.getUdfs();
            Object[] values = new Object[columnIndexes.length];
            for (int i = 0; i < values.length; i++) {
                if (columnIndexes[i] < 0) {
                    values[i] = kudfs[i];
                } else {
                    values[i] = genericRowValueTypeEnforcer.enforceFieldType(columnIndexes[i], row.getColumns().get(columnIndexes[i]));
                }
            }
            return (Boolean) ee.evaluate(values);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        log.error("Invalid format: " + key + " : " + row);
        return false;
    };
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) KsqlException(io.confluent.ksql.util.KsqlException) Predicate(org.apache.kafka.streams.kstream.Predicate) Kudf(io.confluent.ksql.function.udf.Kudf)

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