Search in sources :

Example 1 with Merger

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

the class KGroupedStreamImplTest method shouldAggregateSessionWindows.

@Test
public void shouldAggregateSessionWindows() throws Exception {
    final Map<Windowed<String>, Integer> results = new HashMap<>();
    groupedStream.aggregate(new Initializer<Integer>() {

        @Override
        public Integer apply() {
            return 0;
        }
    }, new Aggregator<String, String, Integer>() {

        @Override
        public Integer apply(final String aggKey, final String value, final Integer aggregate) {
            return aggregate + 1;
        }
    }, new Merger<String, Integer>() {

        @Override
        public Integer apply(final String aggKey, final Integer aggOne, final Integer aggTwo) {
            return aggOne + aggTwo;
        }
    }, SessionWindows.with(30), Serdes.Integer(), "session-store").foreach(new ForeachAction<Windowed<String>, Integer>() {

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

Aggregations

HashMap (java.util.HashMap)1 Aggregator (org.apache.kafka.streams.kstream.Aggregator)1 Initializer (org.apache.kafka.streams.kstream.Initializer)1 Merger (org.apache.kafka.streams.kstream.Merger)1 Windowed (org.apache.kafka.streams.kstream.Windowed)1 KStreamTestDriver (org.apache.kafka.test.KStreamTestDriver)1 MockAggregator (org.apache.kafka.test.MockAggregator)1 MockInitializer (org.apache.kafka.test.MockInitializer)1 Test (org.junit.Test)1