use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.
the class KStreamSplitTest method testResultingMap.
@Test
public void testResultingMap() {
final Map<String, KStream<Integer, String>> branches = source.split(Named.as("foo-")).branch(isEven, Branched.as("bar")).branch(isMultipleOfThree, Branched.withConsumer(ks -> {
})).branch(isMultipleOfFive, Branched.withFunction(ks -> null)).branch(isNegative, Branched.withFunction(ks -> ks)).branch(isMultipleOfSeven).defaultBranch();
assertEquals(4, branches.size());
// direct the branched streams into different topics named with branch name
for (final Map.Entry<String, KStream<Integer, String>> branch : branches.entrySet()) {
branch.getValue().to(branch.getKey());
}
builder.build();
withDriver(driver -> {
final TestOutputTopic<Integer, String> even = driver.createOutputTopic("foo-bar", new IntegerDeserializer(), new StringDeserializer());
final TestOutputTopic<Integer, String> negative = driver.createOutputTopic("foo-4", new IntegerDeserializer(), new StringDeserializer());
final TestOutputTopic<Integer, String> x7 = driver.createOutputTopic("foo-5", new IntegerDeserializer(), new StringDeserializer());
final TestOutputTopic<Integer, String> defaultBranch = driver.createOutputTopic("foo-0", new IntegerDeserializer(), new StringDeserializer());
assertEquals(Arrays.asList("V0", "V2", "V4", "V6"), even.readValuesToList());
assertEquals(Arrays.asList("V-1"), negative.readValuesToList());
assertEquals(Arrays.asList("V7"), x7.readValuesToList());
assertEquals(Arrays.asList("V1"), defaultBranch.readValuesToList());
});
}
use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.
the class SessionWindowedCogroupedKStreamImplTest method sessionWindowMixAggregatorsTest.
@Test
public void sessionWindowMixAggregatorsTest() {
final KTable<Windowed<String>, String> customers = windowedCogroupedStream.aggregate(MockInitializer.STRING_INIT, sessionMerger, Materialized.with(Serdes.String(), Serdes.String()));
customers.toStream().to(OUTPUT);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> testInputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
final TestInputTopic<String, String> testInputTopic2 = driver.createInputTopic(TOPIC2, new StringSerializer(), new StringSerializer());
final TestOutputTopic<Windowed<String>, String> testOutputTopic = driver.createOutputTopic(OUTPUT, new SessionWindowedDeserializer<>(new StringDeserializer()), new StringDeserializer());
testInputTopic.pipeInput("k1", "A", 0);
testInputTopic.pipeInput("k2", "A", 0);
testInputTopic.pipeInput("k2", "A", 1);
testInputTopic.pipeInput("k1", "A", 2);
testInputTopic2.pipeInput("k1", "B", 3);
testInputTopic2.pipeInput("k2", "B", 3);
testInputTopic2.pipeInput("k2", "B", 444);
testInputTopic2.pipeInput("k1", "B", 444);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A", 0);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A", 0);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", null, 0);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+0+A+A", 1);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", null, 0);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+0+A+A", 2);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", null, 2);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+0+0+A+A-B", 3);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", null, 1);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+0+0+A+A-B", 3);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0-B", 444);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0-B", 444);
}
}
use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.
the class SessionWindowedCogroupedKStreamImplTest method sessionWindowAggregate2Test.
@Test
public void sessionWindowAggregate2Test() {
final KTable<Windowed<String>, String> customers = groupedStream.cogroup(MockAggregator.TOSTRING_ADDER).windowedBy(SessionWindows.with(ofMillis(500))).aggregate(MockInitializer.STRING_INIT, sessionMerger, Materialized.with(Serdes.String(), Serdes.String()));
customers.toStream().to(OUTPUT);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> testInputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
final TestOutputTopic<Windowed<String>, String> testOutputTopic = driver.createOutputTopic(OUTPUT, new SessionWindowedDeserializer<>(new StringDeserializer()), new StringDeserializer());
testInputTopic.pipeInput("k1", "A", 0);
testInputTopic.pipeInput("k1", "A", 0);
testInputTopic.pipeInput("k2", "B", 599);
testInputTopic.pipeInput("k1", "B", 607);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A", 0);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+0+A+A", 0);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+B", 599);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+B", 607);
}
}
use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.
the class SlidingWindowedCogroupedKStreamImplTest method slidingWindowAggregateStreamsTest.
@Test
public void slidingWindowAggregateStreamsTest() {
final KTable<Windowed<String>, String> customers = windowedCogroupedStream.aggregate(MockInitializer.STRING_INIT, Materialized.with(Serdes.String(), Serdes.String()));
customers.toStream().to(OUTPUT);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> testInputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
final TestOutputTopic<Windowed<String>, String> testOutputTopic = driver.createOutputTopic(OUTPUT, new TimeWindowedDeserializer<>(new StringDeserializer(), WINDOW_SIZE_MS), new StringDeserializer());
testInputTopic.pipeInput("k1", "A", 500);
testInputTopic.pipeInput("k2", "A", 500);
testInputTopic.pipeInput("k2", "A", 501);
testInputTopic.pipeInput("k1", "A", 502);
testInputTopic.pipeInput("k1", "B", 503);
testInputTopic.pipeInput("k2", "B", 503);
testInputTopic.pipeInput("k2", "B", 504);
testInputTopic.pipeInput("k1", "B", 504);
final List<TestRecord<Windowed<String>, String>> results = testOutputTopic.readRecordsToList();
final List<TestRecord<Windowed<String>, String>> expected = new LinkedList<>();
// k1-A-500
expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(0L, 500L)), "0+A", null, 500L));
// k2-A-500
expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(0L, 500L)), "0+A", null, 500L));
// k2-A-501
expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(501L, 1001L)), "0+A", null, 501L));
expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(1L, 501L)), "0+A+A", null, 501L));
// k1-A-502
expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(501L, 1001L)), "0+A", null, 502L));
expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(2L, 502L)), "0+A+A", null, 502L));
// k1-B-503
expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(501L, 1001L)), "0+A+B", null, 503L));
expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(503L, 1003L)), "0+B", null, 503L));
expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(3L, 503L)), "0+A+A+B", null, 503L));
// k2-B-503
expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(501L, 1001L)), "0+A+B", null, 503L));
expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(502L, 1002)), "0+B", null, 503L));
expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(3L, 503L)), "0+A+A+B", null, 503L));
// k2-B-504
expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(502L, 1002L)), "0+B+B", null, 504L));
expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(501L, 1001L)), "0+A+B+B", null, 504L));
expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(504L, 1004L)), "0+B", null, 504L));
expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(4L, 504L)), "0+A+A+B+B", null, 504L));
// k1-B-504
expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(503L, 1003L)), "0+B+B", null, 504L));
expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(501L, 1001L)), "0+A+B+B", null, 504L));
expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(504L, 1004L)), "0+B", null, 504L));
expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(4L, 504L)), "0+A+A+B+B", null, 504L));
assertEquals(expected, results);
}
}
use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.
the class TimeWindowedCogroupedKStreamImplTest method timeWindowAggregateOverlappingWindowsTest.
@Test
public void timeWindowAggregateOverlappingWindowsTest() {
final KTable<Windowed<String>, String> customers = groupedStream.cogroup(MockAggregator.TOSTRING_ADDER).windowedBy(TimeWindows.of(ofMillis(500L)).advanceBy(ofMillis(200L))).aggregate(MockInitializer.STRING_INIT, Materialized.with(Serdes.String(), Serdes.String()));
customers.toStream().to(OUTPUT);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> testInputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
final TestOutputTopic<Windowed<String>, String> testOutputTopic = driver.createOutputTopic(OUTPUT, new TimeWindowedDeserializer<>(new StringDeserializer(), WINDOW_SIZE), new StringDeserializer());
testInputTopic.pipeInput("k1", "A", 0);
testInputTopic.pipeInput("k2", "A", 0);
testInputTopic.pipeInput("k1", "B", 250);
testInputTopic.pipeInput("k2", "B", 250);
testInputTopic.pipeInput("k2", "A", 500L);
testInputTopic.pipeInput("k1", "A", 500L);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A", 0);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A", 0);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A+B", 250);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+B", 250);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A+B", 250);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+B", 250);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+B+A", 500);
assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A", 500);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+B+A", 500);
assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A", 500);
}
}
Aggregations