use of org.apache.kafka.streams.kstream.KStreamBuilder in project kafka by apache.
the class KStreamTransformTest method testTransform.
@Test
public void testTransform() {
KStreamBuilder builder = new KStreamBuilder();
TransformerSupplier<Number, Number, KeyValue<Integer, Integer>> transformerSupplier = new TransformerSupplier<Number, Number, KeyValue<Integer, Integer>>() {
public Transformer<Number, Number, KeyValue<Integer, Integer>> get() {
return new Transformer<Number, Number, KeyValue<Integer, Integer>>() {
private int total = 0;
@Override
public void init(ProcessorContext context) {
}
@Override
public KeyValue<Integer, Integer> transform(Number key, Number value) {
total += value.intValue();
return KeyValue.pair(key.intValue() * 2, total);
}
@Override
public KeyValue<Integer, Integer> punctuate(long timestamp) {
return KeyValue.pair(-1, (int) timestamp);
}
@Override
public void close() {
}
};
}
};
final int[] expectedKeys = { 1, 10, 100, 1000 };
MockProcessorSupplier<Integer, Integer> processor = new MockProcessorSupplier<>();
KStream<Integer, Integer> stream = builder.stream(intSerde, intSerde, topicName);
stream.transform(transformerSupplier).process(processor);
driver = new KStreamTestDriver(builder);
for (int expectedKey : expectedKeys) {
driver.process(topicName, expectedKey, expectedKey * 10);
}
driver.punctuate(2);
driver.punctuate(3);
assertEquals(6, processor.processed.size());
String[] expected = { "2:10", "20:110", "200:1110", "2000:11110", "-1:2", "-1:3" };
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], processor.processed.get(i));
}
}
use of org.apache.kafka.streams.kstream.KStreamBuilder in project kafka by apache.
the class KTableKTableOuterJoinTest method testSendingOldValue.
@Test
public void testSendingOldValue() throws Exception {
final KStreamBuilder builder = new KStreamBuilder();
final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
KTable<Integer, String> table1;
KTable<Integer, String> table2;
KTable<Integer, String> joined;
MockProcessorSupplier<Integer, String> proc;
table1 = builder.table(intSerde, stringSerde, topic1, storeName1);
table2 = builder.table(intSerde, stringSerde, topic2, storeName2);
joined = table1.outerJoin(table2, MockValueJoiner.TOSTRING_JOINER);
((KTableImpl<?, ?, ?>) joined).enableSendingOldValues();
proc = new MockProcessorSupplier<>();
builder.addProcessor("proc", proc, ((KTableImpl<?, ?, ?>) joined).name);
driver = new KStreamTestDriver(builder, stateDir);
assertTrue(((KTableImpl<?, ?, ?>) table1).sendingOldValueEnabled());
assertTrue(((KTableImpl<?, ?, ?>) table2).sendingOldValueEnabled());
assertTrue(((KTableImpl<?, ?, ?>) joined).sendingOldValueEnabled());
for (int i = 0; i < 2; i++) {
driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+null<-null)", "1:(X1+null<-null)");
for (int i = 0; i < 2; i++) {
driver.process(topic2, expectedKeys[i], "Y" + expectedKeys[i]);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+Y0<-X0+null)", "1:(X1+Y1<-X1+null)");
for (int expectedKey : expectedKeys) {
driver.process(topic1, expectedKey, "X" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+Y0<-X0+Y0)", "1:(X1+Y1<-X1+Y1)", "2:(X2+null<-null)", "3:(X3+null<-null)");
// push all items to the other stream. this should produce four items.
for (int expectedKey : expectedKeys) {
driver.process(topic2, expectedKey, "YY" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+YY0<-X0+Y0)", "1:(X1+YY1<-X1+Y1)", "2:(X2+YY2<-X2+null)", "3:(X3+YY3<-X3+null)");
for (int expectedKey : expectedKeys) {
driver.process(topic1, expectedKey, "X" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+YY0<-X0+YY0)", "1:(X1+YY1<-X1+YY1)", "2:(X2+YY2<-X2+YY2)", "3:(X3+YY3<-X3+YY3)");
for (int i = 0; i < 2; i++) {
driver.process(topic2, expectedKeys[i], null);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+null<-X0+YY0)", "1:(X1+null<-X1+YY1)");
for (int expectedKey : expectedKeys) {
driver.process(topic1, expectedKey, "XX" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(XX0+null<-X0+null)", "1:(XX1+null<-X1+null)", "2:(XX2+YY2<-X2+YY2)", "3:(XX3+YY3<-X3+YY3)");
for (int i = 1; i < 3; i++) {
driver.process(topic1, expectedKeys[i], null);
}
driver.flushState();
proc.checkAndClearProcessResult("1:(null<-XX1+null)", "2:(null+YY2<-XX2+YY2)");
}
use of org.apache.kafka.streams.kstream.KStreamBuilder in project kafka by apache.
the class KTableKTableOuterJoinTest method testNotSendingOldValue.
@Test
public void testNotSendingOldValue() throws Exception {
final KStreamBuilder builder = new KStreamBuilder();
final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
KTable<Integer, String> table1;
KTable<Integer, String> table2;
KTable<Integer, String> joined;
MockProcessorSupplier<Integer, String> proc;
table1 = builder.table(intSerde, stringSerde, topic1, storeName1);
table2 = builder.table(intSerde, stringSerde, topic2, storeName2);
joined = table1.outerJoin(table2, MockValueJoiner.TOSTRING_JOINER);
proc = new MockProcessorSupplier<>();
builder.addProcessor("proc", proc, ((KTableImpl<?, ?, ?>) joined).name);
driver = new KStreamTestDriver(builder, stateDir);
assertTrue(((KTableImpl<?, ?, ?>) table1).sendingOldValueEnabled());
assertTrue(((KTableImpl<?, ?, ?>) table2).sendingOldValueEnabled());
assertFalse(((KTableImpl<?, ?, ?>) joined).sendingOldValueEnabled());
for (int i = 0; i < 2; i++) {
driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+null<-null)", "1:(X1+null<-null)");
for (int i = 0; i < 2; i++) {
driver.process(topic2, expectedKeys[i], "Y" + expectedKeys[i]);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+Y0<-null)", "1:(X1+Y1<-null)");
for (int expectedKey : expectedKeys) {
driver.process(topic1, expectedKey, "X" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+Y0<-null)", "1:(X1+Y1<-null)", "2:(X2+null<-null)", "3:(X3+null<-null)");
// push all items to the other stream. this should produce four items.
for (int expectedKey : expectedKeys) {
driver.process(topic2, expectedKey, "YY" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+YY0<-null)", "1:(X1+YY1<-null)", "2:(X2+YY2<-null)", "3:(X3+YY3<-null)");
for (int expectedKey : expectedKeys) {
driver.process(topic1, expectedKey, "X" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+YY0<-null)", "1:(X1+YY1<-null)", "2:(X2+YY2<-null)", "3:(X3+YY3<-null)");
for (int i = 0; i < 2; i++) {
driver.process(topic2, expectedKeys[i], null);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+null<-null)", "1:(X1+null<-null)");
for (int expectedKey : expectedKeys) {
driver.process(topic1, expectedKey, "XX" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(XX0+null<-null)", "1:(XX1+null<-null)", "2:(XX2+YY2<-null)", "3:(XX3+YY3<-null)");
for (int i = 1; i < 3; i++) {
driver.process(topic1, expectedKeys[i], null);
}
driver.flushState();
proc.checkAndClearProcessResult("1:(null<-null)", "2:(null+YY2<-null)");
}
use of org.apache.kafka.streams.kstream.KStreamBuilder in project kafka by apache.
the class KTableMapValuesTest method testNotSendingOldValue.
@Test
public void testNotSendingOldValue() throws IOException {
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, Integer> table2 = (KTableImpl<String, String, Integer>) table1.mapValues(new ValueMapper<String, Integer>() {
@Override
public Integer apply(String value) {
return new Integer(value);
}
});
MockProcessorSupplier<String, Integer> proc = new MockProcessorSupplier<>();
builder.addProcessor("proc", proc, table2.name);
driver = new KStreamTestDriver(builder, stateDir, null, null);
assertFalse(table1.sendingOldValueEnabled());
assertFalse(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<-null)", "B:(2<-null)");
driver.process(topic1, "A", "03");
driver.flushState();
proc.checkAndClearProcessResult("A:(3<-null)");
driver.process(topic1, "A", null);
driver.flushState();
proc.checkAndClearProcessResult("A:(null<-null)");
}
use of org.apache.kafka.streams.kstream.KStreamBuilder 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"));
}
Aggregations