Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kafka-streams-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ dependencies {
testImplementation(libs.mockito.core)
testImplementation(libs.mockito.junit)
testImplementation(libs.logcaptor)
testImplementation(group = "org.apache.kafka", name = "kafka-streams-test-utils")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* MIT License
*
* Copyright (c) 2025 bakdata
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.bakdata.kafka.streams.kstream;

import com.bakdata.kafka.Configurator;
import com.bakdata.kafka.Preconfigured;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
import org.apache.kafka.streams.state.KeyValueStore;
import org.apache.kafka.streams.state.SessionBytesStoreSupplier;
import org.apache.kafka.streams.state.SessionStore;
import org.apache.kafka.streams.state.StoreBuilder;
import org.apache.kafka.streams.state.Stores;
import org.apache.kafka.streams.state.TimestampedKeyValueStore;
import org.apache.kafka.streams.state.TimestampedWindowStore;
import org.apache.kafka.streams.state.VersionedBytesStoreSupplier;
import org.apache.kafka.streams.state.VersionedKeyValueStore;
import org.apache.kafka.streams.state.WindowBytesStoreSupplier;
import org.apache.kafka.streams.state.WindowStore;

/**
* Use {@link Preconfigured} to lazily configure {@link Serde} for {@link Stores} using {@link Configurator}
*/
@RequiredArgsConstructor
public class StoresX {

private final @NonNull Configurator configurator;

/**
* @see Stores#sessionStoreBuilder(SessionBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<SessionStore<K, V>> sessionStoreBuilder(final SessionBytesStoreSupplier supplier,
final Preconfigured<? extends Serde<K>> keySerde, final Preconfigured<? extends Serde<V>> valueSerde) {
return Stores.sessionStoreBuilder(supplier, this.configurator.configureForKeys(keySerde),
this.configurator.configureForValues(valueSerde));
}

/**
* @see Stores#sessionStoreBuilder(SessionBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<SessionStore<K, V>> sessionStoreBuilder(final SessionBytesStoreSupplier supplier,
final Serde<K> keySerde, final Serde<V> valueSerde) {
return this.sessionStoreBuilder(supplier, Preconfigured.create(keySerde), Preconfigured.create(valueSerde));
}

/**
* @see Stores#timestampedWindowStoreBuilder(WindowBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<TimestampedWindowStore<K, V>> timestampedWindowStoreBuilder(
final WindowBytesStoreSupplier supplier, final Preconfigured<? extends Serde<K>> keySerde,
final Preconfigured<? extends Serde<V>> valueSerde) {
return Stores.timestampedWindowStoreBuilder(supplier, this.configurator.configureForKeys(keySerde),
this.configurator.configureForValues(valueSerde));
}

/**
* @see Stores#timestampedWindowStoreBuilder(WindowBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<TimestampedWindowStore<K, V>> timestampedWindowStoreBuilder(
final WindowBytesStoreSupplier supplier, final Serde<K> keySerde, final Serde<V> valueSerde) {
return this.timestampedWindowStoreBuilder(supplier, Preconfigured.create(keySerde),
Preconfigured.create(valueSerde));
}

/**
* @see Stores#windowStoreBuilder(WindowBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<WindowStore<K, V>> windowStoreBuilder(final WindowBytesStoreSupplier supplier,
final Preconfigured<? extends Serde<K>> keySerde, final Preconfigured<? extends Serde<V>> valueSerde) {
return Stores.windowStoreBuilder(supplier, this.configurator.configureForKeys(keySerde),
this.configurator.configureForValues(valueSerde));
}

/**
* @see Stores#windowStoreBuilder(WindowBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<WindowStore<K, V>> windowStoreBuilder(final WindowBytesStoreSupplier supplier,
final Serde<K> keySerde, final Serde<V> valueSerde) {
return this.windowStoreBuilder(supplier, Preconfigured.create(keySerde), Preconfigured.create(valueSerde));
}

/**
* @see Stores#versionedKeyValueStoreBuilder(VersionedBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<VersionedKeyValueStore<K, V>> versionedKeyValueStoreBuilder(
final VersionedBytesStoreSupplier supplier, final Preconfigured<? extends Serde<K>> keySerde,
final Preconfigured<? extends Serde<V>> valueSerde) {
return Stores.versionedKeyValueStoreBuilder(supplier, this.configurator.configureForKeys(keySerde),
this.configurator.configureForValues(valueSerde));
}

/**
* @see Stores#versionedKeyValueStoreBuilder(VersionedBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<VersionedKeyValueStore<K, V>> versionedKeyValueStoreBuilder(
final VersionedBytesStoreSupplier supplier, final Serde<K> keySerde, final Serde<V> valueSerde) {
return this.versionedKeyValueStoreBuilder(supplier, Preconfigured.create(keySerde),
Preconfigured.create(valueSerde));
}

/**
* @see Stores#timestampedKeyValueStoreBuilder(KeyValueBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<TimestampedKeyValueStore<K, V>> timestampedKeyValueStoreBuilder(
final KeyValueBytesStoreSupplier supplier, final Preconfigured<? extends Serde<K>> keySerde,
final Preconfigured<? extends Serde<V>> valueSerde) {
return Stores.timestampedKeyValueStoreBuilder(supplier, this.configurator.configureForKeys(keySerde),
this.configurator.configureForValues(valueSerde));
}

/**
* @see Stores#timestampedKeyValueStoreBuilder(KeyValueBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<TimestampedKeyValueStore<K, V>> timestampedKeyValueStoreBuilder(
final KeyValueBytesStoreSupplier supplier, final Serde<K> keySerde,
final Serde<V> valueSerde) {
return this.timestampedKeyValueStoreBuilder(supplier, Preconfigured.create(keySerde),
Preconfigured.create(valueSerde));
}

/**
* @see Stores#keyValueStoreBuilder(KeyValueBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<KeyValueStore<K, V>> keyValueStoreBuilder(final KeyValueBytesStoreSupplier supplier,
final Preconfigured<? extends Serde<K>> keySerde, final Preconfigured<? extends Serde<V>> valueSerde) {
return Stores.keyValueStoreBuilder(supplier, this.configurator.configureForKeys(keySerde),
this.configurator.configureForValues(valueSerde));
}

/**
* @see Stores#keyValueStoreBuilder(KeyValueBytesStoreSupplier, Serde, Serde)
*/
public <K, V> StoreBuilder<KeyValueStore<K, V>> keyValueStoreBuilder(final KeyValueBytesStoreSupplier supplier,
final Serde<K> keySerde, final Serde<V> valueSerde) {
return this.keyValueStoreBuilder(supplier, Preconfigured.create(keySerde), Preconfigured.create(valueSerde));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* MIT License
*
* Copyright (c) 2025 bakdata
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.bakdata.kafka.streams.kstream;

import org.apache.kafka.streams.processor.StateStore;
import org.apache.kafka.streams.processor.api.Processor;
import org.apache.kafka.streams.processor.api.ProcessorContext;
import org.apache.kafka.streams.processor.api.Record;

abstract class SimpleProcessor<KIn, VIn, KOut, VOut> implements Processor<KIn, VIn, KOut, VOut> {
private ProcessorContext<KOut, VOut> context = null;

@Override
public void init(final ProcessorContext<KOut, VOut> context) {
this.context = context;
}

protected void forward(final Record<? extends KOut, ? extends VOut> outputRecord) {
this.context.forward(outputRecord);
}

protected <S extends StateStore> S getStateStore(final String name) {
return this.context.getStateStore(name);
}

}
Loading
Loading