Search in sources :

Example 66 with SessionWindow

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));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KsqlMaterializedWindowedTable(io.confluent.ksql.execution.streams.materialization.KsqlMaterialization.KsqlMaterializedWindowedTable) Instant(java.time.Instant) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Test(org.junit.Test)

Example 67 with SessionWindow

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);
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) GenericRow(io.confluent.ksql.GenericRow) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 68 with SessionWindow

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));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) KeyValue(org.apache.kafka.streams.KeyValue) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 69 with SessionWindow

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);
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 70 with SessionWindow

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);
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Aggregations

SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)130 Test (org.junit.Test)113 Windowed (org.apache.kafka.streams.kstream.Windowed)112 Bytes (org.apache.kafka.common.utils.Bytes)50 KeyValue (org.apache.kafka.streams.KeyValue)50 ArrayList (java.util.ArrayList)10 Properties (java.util.Properties)9 HashMap (java.util.HashMap)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)8 StreamsTestUtils.verifyWindowedKeyValue (org.apache.kafka.test.StreamsTestUtils.verifyWindowedKeyValue)8 LinkedList (java.util.LinkedList)6 ReadOnlySessionStoreStub (org.apache.kafka.test.ReadOnlySessionStoreStub)6 GenericRow (io.confluent.ksql.GenericRow)5 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)4 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)4 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)4 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)4 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)4 IntegrationTest (org.apache.kafka.test.IntegrationTest)4