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();
}
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)");
}
use of org.apache.kafka.streams.kstream.Predicate in project kafka by apache.
the class StreamsMetadataStateTest method shouldGetAllStreamsInstancesWithNoStores.
@Test
public void shouldGetAllStreamsInstancesWithNoStores() throws Exception {
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));
}
use of org.apache.kafka.streams.kstream.Predicate in project kafka by apache.
the class KStreamImplTest method testNumProcesses.
@Test
public void testNumProcesses() {
final KStreamBuilder builder = new KStreamBuilder();
KStream<String, String> source1 = builder.stream(stringSerde, stringSerde, "topic-1", "topic-2");
KStream<String, String> source2 = builder.stream(stringSerde, stringSerde, "topic-3", "topic-4");
KStream<String, String> stream1 = source1.filter(new Predicate<String, String>() {
@Override
public boolean test(String key, String value) {
return true;
}
}).filterNot(new Predicate<String, String>() {
@Override
public boolean test(String key, String value) {
return false;
}
});
KStream<String, Integer> stream2 = stream1.mapValues(new ValueMapper<String, Integer>() {
@Override
public Integer apply(String value) {
return new Integer(value);
}
});
KStream<String, Integer> stream3 = source2.flatMapValues(new ValueMapper<String, Iterable<Integer>>() {
@Override
public Iterable<Integer> apply(String value) {
return Collections.singletonList(new Integer(value));
}
});
KStream<String, Integer>[] streams2 = stream2.branch(new Predicate<String, Integer>() {
@Override
public boolean test(String key, Integer value) {
return (value % 2) == 0;
}
}, new Predicate<String, Integer>() {
@Override
public boolean test(String key, Integer value) {
return true;
}
});
KStream<String, Integer>[] streams3 = stream3.branch(new Predicate<String, Integer>() {
@Override
public boolean test(String key, Integer value) {
return (value % 2) == 0;
}
}, new Predicate<String, Integer>() {
@Override
public boolean test(String key, Integer value) {
return true;
}
});
final int anyWindowSize = 1;
KStream<String, Integer> stream4 = streams2[0].join(streams3[0], new ValueJoiner<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer value1, Integer value2) {
return value1 + value2;
}
}, JoinWindows.of(anyWindowSize), stringSerde, intSerde, intSerde);
KStream<String, Integer> stream5 = streams2[1].join(streams3[1], new ValueJoiner<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer value1, Integer value2) {
return value1 + value2;
}
}, JoinWindows.of(anyWindowSize), stringSerde, intSerde, intSerde);
stream4.to("topic-5");
streams2[1].through("topic-6").process(new MockProcessorSupplier<String, Integer>());
assertEquals(// sources
2 + // stream1
2 + // stream2
1 + // stream3
1 + 1 + // streams2
2 + 1 + // streams3
2 + // stream2-stream3 joins
5 * 2 + // to
1 + // through
2 + // process
1, builder.setApplicationId("X").build(null).processors().size());
}
use of org.apache.kafka.streams.kstream.Predicate in project kafka by apache.
the class KTableFilterTest method testSendingOldValue.
@Test
public void testSendingOldValue() 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;
}
});
table2.enableSendingOldValues();
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.checkEmptyAndClearProcessResult();
driver.process(topic1, "A", 2);
driver.process(topic1, "B", 2);
driver.flushState();
proc1.checkAndClearProcessResult("A:(2<-1)", "B:(2<-1)");
proc2.checkAndClearProcessResult("A:(2<-null)", "B:(2<-null)");
driver.process(topic1, "A", 3);
driver.flushState();
proc1.checkAndClearProcessResult("A:(3<-2)");
proc2.checkAndClearProcessResult("A:(null<-2)");
driver.process(topic1, "A", null);
driver.process(topic1, "B", null);
driver.flushState();
proc1.checkAndClearProcessResult("A:(null<-3)", "B:(null<-2)");
proc2.checkAndClearProcessResult("B:(null<-2)");
}
Aggregations