Search in sources :

Example 1 with Reducer

use of org.apache.kafka.streams.kstream.Reducer 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 2 with Reducer

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

the class KStreamAggregationIntegrationTest method shouldReduceSessionWindows.

@Test
public void shouldReduceSessionWindows() throws Exception {
    // something to do with time
    final long sessionGap = 1000L;
    final long maintainMillis = sessionGap * 3;
    final long t1 = mockTime.milliseconds();
    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>, String> results = new HashMap<>();
    final CountDownLatch latch = new CountDownLatch(11);
    final String userSessionsStore = "UserSessionsStore";
    builder.stream(Serdes.String(), Serdes.String(), userSessionsStream).groupByKey(Serdes.String(), Serdes.String()).reduce(new Reducer<String>() {

        @Override
        public String apply(final String value1, final String value2) {
            return value1 + ":" + value2;
        }
    }, SessionWindows.with(sessionGap).until(maintainMillis), userSessionsStore).foreach(new ForeachAction<Windowed<String>, String>() {

        @Override
        public void apply(final Windowed<String> key, final String value) {
            results.put(key, value);
            latch.countDown();
        }
    });
    startStreams();
    latch.await(30, TimeUnit.SECONDS);
    final ReadOnlySessionStore<String, String> sessionStore = kafkaStreams.store(userSessionsStore, QueryableStoreTypes.<String, String>sessionStore());
    // verify correct data received
    assertThat(results.get(new Windowed<>("bob", new SessionWindow(t1, t1))), equalTo("start"));
    assertThat(results.get(new Windowed<>("penny", new SessionWindow(t1, t1))), equalTo("start"));
    assertThat(results.get(new Windowed<>("jo", new SessionWindow(t1, t1))), equalTo("pause"));
    assertThat(results.get(new Windowed<>("jo", new SessionWindow(t4, t4))), equalTo("resume"));
    assertThat(results.get(new Windowed<>("emily", new SessionWindow(t1, t2))), equalTo("pause:resume"));
    assertThat(results.get(new Windowed<>("bob", new SessionWindow(t3, t4))), equalTo("pause:resume"));
    assertThat(results.get(new Windowed<>("penny", new SessionWindow(t3, t3))), equalTo("stop"));
    // verify can query data via IQ
    final KeyValueIterator<Windowed<String>, String> bob = sessionStore.fetch("bob");
    assertThat(bob.next(), equalTo(KeyValue.pair(new Windowed<>("bob", new SessionWindow(t1, t1)), "start")));
    assertThat(bob.next(), equalTo(KeyValue.pair(new Windowed<>("bob", new SessionWindow(t3, t4)), "pause:resume")));
    assertFalse(bob.hasNext());
}
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) Reducer(org.apache.kafka.streams.kstream.Reducer) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)2 Reducer (org.apache.kafka.streams.kstream.Reducer)2 Windowed (org.apache.kafka.streams.kstream.Windowed)2 Test (org.junit.Test)2 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 KeyValue (org.apache.kafka.streams.KeyValue)1 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)1 KStreamTestDriver (org.apache.kafka.test.KStreamTestDriver)1 MockReducer (org.apache.kafka.test.MockReducer)1