use of org.apache.kafka.streams.kstream.KStreamBuilder in project kafka by apache.
the class KTableMapValuesTest method testKTable.
@Test
public void testKTable() {
final KStreamBuilder builder = new KStreamBuilder();
String topic1 = "topic1";
KTable<String, String> table1 = builder.table(stringSerde, stringSerde, topic1, "anyStoreName");
KTable<String, Integer> table2 = table1.mapValues(new ValueMapper<CharSequence, Integer>() {
@Override
public Integer apply(CharSequence value) {
return value.charAt(0) - 48;
}
});
MockProcessorSupplier<String, Integer> proc2 = new MockProcessorSupplier<>();
table2.toStream().process(proc2);
driver = new KStreamTestDriver(builder, stateDir);
driver.process(topic1, "A", "1");
driver.process(topic1, "B", "2");
driver.process(topic1, "C", "3");
driver.process(topic1, "D", "4");
driver.flushState();
assertEquals(Utils.mkList("A:1", "B:2", "C:3", "D:4"), proc2.processed);
}
use of org.apache.kafka.streams.kstream.KStreamBuilder in project kafka by apache.
the class KTableSourceTest method testValueGetter.
@Test
public void testValueGetter() throws IOException {
final KStreamBuilder builder = new KStreamBuilder();
String topic1 = "topic1";
KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(stringSerde, stringSerde, topic1, "anyStoreName");
KTableValueGetterSupplier<String, String> getterSupplier1 = table1.valueGetterSupplier();
driver = new KStreamTestDriver(builder, stateDir, null, null);
KTableValueGetter<String, String> getter1 = getterSupplier1.get();
getter1.init(driver.context());
driver.process(topic1, "A", "01");
driver.process(topic1, "B", "01");
driver.process(topic1, "C", "01");
assertEquals("01", getter1.get("A"));
assertEquals("01", getter1.get("B"));
assertEquals("01", getter1.get("C"));
driver.process(topic1, "A", "02");
driver.process(topic1, "B", "02");
assertEquals("02", getter1.get("A"));
assertEquals("02", getter1.get("B"));
assertEquals("01", getter1.get("C"));
driver.process(topic1, "A", "03");
assertEquals("03", getter1.get("A"));
assertEquals("02", getter1.get("B"));
assertEquals("01", getter1.get("C"));
driver.process(topic1, "A", null);
driver.process(topic1, "B", null);
assertNull(getter1.get("A"));
assertNull(getter1.get("B"));
assertEquals("01", getter1.get("C"));
}
use of org.apache.kafka.streams.kstream.KStreamBuilder in project kafka by apache.
the class KTableSourceTest method testKTable.
@Test
public void testKTable() {
final KStreamBuilder builder = new KStreamBuilder();
String topic1 = "topic1";
KTable<String, Integer> table1 = builder.table(stringSerde, intSerde, topic1, "anyStoreName");
MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
table1.toStream().process(proc1);
driver = new KStreamTestDriver(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.kstream.KStreamBuilder in project kafka by apache.
the class KTableSourceTest method testNotSendingOldValue.
@Test
public void testNotSendingOldValue() throws IOException {
final KStreamBuilder builder = new KStreamBuilder();
String topic1 = "topic1";
KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(stringSerde, stringSerde, topic1, "anyStoreName");
MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
builder.addProcessor("proc1", proc1, table1.name);
driver = new KStreamTestDriver(builder, stateDir, null, null);
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<-null)", "B:(02<-null)");
driver.process(topic1, "A", "03");
driver.flushState();
proc1.checkAndClearProcessResult("A:(03<-null)");
driver.process(topic1, "A", null);
driver.process(topic1, "B", null);
driver.flushState();
proc1.checkAndClearProcessResult("A:(null<-null)", "B:(null<-null)");
}
use of org.apache.kafka.streams.kstream.KStreamBuilder in project kafka by apache.
the class KTableSourceTest method testSendingOldValue.
@Test
public void testSendingOldValue() throws IOException {
final KStreamBuilder builder = new KStreamBuilder();
String topic1 = "topic1";
KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(stringSerde, stringSerde, topic1, "anyStoreName");
table1.enableSendingOldValues();
assertTrue(table1.sendingOldValueEnabled());
MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
builder.addProcessor("proc1", proc1, table1.name);
driver = new KStreamTestDriver(builder, stateDir, null, null);
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)");
}
Aggregations