use of org.apache.kafka.streams.KeyValueTimestamp in project kafka by apache.
the class KStreamMapTest method testMap.
@Test
public void testMap() {
final StreamsBuilder builder = new StreamsBuilder();
final String topicName = "topic";
final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
final MockApiProcessorSupplier<String, Integer, Void, Void> supplier = new MockApiProcessorSupplier<>();
final KStream<Integer, String> stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
stream.map((key, value) -> KeyValue.pair(value, key)).process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
for (final int expectedKey : expectedKeys) {
final TestInputTopic<Integer, String> inputTopic = driver.createInputTopic(topicName, new IntegerSerializer(), new StringSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
inputTopic.pipeInput(expectedKey, "V" + expectedKey, 10L - expectedKey);
}
}
final KeyValueTimestamp[] expected = new KeyValueTimestamp[] { new KeyValueTimestamp<>("V0", 0, 10), new KeyValueTimestamp<>("V1", 1, 9), new KeyValueTimestamp<>("V2", 2, 8), new KeyValueTimestamp<>("V3", 3, 7) };
assertEquals(4, supplier.theCapturedProcessor().processed().size());
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], supplier.theCapturedProcessor().processed().get(i));
}
}
use of org.apache.kafka.streams.KeyValueTimestamp in project kafka by apache.
the class KStreamMapValuesTest method testFlatMapValues.
@Test
public void testFlatMapValues() {
final StreamsBuilder builder = new StreamsBuilder();
final int[] expectedKeys = { 1, 10, 100, 1000 };
final KStream<Integer, String> stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
stream.mapValues(CharSequence::length).process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
for (final int expectedKey : expectedKeys) {
final TestInputTopic<Integer, String> inputTopic = driver.createInputTopic(topicName, new IntegerSerializer(), new StringSerializer());
inputTopic.pipeInput(expectedKey, Integer.toString(expectedKey), expectedKey / 2L);
}
}
final KeyValueTimestamp[] expected = { new KeyValueTimestamp<>(1, 1, 0), new KeyValueTimestamp<>(10, 2, 5), new KeyValueTimestamp<>(100, 3, 50), new KeyValueTimestamp<>(1000, 4, 500) };
assertArrayEquals(expected, supplier.theCapturedProcessor().processed().toArray());
}
use of org.apache.kafka.streams.KeyValueTimestamp in project kafka by apache.
the class KStreamMapValuesTest method testMapValuesWithKeys.
@Test
public void testMapValuesWithKeys() {
final StreamsBuilder builder = new StreamsBuilder();
final ValueMapperWithKey<Integer, CharSequence, Integer> mapper = (readOnlyKey, value) -> value.length() + readOnlyKey;
final int[] expectedKeys = { 1, 10, 100, 1000 };
final KStream<Integer, String> stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
stream.mapValues(mapper).process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<Integer, String> inputTopic = driver.createInputTopic(topicName, new IntegerSerializer(), new StringSerializer());
for (final int expectedKey : expectedKeys) {
inputTopic.pipeInput(expectedKey, Integer.toString(expectedKey), expectedKey / 2L);
}
}
final KeyValueTimestamp[] expected = { new KeyValueTimestamp<>(1, 2, 0), new KeyValueTimestamp<>(10, 12, 5), new KeyValueTimestamp<>(100, 103, 50), new KeyValueTimestamp<>(1000, 1004, 500) };
assertArrayEquals(expected, supplier.theCapturedProcessor().processed().toArray());
}
use of org.apache.kafka.streams.KeyValueTimestamp in project kafka by apache.
the class KGroupedStreamImplTest method doCountSlidingWindows.
private void doCountSlidingWindows(final MockApiProcessorSupplier<Windowed<String>, Long, Void, Void> supplier) {
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
inputTopic.pipeInput("1", "A", 500L);
inputTopic.pipeInput("1", "A", 999L);
inputTopic.pipeInput("1", "A", 600L);
inputTopic.pipeInput("2", "B", 500L);
inputTopic.pipeInput("2", "B", 600L);
inputTopic.pipeInput("2", "B", 700L);
inputTopic.pipeInput("3", "C", 501L);
inputTopic.pipeInput("1", "A", 1000L);
inputTopic.pipeInput("1", "A", 1000L);
inputTopic.pipeInput("2", "B", 1000L);
inputTopic.pipeInput("2", "B", 1000L);
inputTopic.pipeInput("3", "C", 600L);
}
final Comparator<KeyValueTimestamp<Windowed<String>, Long>> comparator = Comparator.comparing((KeyValueTimestamp<Windowed<String>, Long> o) -> o.key().key()).thenComparing((KeyValueTimestamp<Windowed<String>, Long> o) -> o.key().window().start());
final ArrayList<KeyValueTimestamp<Windowed<String>, Long>> actual = supplier.theCapturedProcessor().processed();
actual.sort(comparator);
assertThat(actual, equalTo(Arrays.asList(// processing A@500
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(0L, 500L)), 1L, 500L), // processing A@600
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(100L, 600L)), 2L, 600L), // processing A@999
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(499L, 999L)), 2L, 999L), // processing A@600
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(499L, 999L)), 3L, 999L), // processing first A@1000
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(500L, 1000L)), 4L, 1000L), // processing second A@1000
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(500L, 1000L)), 5L, 1000L), // processing A@999
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(501L, 1001L)), 1L, 999L), // processing A@600
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(501L, 1001L)), 2L, 999L), // processing first A@1000
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(501L, 1001L)), 3L, 1000L), // processing second A@1000
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(501L, 1001L)), 4L, 1000L), // processing A@600
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(601L, 1101L)), 1L, 999L), // processing first A@1000
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(601L, 1101L)), 2L, 1000L), // processing second A@1000
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(601L, 1101L)), 3L, 1000L), // processing first A@1000
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(1000L, 1500L)), 1L, 1000L), // processing second A@1000
new KeyValueTimestamp<>(new Windowed<>("1", new TimeWindow(1000L, 1500L)), 2L, 1000L), // processing B@500
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(0L, 500L)), 1L, 500L), // processing B@600
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(100L, 600L)), 2L, 600L), // processing B@700
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(200L, 700L)), 3L, 700L), // processing first B@1000
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(500L, 1000L)), 4L, 1000L), // processing second B@1000
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(500L, 1000L)), 5L, 1000L), // processing B@600
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(501L, 1001L)), 1L, 600L), // processing B@700
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(501L, 1001L)), 2L, 700L), // processing first B@1000
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(501L, 1001L)), 3L, 1000L), // processing second B@1000
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(501L, 1001L)), 4L, 1000L), // processing B@700
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(601L, 1101L)), 1L, 700L), // processing first B@1000
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(601L, 1101)), 2L, 1000L), // processing second B@1000
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(601L, 1101)), 3L, 1000L), // processing first B@1000
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(701L, 1201L)), 1L, 1000L), // processing second B@1000
new KeyValueTimestamp<>(new Windowed<>("2", new TimeWindow(701L, 1201L)), 2L, 1000L), // processing C@501
new KeyValueTimestamp<>(new Windowed<>("3", new TimeWindow(1L, 501L)), 1L, 501L), // processing C@600
new KeyValueTimestamp<>(new Windowed<>("3", new TimeWindow(100L, 600L)), 2L, 600L), // processing C@600
new KeyValueTimestamp<>(new Windowed<>("3", new TimeWindow(502L, 1002L)), 1L, 600L))));
}
use of org.apache.kafka.streams.KeyValueTimestamp in project kafka by apache.
the class KStreamImplTest method shouldMergeTwoStreams.
@Test
public void shouldMergeTwoStreams() {
final String topic1 = "topic-1";
final String topic2 = "topic-2";
final KStream<String, String> source1 = builder.stream(topic1);
final KStream<String, String> source2 = builder.stream(topic2);
final KStream<String, String> merged = source1.merge(source2);
merged.process(processorSupplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> inputTopic1 = driver.createInputTopic(topic1, new StringSerializer(), new StringSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
final TestInputTopic<String, String> inputTopic2 = driver.createInputTopic(topic2, new StringSerializer(), new StringSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
inputTopic1.pipeInput("A", "aa");
inputTopic2.pipeInput("B", "bb");
inputTopic2.pipeInput("C", "cc");
inputTopic1.pipeInput("D", "dd");
}
assertEquals(asList(new KeyValueTimestamp<>("A", "aa", 0), new KeyValueTimestamp<>("B", "bb", 0), new KeyValueTimestamp<>("C", "cc", 0), new KeyValueTimestamp<>("D", "dd", 0)), processorSupplier.theCapturedProcessor().processed());
}
Aggregations