Search in sources :

Example 1 with SessionStore

use of org.apache.kafka.streams.state.SessionStore in project apache-kafka-on-k8s by banzaicloud.

the class SessionWindowedKStreamImpl method materialize.

private <VR> StoreBuilder<SessionStore<K, VR>> materialize(final MaterializedInternal<K, VR, SessionStore<Bytes, byte[]>> materialized) {
    SessionBytesStoreSupplier supplier = (SessionBytesStoreSupplier) materialized.storeSupplier();
    if (supplier == null) {
        supplier = Stores.persistentSessionStore(materialized.storeName(), windows.maintainMs());
    }
    final StoreBuilder<SessionStore<K, VR>> builder = Stores.sessionStoreBuilder(supplier, materialized.keySerde(), materialized.valueSerde());
    if (materialized.loggingEnabled()) {
        builder.withLoggingEnabled(materialized.logConfig());
    } else {
        builder.withLoggingDisabled();
    }
    if (materialized.cachingEnabled()) {
        builder.withCachingEnabled();
    }
    return builder;
}
Also used : SessionStore(org.apache.kafka.streams.state.SessionStore) SessionBytesStoreSupplier(org.apache.kafka.streams.state.SessionBytesStoreSupplier)

Example 2 with SessionStore

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

the class PositionRestartIntegrationTest method setUpSessionPAPITopology.

private void setUpSessionPAPITopology(final SessionBytesStoreSupplier supplier, final StreamsBuilder builder) {
    final StoreBuilder<?> sessionStoreStoreBuilder;
    final ProcessorSupplier<Integer, Integer, Void, Void> processorSupplier;
    sessionStoreStoreBuilder = Stores.sessionStoreBuilder(supplier, Serdes.Integer(), Serdes.Integer());
    processorSupplier = () -> new ContextualProcessor<Integer, Integer, Void, Void>() {

        @Override
        public void process(final Record<Integer, Integer> record) {
            final SessionStore<Integer, Integer> stateStore = context().getStateStore(sessionStoreStoreBuilder.name());
            stateStore.put(new Windowed<>(record.key(), new SessionWindow(WINDOW_START, WINDOW_START)), record.value());
        }
    };
    if (cache) {
        sessionStoreStoreBuilder.withCachingEnabled();
    } else {
        sessionStoreStoreBuilder.withCachingDisabled();
    }
    if (log) {
        sessionStoreStoreBuilder.withLoggingEnabled(Collections.emptyMap());
    } else {
        sessionStoreStoreBuilder.withLoggingDisabled();
    }
    builder.addStateStore(sessionStoreStoreBuilder);
    builder.stream(INPUT_TOPIC_NAME, Consumed.with(Serdes.Integer(), Serdes.Integer())).process(processorSupplier, sessionStoreStoreBuilder.name());
}
Also used : SessionStore(org.apache.kafka.streams.state.SessionStore) Windowed(org.apache.kafka.streams.kstream.Windowed) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow)

Example 3 with SessionStore

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

the class IQv2StoreIntegrationTest method setUpSessionPAPITopology.

private void setUpSessionPAPITopology(final SessionBytesStoreSupplier supplier, final StreamsBuilder builder) {
    final StoreBuilder<?> sessionStoreStoreBuilder;
    final ProcessorSupplier<Integer, Integer, Void, Void> processorSupplier;
    sessionStoreStoreBuilder = Stores.sessionStoreBuilder(supplier, Serdes.Integer(), Serdes.Integer());
    processorSupplier = () -> new ContextualProcessor<Integer, Integer, Void, Void>() {

        @Override
        public void process(final Record<Integer, Integer> record) {
            final SessionStore<Integer, Integer> stateStore = context().getStateStore(sessionStoreStoreBuilder.name());
            stateStore.put(new Windowed<>(record.key(), new SessionWindow(WINDOW_START, WINDOW_START)), record.value());
        }
    };
    if (cache) {
        sessionStoreStoreBuilder.withCachingEnabled();
    } else {
        sessionStoreStoreBuilder.withCachingDisabled();
    }
    if (log) {
        sessionStoreStoreBuilder.withLoggingEnabled(Collections.emptyMap());
    } else {
        sessionStoreStoreBuilder.withLoggingDisabled();
    }
    if (storeToTest.global()) {
        builder.addGlobalStore(sessionStoreStoreBuilder, INPUT_TOPIC_NAME, Consumed.with(Serdes.Integer(), Serdes.Integer()), processorSupplier);
    } else {
        builder.addStateStore(sessionStoreStoreBuilder);
        builder.stream(INPUT_TOPIC_NAME, Consumed.with(Serdes.Integer(), Serdes.Integer())).process(processorSupplier, sessionStoreStoreBuilder.name());
    }
}
Also used : SessionStore(org.apache.kafka.streams.state.SessionStore) Windowed(org.apache.kafka.streams.kstream.Windowed) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow)

