use of org.apache.kafka.streams.processor.StateStoreSupplier in project kafka by apache.
the class KStreamBuilder method table.
/**
* Create a {@link KTable} for the specified topic.
* Input {@link KeyValue records} with {@code null} key will be dropped.
* <p>
* Note that the specified input topics must be partitioned by key.
* If this is not the case the returned {@link KTable} will be corrupted.
* <p>
* The resulting {@link KTable} will be materialized in a local {@link KeyValueStore} with the given
* {@code storeName}.
* However, no internal changelog topic is created since the original input topic can be used for recovery (cf.
* methods of {@link KGroupedStream} and {@link KGroupedTable} that return a {@link KTable}).
* <p>
* To query the local {@link KeyValueStore} it must be obtained via
* {@link KafkaStreams#store(String, QueryableStoreType) KafkaStreams#store(...)}:
* <pre>{@code
* KafkaStreams streams = ...
* ReadOnlyKeyValueStore<String,Long> localStore = streams.store(storeName, QueryableStoreTypes.<String, Long>keyValueStore());
* String key = "some-key";
* Long valueForKey = localStore.get(key); // key must be local (application state is shared over all running Kafka Streams instances)
* }</pre>
* For non-local keys, a custom RPC mechanism must be implemented using {@link KafkaStreams#allMetadata()} to
* query the value of the key on a parallel running instance of your Kafka Streams application.
*
* @param offsetReset the {@code "auto.offset.reset"} policy to use for the specified topic if no valid committed
* offsets are available
* @param keySerde key serde used to send key-value pairs,
* if not specified the default key serde defined in the configuration will be used
* @param valSerde value serde used to send key-value pairs,
* if not specified the default value serde defined in the configuration will be used
* @param topic the topic name; cannot be {@code null}
* @param storeName the state store name; cannot be {@code null}
* @return a {@link KTable} for the specified topic
*/
public <K, V> KTable<K, V> table(final AutoOffsetReset offsetReset, final Serde<K> keySerde, final Serde<V> valSerde, final String topic, final String storeName) {
final String source = newName(KStreamImpl.SOURCE_NAME);
final String name = newName(KTableImpl.SOURCE_NAME);
final ProcessorSupplier<K, V> processorSupplier = new KTableSource<>(storeName);
addSource(offsetReset, source, keySerde == null ? null : keySerde.deserializer(), valSerde == null ? null : valSerde.deserializer(), topic);
addProcessor(name, processorSupplier, source);
final KTableImpl<K, ?, V> kTable = new KTableImpl<>(this, name, processorSupplier, Collections.singleton(source), storeName);
// only materialize the KTable into a state store if the storeName is not null
if (storeName != null) {
final StateStoreSupplier storeSupplier = new RocksDBKeyValueStoreSupplier<>(storeName, keySerde, valSerde, false, Collections.<String, String>emptyMap(), true);
addStateStore(storeSupplier, name);
connectSourceStoreAndTopic(storeName, topic);
}
return kTable;
}
use of org.apache.kafka.streams.processor.StateStoreSupplier in project kafka by apache.
the class StoresTest method shouldCreateInMemoryStoreSupplierNotLogged.
@Test
public void shouldCreateInMemoryStoreSupplierNotLogged() throws Exception {
final StateStoreSupplier supplier = Stores.create("store").withKeys(Serdes.String()).withValues(Serdes.String()).inMemory().disableLogging().build();
assertFalse(supplier.loggingEnabled());
}
use of org.apache.kafka.streams.processor.StateStoreSupplier in project kafka by apache.
the class StoresTest method shouldCreatePersistenStoreSupplierWithLoggedConfig.
@Test
public void shouldCreatePersistenStoreSupplierWithLoggedConfig() throws Exception {
final StateStoreSupplier supplier = Stores.create("store").withKeys(Serdes.String()).withValues(Serdes.String()).persistent().enableLogging(Collections.singletonMap("retention.ms", "1000")).build();
final Map<String, String> config = supplier.logConfig();
assertTrue(supplier.loggingEnabled());
assertEquals("1000", config.get("retention.ms"));
}
use of org.apache.kafka.streams.processor.StateStoreSupplier in project kafka by apache.
the class StoresTest method shouldCreatePersistenStoreSupplierNotLogged.
@Test
public void shouldCreatePersistenStoreSupplierNotLogged() throws Exception {
final StateStoreSupplier supplier = Stores.create("store").withKeys(Serdes.String()).withValues(Serdes.String()).persistent().disableLogging().build();
assertFalse(supplier.loggingEnabled());
}
use of org.apache.kafka.streams.processor.StateStoreSupplier in project kafka by apache.
the class InMemoryKeyValueStoreTest method createKeyValueStore.
@SuppressWarnings("unchecked")
@Override
protected <K, V> KeyValueStore<K, V> createKeyValueStore(ProcessorContext context, Class<K> keyClass, Class<V> valueClass, boolean useContextSerdes) {
StateStoreSupplier supplier;
if (useContextSerdes) {
supplier = Stores.create("my-store").withKeys(context.keySerde()).withValues(context.valueSerde()).inMemory().build();
} else {
supplier = Stores.create("my-store").withKeys(keyClass).withValues(valueClass).inMemory().build();
}
KeyValueStore<K, V> store = (KeyValueStore<K, V>) supplier.get();
store.init(context, store);
return store;
}
Aggregations