Search in sources :

Example 6 with Windowed

use of org.apache.kafka.streams.kstream.Windowed in project kafka by apache.

the class KGroupedStreamImplTest method shouldReduceSessionWindows.

@Test
public void shouldReduceSessionWindows() throws Exception {
    final Map<Windowed<String>, String> results = new HashMap<>();
    groupedStream.reduce(new Reducer<String>() {

        @Override
        public String apply(final String value1, final String value2) {
            return value1 + ":" + value2;
        }
    }, SessionWindows.with(30), "session-store").foreach(new ForeachAction<Windowed<String>, String>() {

        @Override
        public void apply(final Windowed<String> key, final String value) {
            results.put(key, value);
        }
    });
    driver = new KStreamTestDriver(builder, TestUtils.tempDirectory());
    driver.setTime(10);
    driver.process(TOPIC, "1", "A");
    driver.setTime(15);
    driver.process(TOPIC, "2", "Z");
    driver.setTime(30);
    driver.process(TOPIC, "1", "B");
    driver.setTime(70);
    driver.process(TOPIC, "1", "A");
    driver.setTime(90);
    driver.process(TOPIC, "1", "B");
    driver.setTime(100);
    driver.process(TOPIC, "1", "C");
    driver.flushState();
    assertEquals("A:B", results.get(new Windowed<>("1", new SessionWindow(10, 30))));
    assertEquals("Z", results.get(new Windowed<>("2", new SessionWindow(15, 15))));
    assertEquals("A:B:C", results.get(new Windowed<>("1", new SessionWindow(70, 100))));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) HashMap(java.util.HashMap) MockReducer(org.apache.kafka.test.MockReducer) Reducer(org.apache.kafka.streams.kstream.Reducer) Test(org.junit.Test)

Example 7 with Windowed

use of org.apache.kafka.streams.kstream.Windowed in project kafka by apache.

the class KStreamAggregationDedupIntegrationTest method shouldGroupByKey.

@Test
public void shouldGroupByKey() throws Exception {
    final long timestamp = mockTime.milliseconds();
    produceMessages(timestamp);
    produceMessages(timestamp);
    stream.groupByKey(Serdes.Integer(), Serdes.String()).count(TimeWindows.of(500L), "count-windows").toStream(new KeyValueMapper<Windowed<Integer>, Long, String>() {

        @Override
        public String apply(final Windowed<Integer> windowedKey, final Long value) {
            return windowedKey.key() + "@" + windowedKey.window().start();
        }
    }).to(Serdes.String(), Serdes.Long(), outputTopic);
    startStreams();
    final List<KeyValue<String, Long>> results = receiveMessages(new StringDeserializer(), new LongDeserializer(), 5);
    Collections.sort(results, new Comparator<KeyValue<String, Long>>() {

        @Override
        public int compare(final KeyValue<String, Long> o1, final KeyValue<String, Long> o2) {
            return KStreamAggregationDedupIntegrationTest.compare(o1, o2);
        }
    });
    final long window = timestamp / 500 * 500;
    assertThat(results, is(Arrays.asList(KeyValue.pair("1@" + window, 2L), KeyValue.pair("2@" + window, 2L), KeyValue.pair("3@" + window, 2L), KeyValue.pair("4@" + window, 2L), KeyValue.pair("5@" + window, 2L))));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) MockKeyValueMapper(org.apache.kafka.test.MockKeyValueMapper) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) Windowed(org.apache.kafka.streams.kstream.Windowed) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) Test(org.junit.Test)

Example 8 with Windowed

use of org.apache.kafka.streams.kstream.Windowed in project kafka by apache.

the class KStreamAggregationIntegrationTest method shouldCountSessionWindows.

