use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KTableMapValuesTest method testSendingOldValue.
@Test
public void testSendingOldValue() {
StreamsBuilder builder = new StreamsBuilder();
String topic1 = "topic1";
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);
}
});
table2.enableSendingOldValues();
MockProcessorSupplier<String, Integer> proc = new MockProcessorSupplier<>();
builder.build().addProcessor("proc", proc, table2.name);
driver.setUp(builder, stateDir);
assertTrue(table1.sendingOldValueEnabled());
assertTrue(table2.sendingOldValueEnabled());
driver.process(topic1, "A", "01");
driver.process(topic1, "B", "01");
driver.process(topic1, "C", "01");
driver.flushState();
proc.checkAndClearProcessResult("A:(1<-null)", "B:(1<-null)", "C:(1<-null)");
driver.process(topic1, "A", "02");
driver.process(topic1, "B", "02");
driver.flushState();
proc.checkAndClearProcessResult("A:(2<-1)", "B:(2<-1)");
driver.process(topic1, "A", "03");
driver.flushState();
proc.checkAndClearProcessResult("A:(3<-2)");
driver.process(topic1, "A", null);
driver.flushState();
proc.checkAndClearProcessResult("A:(null<-3)");
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KTableSourceTest method testKTable.
@Test
public void testKTable() {
final StreamsBuilder builder = new StreamsBuilder();
String topic1 = "topic1";
KTable<String, Integer> table1 = builder.table(topic1, Consumed.with(stringSerde, intSerde));
MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
table1.toStream().process(proc1);
driver.setUp(builder, stateDir);
driver.process(topic1, "A", 1);
driver.process(topic1, "B", 2);
driver.process(topic1, "C", 3);
driver.process(topic1, "D", 4);
driver.flushState();
driver.process(topic1, "A", null);
driver.process(topic1, "B", null);
driver.flushState();
assertEquals(Utils.mkList("A:1", "B:2", "C:3", "D:4", "A:null", "B:null"), proc1.processed);
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KTableSourceTest method testSendingOldValue.
@Test
public void testSendingOldValue() {
final StreamsBuilder builder = new StreamsBuilder();
String topic1 = "topic1";
KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, stringConsumed);
table1.enableSendingOldValues();
assertTrue(table1.sendingOldValueEnabled());
MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
builder.build().addProcessor("proc1", proc1, table1.name);
driver.setUp(builder, stateDir);
driver.process(topic1, "A", "01");
driver.process(topic1, "B", "01");
driver.process(topic1, "C", "01");
driver.flushState();
proc1.checkAndClearProcessResult("A:(01<-null)", "B:(01<-null)", "C:(01<-null)");
driver.process(topic1, "A", "02");
driver.process(topic1, "B", "02");
driver.flushState();
proc1.checkAndClearProcessResult("A:(02<-01)", "B:(02<-01)");
driver.process(topic1, "A", "03");
driver.flushState();
proc1.checkAndClearProcessResult("A:(03<-02)");
driver.process(topic1, "A", null);
driver.process(topic1, "B", null);
driver.flushState();
proc1.checkAndClearProcessResult("A:(null<-03)", "B:(null<-02)");
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KStreamFilterTest method testFilter.
@Test
public void testFilter() {
StreamsBuilder builder = new StreamsBuilder();
final int[] expectedKeys = new int[] { 1, 2, 3, 4, 5, 6, 7 };
KStream<Integer, String> stream;
MockProcessorSupplier<Integer, String> processor;
processor = new MockProcessorSupplier<>();
stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
stream.filter(isMultipleOfThree).process(processor);
driver.setUp(builder);
for (int expectedKey : expectedKeys) {
driver.process(topicName, expectedKey, "V" + expectedKey);
}
assertEquals(2, processor.processed.size());
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KStreamFlatMapTest method testFlatMap.
@Test
public void testFlatMap() {
StreamsBuilder builder = new StreamsBuilder();
KeyValueMapper<Number, Object, Iterable<KeyValue<String, String>>> mapper = new KeyValueMapper<Number, Object, Iterable<KeyValue<String, String>>>() {
@Override
public Iterable<KeyValue<String, String>> apply(Number key, Object value) {
ArrayList<KeyValue<String, String>> result = new ArrayList<>();
for (int i = 0; i < key.intValue(); i++) {
result.add(KeyValue.pair(Integer.toString(key.intValue() * 10 + i), value.toString()));
}
return result;
}
};
final int[] expectedKeys = { 0, 1, 2, 3 };
KStream<Integer, String> stream;
MockProcessorSupplier<String, String> processor;
processor = new MockProcessorSupplier<>();
stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
stream.flatMap(mapper).process(processor);
driver.setUp(builder);
for (int expectedKey : expectedKeys) {
driver.process(topicName, expectedKey, "V" + expectedKey);
}
assertEquals(6, processor.processed.size());
String[] expected = { "10:V1", "20:V2", "21:V2", "30:V3", "31:V3", "32:V3" };
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], processor.processed.get(i));
}
}
Aggregations