use of org.apache.kafka.streams.kstream.internals.SessionWindow in project ksql by confluentinc.
the class KsqlMaterializationTest method shouldMaintainResultOrdering.
@Test
public void shouldMaintainResultOrdering() {
// Given:
final MaterializedWindowedTable table = materialization.windowed();
givenNoopFilter();
when(project.apply(any(), any(), any())).thenReturn(Optional.of(transformed));
final Instant now = Instant.now();
final TimeWindow window1 = new TimeWindow(now.plusMillis(10).toEpochMilli(), now.plusMillis(11).toEpochMilli());
final SessionWindow window2 = new SessionWindow(now.toEpochMilli(), now.plusMillis(2).toEpochMilli());
final TimeWindow window3 = new TimeWindow(now.toEpochMilli(), now.plusMillis(3).toEpochMilli());
final ImmutableList<WindowedRow> rows = ImmutableList.of(WindowedRow.of(schema, new Windowed<>(aKey, window1), aValue, aRowtime), WindowedRow.of(schema, new Windowed<>(aKey, window2), aValue, aRowtime), WindowedRow.of(schema, new Windowed<>(aKey, window3), aValue, aRowtime));
when(innerWindowed.get(any(), anyInt(), any(), any(), any())).thenReturn(KsMaterializedQueryResult.rowIteratorWithPosition(rows.iterator(), position));
// When:
final Iterator<WindowedRow> result = table.get(aKey, partition, windowStartBounds, windowEndBounds);
// Then:
assertThat(result.hasNext(), is(true));
final List<WindowedRow> resultList = Lists.newArrayList(result);
assertThat(resultList, hasSize(rows.size()));
assertThat(resultList.get(0).windowedKey().window(), is(window1));
assertThat(resultList.get(1).windowedKey().window(), is(window2));
assertThat(resultList.get(2).windowedKey().window(), is(window3));
}
use of org.apache.kafka.streams.kstream.internals.SessionWindow in project ksql by confluentinc.
the class WindowingIntTest method shouldAggregateSessionWindow.
@Test
public void shouldAggregateSessionWindow() {
// Given:
givenTable("CREATE TABLE %s AS " + "SELECT ORDERID, COUNT(*), SUM(ORDERUNITS) " + "FROM " + ORDERS_STREAM + " WINDOW SESSION (10 SECONDS) " + "GROUP BY ORDERID;");
final long sessionEnd = batch0SentMs + batch1Delay;
final Map<Windowed<String>, GenericRow> expected = ImmutableMap.<Windowed<String>, GenericRow>builder().put(new Windowed<>("ORDER_1", new SessionWindow(batch0SentMs, sessionEnd)), genericRow(2L, 20.0)).put(new Windowed<>("ORDER_2", new SessionWindow(batch0SentMs, sessionEnd)), genericRow(2L, 40.0)).put(new Windowed<>("ORDER_3", new SessionWindow(batch0SentMs, sessionEnd)), genericRow(2L, 60.0)).put(new Windowed<>("ORDER_4", new SessionWindow(batch0SentMs, sessionEnd)), genericRow(2L, 80.0)).put(new Windowed<>("ORDER_5", new SessionWindow(batch0SentMs, sessionEnd)), genericRow(2L, 100.0)).put(new Windowed<>("ORDER_6", new SessionWindow(batch0SentMs, sessionEnd)), genericRow(6L, 420.0)).build();
// Then:
assertOutputOf(resultStream0, expected, mapHasItems(expected));
assertTopicsCleanedUp(TopicCleanupPolicy.COMPACT_DELETE, 2, resultStream0);
}
use of org.apache.kafka.streams.kstream.internals.SessionWindow 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.internals.SessionWindow 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.internals.SessionWindow 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);
}
Aggregations