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;
}
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());
}
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());
}
}
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);
}
}
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;
}
Aggregations