use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KStreamFlatMapValuesTest method testFlatMapValuesWithKeys.
@Test
public void testFlatMapValuesWithKeys() {
StreamsBuilder builder = new StreamsBuilder();
ValueMapperWithKey<Integer, Number, Iterable<String>> mapper = new ValueMapperWithKey<Integer, Number, Iterable<String>>() {
@Override
public Iterable<String> apply(final Integer readOnlyKey, final Number value) {
ArrayList<String> result = new ArrayList<>();
result.add("v" + value);
result.add("k" + readOnlyKey);
return result;
}
};
final int[] expectedKeys = { 0, 1, 2, 3 };
final KStream<Integer, Integer> stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.Integer()));
final MockProcessorSupplier<Integer, String> processor = new MockProcessorSupplier<>();
stream.flatMapValues(mapper).process(processor);
driver.setUp(builder);
for (final int expectedKey : expectedKeys) {
driver.process(topicName, expectedKey, expectedKey);
}
String[] expected = { "0:v0", "0:k0", "1:v1", "1:k1", "2:v2", "2:k2", "3:v3", "3:k3" };
assertArrayEquals(expected, processor.processed.toArray());
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KStreamFlatMapValuesTest method testFlatMapValues.
@Test
public void testFlatMapValues() {
StreamsBuilder builder = new StreamsBuilder();
ValueMapper<Number, Iterable<String>> mapper = new ValueMapper<Number, Iterable<String>>() {
@Override
public Iterable<String> apply(Number value) {
ArrayList<String> result = new ArrayList<String>();
result.add("v" + value);
result.add("V" + value);
return result;
}
};
final int[] expectedKeys = { 0, 1, 2, 3 };
final KStream<Integer, Integer> stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.Integer()));
final MockProcessorSupplier<Integer, String> processor = new MockProcessorSupplier<>();
stream.flatMapValues(mapper).process(processor);
driver.setUp(builder);
for (final int expectedKey : expectedKeys) {
driver.process(topicName, expectedKey, expectedKey);
}
String[] expected = { "0:v0", "0:V0", "1:v1", "1:V1", "2:v2", "2:V2", "3:v3", "3:V3" };
assertArrayEquals(expected, processor.processed.toArray());
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KStreamForeachTest 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();
KStream<Integer, String> stream = builder.stream(topicName, Consumed.with(intSerde, stringSerde));
stream.foreach(action);
// Then
driver.setUp(builder);
for (KeyValue<Integer, String> record : inputRecords) {
driver.process(topicName, record.key, record.value);
}
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 KStreamGlobalKTableJoinTest method setUp.
@Before
public void setUp() throws IOException {
stateDir = TestUtils.tempDirectory("kafka-test");
builder = new StreamsBuilder();
final KStream<Integer, String> stream;
// value of stream optionally contains key of table
final GlobalKTable<String, String> table;
final KeyValueMapper<Integer, String, String> keyMapper;
processor = new MockProcessorSupplier<>();
final Consumed<Integer, String> streamConsumed = Consumed.with(intSerde, stringSerde);
final Consumed<String, String> tableConsumed = Consumed.with(stringSerde, stringSerde);
stream = builder.stream(streamTopic, streamConsumed);
table = builder.globalTable(globalTableTopic, tableConsumed);
keyMapper = new KeyValueMapper<Integer, String, String>() {
@Override
public String apply(final Integer key, final String value) {
final String[] tokens = value.split(",");
// If not present, use null to indicate no match
return tokens.length > 1 ? tokens[1] : null;
}
};
stream.join(table, keyMapper, MockValueJoiner.TOSTRING_JOINER).process(processor);
driver.setUp(builder, stateDir);
driver.setTime(0L);
}
use of org.apache.kafka.streams.StreamsBuilder in project apache-kafka-on-k8s by banzaicloud.
the class KStreamImplTest method shouldSendDataThroughTopicUsingProduced.
@Test
public void shouldSendDataThroughTopicUsingProduced() {
final StreamsBuilder builder = new StreamsBuilder();
final String input = "topic";
final KStream<String, String> stream = builder.stream(input, consumed);
final MockProcessorSupplier<String, String> processorSupplier = new MockProcessorSupplier<>();
stream.through("through-topic", Produced.with(stringSerde, stringSerde)).process(processorSupplier);
driver.setUp(builder);
driver.process(input, "a", "b");
assertThat(processorSupplier.processed, equalTo(Collections.singletonList("a:b")));
}
Aggregations