use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KTableFilterTest method testValueGetter.
@Test
public void testValueGetter() {
StreamsBuilder builder = new StreamsBuilder();
String topic1 = "topic1";
KTableImpl<String, Integer, Integer> table1 = (KTableImpl<String, Integer, Integer>) builder.table(topic1, consumed);
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;
}
});
KTableImpl<String, Integer, Integer> table3 = (KTableImpl<String, Integer, Integer>) table1.filterNot(new Predicate<String, Integer>() {
@Override
public boolean test(String key, Integer value) {
return (value % 2) == 0;
}
});
doTestValueGetter(builder, table2, table3, topic1);
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KTableFilterTest method testNotSendingOldValue.
@Test
public void testNotSendingOldValue() {
StreamsBuilder builder = new StreamsBuilder();
String topic1 = "topic1";
KTableImpl<String, Integer, Integer> table1 = (KTableImpl<String, Integer, Integer>) builder.table(topic1, consumed);
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;
}
});
doTestNotSendingOldValue(builder, table1, table2, topic1);
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KTableFilterTest method testQueryableValueGetter.
@Test
public void testQueryableValueGetter() {
StreamsBuilder builder = new StreamsBuilder();
String topic1 = "topic1";
KTableImpl<String, Integer, Integer> table1 = (KTableImpl<String, Integer, Integer>) builder.table(topic1, consumed);
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;
}
}, "anyStoreNameFilter");
KTableImpl<String, Integer, Integer> table3 = (KTableImpl<String, Integer, Integer>) table1.filterNot(new Predicate<String, Integer>() {
@Override
public boolean test(String key, Integer value) {
return (value % 2) == 0;
}
});
doTestValueGetter(builder, table2, table3, topic1);
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KTableForeachTest method testForeach.
@Test
public void testForeach() {
// Given
List<KeyValue<Integer, String>> inputRecords = Arrays.asList(new KeyValue<>(0, "zero"), new KeyValue<>(1, "one"), new KeyValue<>(2, "two"), new KeyValue<>(3, "three"));
List<KeyValue<Integer, String>> expectedRecords = Arrays.asList(new KeyValue<>(0, "ZERO"), new KeyValue<>(2, "ONE"), new KeyValue<>(4, "TWO"), new KeyValue<>(6, "THREE"));
final List<KeyValue<Integer, String>> actualRecords = new ArrayList<>();
ForeachAction<Integer, String> action = new ForeachAction<Integer, String>() {
@Override
public void apply(Integer key, String value) {
actualRecords.add(new KeyValue<>(key * 2, value.toUpperCase(Locale.ROOT)));
}
};
// When
StreamsBuilder builder = new StreamsBuilder();
KTable<Integer, String> table = builder.table(topicName, Consumed.with(intSerde, stringSerde), Materialized.<Integer, String, KeyValueStore<Bytes, byte[]>>as(topicName).withKeySerde(intSerde).withValueSerde(stringSerde));
table.foreach(action);
// Then
driver.setUp(builder, stateDir);
for (KeyValue<Integer, String> record : inputRecords) {
driver.process(topicName, record.key, record.value);
}
driver.flushState();
assertEquals(expectedRecords.size(), actualRecords.size());
for (int i = 0; i < expectedRecords.size(); i++) {
KeyValue<Integer, String> expectedRecord = expectedRecords.get(i);
KeyValue<Integer, String> actualRecord = actualRecords.get(i);
assertEquals(expectedRecord, actualRecord);
}
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KTableImplTest method testValueGetter.
@Test
public void testValueGetter() {
final StreamsBuilder builder = new StreamsBuilder();
String topic1 = "topic1";
String topic2 = "topic2";
String storeName2 = "storeName2";
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);
}
});
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.setUp(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"));
}
Aggregations