Search in sources :

Example 11 with SessionStore

use of org.apache.kafka.streams.state.SessionStore in project kafka by apache.

the class SessionWindowedKStreamImplTest method shouldMaterializeAggregated.

@Test
public void shouldMaterializeAggregated() {
    stream.aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, sessionMerger, Materialized.<String, String, SessionStore<Bytes, byte[]>>as("aggregated").withValueSerde(Serdes.String()));
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        processData(driver);
        final SessionStore<String, String> sessionStore = driver.getSessionStore("aggregated");
        final List<KeyValue<Windowed<String>, String>> data = StreamsTestUtils.toList(sessionStore.fetch("1", "2"));
        assertThat(data, equalTo(Arrays.asList(KeyValue.pair(new Windowed<>("1", new SessionWindow(10, 15)), "0+0+1+2"), KeyValue.pair(new Windowed<>("1", new SessionWindow(600, 600)), "0+3"), KeyValue.pair(new Windowed<>("2", new SessionWindow(599, 600)), "0+0+1+2"))));
    }
}
Also used : SessionStore(org.apache.kafka.streams.state.SessionStore) Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Test(org.junit.Test)

Example 12 with SessionStore

use of org.apache.kafka.streams.state.SessionStore in project kafka by apache.

the class MetricsIntegrationTest method shouldAddMetricsForSessionStore.

@Test
public void shouldAddMetricsForSessionStore() throws Exception {
    final Duration inactivityGap = Duration.ofMillis(50);
    builder.stream(STREAM_INPUT, Consumed.with(Serdes.Integer(), Serdes.String())).groupByKey().windowedBy(SessionWindows.with(inactivityGap).grace(Duration.ZERO)).aggregate(() -> 0L, (aggKey, newValue, aggValue) -> aggValue, (aggKey, leftAggValue, rightAggValue) -> leftAggValue, Materialized.<Integer, Long, SessionStore<Bytes, byte[]>>as(SESSION_AGGREGATED_STREAM_STORE).withValueSerde(Serdes.Long()).withRetention(inactivityGap)).toStream().map((key, value) -> KeyValue.pair(value, value)).to(STREAM_OUTPUT_1, Produced.with(Serdes.Long(), Serdes.Long()));
    produceRecordsForTwoSegments(inactivityGap);
    startApplication();
    verifyStateMetric(State.RUNNING);
    checkSessionStoreMetrics();
    closeApplication();
    checkMetricsDeregistration();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) StreamsConfig(org.apache.kafka.streams.StreamsConfig) MockTime(org.apache.kafka.common.utils.MockTime) BeforeClass(org.junit.BeforeClass) Produced(org.apache.kafka.streams.kstream.Produced) SessionWindows(org.apache.kafka.streams.kstream.SessionWindows) Stores(org.apache.kafka.streams.state.Stores) IntegrationTest(org.apache.kafka.test.IntegrationTest) WindowStore(org.apache.kafka.streams.state.WindowStore) ArrayList(java.util.ArrayList) IntegrationTestUtils.safeUniqueTestName(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.safeUniqueTestName) EmbeddedKafkaCluster(org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster) TestName(org.junit.rules.TestName) After(org.junit.After) Duration(java.time.Duration) Metric(org.apache.kafka.common.Metric) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) SessionStore(org.apache.kafka.streams.state.SessionStore) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) BufferConfig(org.apache.kafka.streams.kstream.Suppressed.BufferConfig) Before(org.junit.Before) Sensor(org.apache.kafka.common.metrics.Sensor) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) TestUtils(org.apache.kafka.test.TestUtils) Consumed(org.apache.kafka.streams.kstream.Consumed) KeyValue(org.apache.kafka.streams.KeyValue) Suppressed(org.apache.kafka.streams.kstream.Suppressed) Test(org.junit.Test) IOException(java.io.IOException) State(org.apache.kafka.streams.KafkaStreams.State) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) Bytes(org.apache.kafka.common.utils.Bytes) IntegrationTestUtils(org.apache.kafka.streams.integration.utils.IntegrationTestUtils) List(java.util.List) Rule(org.junit.Rule) TimeWindows(org.apache.kafka.streams.kstream.TimeWindows) Materialized(org.apache.kafka.streams.kstream.Materialized) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Assert(org.junit.Assert) Collections(java.util.Collections) Topology(org.apache.kafka.streams.Topology) SessionStore(org.apache.kafka.streams.state.SessionStore) Duration(java.time.Duration) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Example 13 with SessionStore

use of org.apache.kafka.streams.state.SessionStore in project kafka by apache.

the class SessionStoreFetchTest method getStoreConfig.

private Materialized<String, Long, SessionStore<Bytes, byte[]>> getStoreConfig(final StoreType type, final String name, final boolean cachingEnabled, final boolean loggingEnabled) {
    final Supplier<SessionBytesStoreSupplier> createStore = () -> {
        if (type == StoreType.InMemory) {
            return Stores.inMemorySessionStore(STORE_NAME, Duration.ofMillis(RETENTION_MS));
        } else if (type == StoreType.RocksDB) {
            return Stores.persistentSessionStore(STORE_NAME, Duration.ofMillis(RETENTION_MS));
        } else {
            return Stores.inMemorySessionStore(STORE_NAME, Duration.ofMillis(RETENTION_MS));
        }
    };
    final SessionBytesStoreSupplier stateStoreSupplier = createStore.get();
    final Materialized<String, Long, SessionStore<Bytes, byte[]>> stateStoreConfig = Materialized.<String, Long>as(stateStoreSupplier).withKeySerde(Serdes.String()).withValueSerde(Serdes.Long());
    if (cachingEnabled) {
        stateStoreConfig.withCachingEnabled();
    } else {
        stateStoreConfig.withCachingDisabled();
    }
    if (loggingEnabled) {
        stateStoreConfig.withLoggingEnabled(new HashMap<String, String>());
    } else {
        stateStoreConfig.withLoggingDisabled();
    }
    return stateStoreConfig;
}
Also used : SessionStore(org.apache.kafka.streams.state.SessionStore) SessionBytesStoreSupplier(org.apache.kafka.streams.state.SessionBytesStoreSupplier)

Aggregations

SessionStore (org.apache.kafka.streams.state.SessionStore)13 KeyValue (org.apache.kafka.streams.KeyValue)6 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)6 Test (org.junit.Test)6 SessionBytesStoreSupplier (org.apache.kafka.streams.state.SessionBytesStoreSupplier)4 Topology (org.apache.kafka.streams.Topology)3 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)3 Windowed (org.apache.kafka.streams.kstream.Windowed)3 Collections (java.util.Collections)2 Properties (java.util.Properties)2 Serdes (org.apache.kafka.common.serialization.Serdes)2 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)2 Bytes (org.apache.kafka.common.utils.Bytes)2 KafkaStreams (org.apache.kafka.streams.KafkaStreams)2 StreamsConfig (org.apache.kafka.streams.StreamsConfig)2 Materialized (org.apache.kafka.streams.kstream.Materialized)2 Produced (org.apache.kafka.streams.kstream.Produced)2 SessionWindows (org.apache.kafka.streams.kstream.SessionWindows)2 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)2 PlayEvent (io.confluent.examples.streams.avro.PlayEvent)1