Search in sources :

Example 31 with Windowed

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))));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 32 with Windowed

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));
}
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 33 with Windowed

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);
}
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 34 with Windowed

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);
}
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 35 with Windowed

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));
}
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

Windowed (org.apache.kafka.streams.kstream.Windowed)39 Test (org.junit.Test)32 KeyValue (org.apache.kafka.streams.KeyValue)18 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)18 HashMap (java.util.HashMap)6 KeyValueMapper (org.apache.kafka.streams.kstream.KeyValueMapper)6 KStreamTestDriver (org.apache.kafka.test.KStreamTestDriver)6 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)5 Bytes (org.apache.kafka.common.utils.Bytes)5 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)5 MockKeyValueMapper (org.apache.kafka.test.MockKeyValueMapper)5 Properties (java.util.Properties)4 Comparator (java.util.Comparator)3 File (java.io.File)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 LongDeserializer (org.apache.kafka.common.serialization.LongDeserializer)2 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)2 KafkaStreams (org.apache.kafka.streams.KafkaStreams)2 Reducer (org.apache.kafka.streams.kstream.Reducer)2 ValueJoiner (org.apache.kafka.streams.kstream.ValueJoiner)2