@Test
public void shouldCountSessionWindows() throws Exception {
    final long sessionGap = 5 * 60 * 1000L;
    final long maintainMillis = sessionGap * 3;
    final long t1 = mockTime.milliseconds() - TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS);
    final List<KeyValue<String, String>> t1Messages = Arrays.asList(new KeyValue<>("bob", "start"), new KeyValue<>("penny", "start"), new KeyValue<>("jo", "pause"), new KeyValue<>("emily", "pause"));
    IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(userSessionsStream, t1Messages, TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), t1);
    final long t2 = t1 + (sessionGap / 2);
    IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(userSessionsStream, Collections.singletonList(new KeyValue<>("emily", "resume")), TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), t2);
    final long t3 = t1 + sessionGap + 1;
    IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(userSessionsStream, Arrays.asList(new KeyValue<>("bob", "pause"), new KeyValue<>("penny", "stop")), TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), t3);
    final long t4 = t3 + (sessionGap / 2);
    IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(userSessionsStream, Arrays.asList(// bobs session continues
    new KeyValue<>("bob", "resume"), // jo's starts new session
    new KeyValue<>("jo", "resume")), TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), t4);
    final Map<Windowed<String>, Long> results = new HashMap<>();
    final CountDownLatch latch = new CountDownLatch(11);
    builder.stream(Serdes.String(), Serdes.String(), userSessionsStream).groupByKey(Serdes.String(), Serdes.String()).count(SessionWindows.with(sessionGap).until(maintainMillis), "UserSessionsStore").toStream().foreach(new ForeachAction<Windowed<String>, Long>() {

        @Override
        public void apply(final Windowed<String> key, final Long value) {
            results.put(key, value);
            latch.countDown();
        }
    });
    startStreams();
    latch.await(30, TimeUnit.SECONDS);
    assertThat(results.get(new Windowed<>("bob", new SessionWindow(t1, t1))), equalTo(1L));
    assertThat(results.get(new Windowed<>("penny", new SessionWindow(t1, t1))), equalTo(1L));
    assertThat(results.get(new Windowed<>("jo", new SessionWindow(t1, t1))), equalTo(1L));
    assertThat(results.get(new Windowed<>("jo", new SessionWindow(t4, t4))), equalTo(1L));
    assertThat(results.get(new Windowed<>("emily", new SessionWindow(t1, t2))), equalTo(2L));
    assertThat(results.get(new Windowed<>("bob", new SessionWindow(t3, t4))), equalTo(2L));
    assertThat(results.get(new Windowed<>("penny", new SessionWindow(t3, t3))), equalTo(1L));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) HashMap(java.util.HashMap) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) Windowed(org.apache.kafka.streams.kstream.Windowed) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Example 9 with Windowed

use of org.apache.kafka.streams.kstream.Windowed in project kafka by apache.

the class CachingSessionStore method findSessions.

public KeyValueIterator<Windowed<K>, AGG> findSessions(final K key, final long earliestSessionEndTime, final long latestSessionStartTime) {
    validateStoreOpen();
    final Bytes binarySessionId = Bytes.wrap(keySerde.serializer().serialize(this.name(), key));
    final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(cacheName, keySchema.lowerRange(binarySessionId, earliestSessionEndTime), keySchema.upperRange(binarySessionId, latestSessionStartTime));
    final KeyValueIterator<Windowed<Bytes>, byte[]> storeIterator = bytesStore.findSessions(binarySessionId, earliestSessionEndTime, latestSessionStartTime);
    final HasNextCondition hasNextCondition = keySchema.hasNextCondition(binarySessionId, earliestSessionEndTime, latestSessionStartTime);
    final PeekingKeyValueIterator<Bytes, LRUCacheEntry> filteredCacheIterator = new FilteredCacheIterator(cacheIterator, hasNextCondition);
    return new MergedSortedCacheSessionStoreIterator<>(filteredCacheIterator, storeIterator, serdes);
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes)

Example 10 with Windowed

use of org.apache.kafka.streams.kstream.Windowed in project kafka by apache.

the class SessionKeySerdeTest method shouldExtractBytesKeyFromBinary.

@Test
public void shouldExtractBytesKeyFromBinary() throws Exception {
    final Bytes bytesKey = Bytes.wrap(key.getBytes());
    final Windowed<Bytes> windowedBytesKey = new Windowed<>(bytesKey, window);
    final Bytes serialized = SessionKeySerde.bytesToBinary(windowedBytesKey);
    assertEquals(windowedBytesKey, SessionKeySerde.fromBytes(serialized));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) 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