Example 4 with SessionStore

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

the class SessionStoreFetchTest method testStoreConfig.

@Test
public void testStoreConfig() {
    final Materialized<String, Long, SessionStore<Bytes, byte[]>> stateStoreConfig = getStoreConfig(storeType, STORE_NAME, enableLogging, enableCaching);
    final StreamsBuilder builder = new StreamsBuilder();
    final KStream<String, String> stream = builder.stream("input", Consumed.with(Serdes.String(), Serdes.String()));
    stream.groupByKey(Grouped.with(Serdes.String(), Serdes.String())).windowedBy(SessionWindows.ofInactivityGapWithNoGrace(ofMillis(WINDOW_SIZE))).count(stateStoreConfig).toStream().to("output");
    final Topology topology = builder.build();
    try (final TopologyTestDriver driver = new TopologyTestDriver(topology)) {
        // get input topic and stateStore
        final TestInputTopic<String, String> input = driver.createInputTopic("input", new StringSerializer(), new StringSerializer());
        final SessionStore<String, Long> stateStore = driver.getSessionStore(STORE_NAME);
        // write some data
        final int medium = DATA_SIZE / 2 * 2;
        for (int i = 0; i < records.size(); i++) {
            final KeyValue<String, String> kv = records.get(i);
            final long windowStartTime = i < medium ? 0 : 1500;
            input.pipeInput(kv.key, kv.value, windowStartTime);
            input.pipeInput(kv.key, kv.value, windowStartTime + WINDOW_SIZE);
        }
        verifyNormalQuery(stateStore);
        verifyInfiniteQuery(stateStore);
        verifyRangeQuery(stateStore);
    }
}
Also used : TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Topology(org.apache.kafka.streams.Topology) SessionStore(org.apache.kafka.streams.state.SessionStore) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Example 5 with SessionStore

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

the class SessionWindowedCogroupedKStreamImpl method materialize.

private StoreBuilder<SessionStore<K, V>> materialize(final MaterializedInternal<K, V, SessionStore<Bytes, byte[]>> materialized) {
    SessionBytesStoreSupplier supplier = (SessionBytesStoreSupplier) materialized.storeSupplier();
    if (supplier == null) {
        final long retentionPeriod = materialized.retention() != null ? materialized.retention().toMillis() : sessionWindows.inactivityGap() + sessionWindows.gracePeriodMs();
        if ((sessionWindows.inactivityGap() + sessionWindows.gracePeriodMs()) > retentionPeriod) {
            throw new IllegalArgumentException("The retention period of the session store " + materialized.storeName() + " must be no smaller than the session inactivity gap plus the" + " grace period." + " Got gap=[" + sessionWindows.inactivityGap() + "]," + " grace=[" + sessionWindows.gracePeriodMs() + "]," + " retention=[" + retentionPeriod + "]");
        }
        supplier = Stores.persistentSessionStore(materialized.storeName(), Duration.ofMillis(retentionPeriod));
    }
    final StoreBuilder<SessionStore<K, V>> builder = Stores.sessionStoreBuilder(supplier, materialized.keySerde(), materialized.valueSerde());
    if (materialized.loggingEnabled()) {
        builder.withLoggingEnabled(materialized.logConfig());
    } else {
        builder.withLoggingDisabled();
    }
    if (materialized.cachingEnabled()) {
        builder.withCachingEnabled();
    }
    return builder;
}
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