use of org.apache.kafka.streams.kstream.Windowed in project kafka by apache.
the class KGroupedStreamImplTest method shouldCountWindowed.
@Test
public void shouldCountWindowed() throws Exception {
final List<KeyValue<Windowed<String>, Long>> results = new ArrayList<>();
groupedStream.count(TimeWindows.of(500L), "aggregate-by-key-windowed").foreach(new ForeachAction<Windowed<String>, Long>() {
@Override
public void apply(final Windowed<String> key, final Long value) {
results.add(KeyValue.pair(key, value));
}
});
driver = new KStreamTestDriver(builder, TestUtils.tempDirectory(), 0);
driver.setTime(0);
driver.process(TOPIC, "1", "A");
driver.process(TOPIC, "2", "B");
driver.process(TOPIC, "3", "C");
driver.setTime(500);
driver.process(TOPIC, "1", "A");
driver.process(TOPIC, "1", "A");
driver.process(TOPIC, "2", "B");
driver.process(TOPIC, "2", "B");
assertThat(results, equalTo(Arrays.asList(KeyValue.pair(new Windowed<>("1", new TimeWindow(0, 500)), 1L), KeyValue.pair(new Windowed<>("2", new TimeWindow(0, 500)), 1L), KeyValue.pair(new Windowed<>("3", new TimeWindow(0, 500)), 1L), KeyValue.pair(new Windowed<>("1", new TimeWindow(500, 1000)), 1L), KeyValue.pair(new Windowed<>("1", new TimeWindow(500, 1000)), 2L), KeyValue.pair(new Windowed<>("2", new TimeWindow(500, 1000)), 1L), KeyValue.pair(new Windowed<>("2", new TimeWindow(500, 1000)), 2L))));
}
use of org.apache.kafka.streams.kstream.Windowed in project kafka by apache.
the class RocksDBSegmentedBytesStoreTest method shouldPutAndFetch.
@Test
public void shouldPutAndFetch() throws Exception {
final String key = "a";
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(10, 10L))), serializeValue(10L));
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(500L, 1000L))), serializeValue(50L));
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(1500L, 2000L))), serializeValue(100L));
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(2500L, 3000L))), serializeValue(200L));
final List<KeyValue<Windowed<String>, Long>> expected = Arrays.asList(KeyValue.pair(new Windowed<>(key, new SessionWindow(10, 10)), 10L), KeyValue.pair(new Windowed<>(key, new SessionWindow(500, 1000)), 50L));
final KeyValueIterator<Bytes, byte[]> values = bytesStore.fetch(Bytes.wrap(key.getBytes()), 0, 1000L);
assertEquals(expected, toList(values));
}
use of org.apache.kafka.streams.kstream.Windowed in project kafka by apache.
the class RocksDBSegmentedBytesStoreTest method shouldRollSegments.
@Test
public void shouldRollSegments() throws Exception {
// just to validate directories
final Segments segments = new Segments(storeName, retention, numSegments);
final String key = "a";
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(0L, 0L))), serializeValue(50L));
assertEquals(Collections.singleton(segments.segmentName(0)), segmentDirs());
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(30000L, 60000L))), serializeValue(100L));
assertEquals(Utils.mkSet(segments.segmentName(0), segments.segmentName(1)), segmentDirs());
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(61000L, 120000L))), serializeValue(200L));
assertEquals(Utils.mkSet(segments.segmentName(0), segments.segmentName(1), segments.segmentName(2)), segmentDirs());
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(121000L, 180000L))), serializeValue(300L));
assertEquals(Utils.mkSet(segments.segmentName(1), segments.segmentName(2), segments.segmentName(3)), segmentDirs());
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(181000L, 240000L))), serializeValue(400L));
assertEquals(Utils.mkSet(segments.segmentName(2), segments.segmentName(3), segments.segmentName(4)), segmentDirs());
final List<KeyValue<Windowed<String>, Long>> results = toList(bytesStore.fetch(Bytes.wrap(key.getBytes()), 0, 240000));
assertEquals(Arrays.asList(KeyValue.pair(new Windowed<>(key, new SessionWindow(61000L, 120000L)), 200L), KeyValue.pair(new Windowed<>(key, new SessionWindow(121000L, 180000L)), 300L), KeyValue.pair(new Windowed<>(key, new SessionWindow(181000L, 240000L)), 400L)), results);
}
use of org.apache.kafka.streams.kstream.Windowed in project kafka by apache.
the class RocksDBSessionStoreTest method shouldFetchAllSessionsWithSameRecordKey.
@Test
public void shouldFetchAllSessionsWithSameRecordKey() throws Exception {
final List<KeyValue<Windowed<String>, Long>> expected = Arrays.asList(KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L), KeyValue.pair(new Windowed<>("a", new SessionWindow(10, 10)), 2L), KeyValue.pair(new Windowed<>("a", new SessionWindow(100, 100)), 3L), KeyValue.pair(new Windowed<>("a", new SessionWindow(1000, 1000)), 4L));
for (KeyValue<Windowed<String>, Long> kv : expected) {
sessionStore.put(kv.key, kv.value);
}
// add one that shouldn't appear in the results
sessionStore.put(new Windowed<>("aa", new SessionWindow(0, 0)), 5L);
final List<KeyValue<Windowed<String>, Long>> results = toList(sessionStore.fetch("a"));
assertEquals(expected, results);
}
use of org.apache.kafka.streams.kstream.Windowed in project kafka by apache.
the class RocksDBSessionStoreTest method shouldFindValuesWithinMergingSessionWindowRange.
@Test
public void shouldFindValuesWithinMergingSessionWindowRange() throws Exception {
final String key = "a";
sessionStore.put(new Windowed<>(key, new SessionWindow(0L, 0L)), 1L);
sessionStore.put(new Windowed<>(key, new SessionWindow(1000L, 1000L)), 2L);
final KeyValueIterator<Windowed<String>, Long> results = sessionStore.findSessions(key, -1, 1000L);
final List<KeyValue<Windowed<String>, Long>> expected = Arrays.asList(KeyValue.pair(new Windowed<>(key, new SessionWindow(0L, 0L)), 1L), KeyValue.pair(new Windowed<>(key, new SessionWindow(1000L, 1000L)), 2L));
assertEquals(expected, toList(results));
}
Aggregations