Search in sources :

Example 1 with StateStoreSupplier

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;
}
Also used : KTableSource(org.apache.kafka.streams.kstream.internals.KTableSource) StateStoreSupplier(org.apache.kafka.streams.processor.StateStoreSupplier) GlobalKTableImpl(org.apache.kafka.streams.kstream.internals.GlobalKTableImpl) KTableImpl(org.apache.kafka.streams.kstream.internals.KTableImpl) RocksDBKeyValueStoreSupplier(org.apache.kafka.streams.state.internals.RocksDBKeyValueStoreSupplier)

Example 2 with StateStoreSupplier

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());
}
Also used : StateStoreSupplier(org.apache.kafka.streams.processor.StateStoreSupplier) Test(org.junit.Test)

Example 3 with StateStoreSupplier

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"));
}
Also used : StateStoreSupplier(org.apache.kafka.streams.processor.StateStoreSupplier) Test(org.junit.Test)

Example 4 with StateStoreSupplier

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());
}
Also used : StateStoreSupplier(org.apache.kafka.streams.processor.StateStoreSupplier) Test(org.junit.Test)

Example 5 with StateStoreSupplier

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;
}
Also used : StateStoreSupplier(org.apache.kafka.streams.processor.StateStoreSupplier) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore)

Aggregations

StateStoreSupplier (org.apache.kafka.streams.processor.StateStoreSupplier)15 Test (org.junit.Test)11 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)3 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)2 MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)2 MockStateStoreSupplier (org.apache.kafka.test.MockStateStoreSupplier)2 HashMap (java.util.HashMap)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 GlobalKTableImpl (org.apache.kafka.streams.kstream.internals.GlobalKTableImpl)1 KTableImpl (org.apache.kafka.streams.kstream.internals.KTableImpl)1 KTableSource (org.apache.kafka.streams.kstream.internals.KTableSource)1 StateStore (org.apache.kafka.streams.processor.StateStore)1 RocksDBKeyValueStoreSupplier (org.apache.kafka.streams.state.internals.RocksDBKeyValueStoreSupplier)1 ProcessorTopologyTestDriver (org.apache.kafka.test.ProcessorTopologyTestDriver)1 IterativeJoin (org.apache.rya.api.function.join.IterativeJoin)1 LeftOuterJoin (org.apache.rya.api.function.join.LeftOuterJoin)1 NaturalJoin (org.apache.rya.api.function.join.NaturalJoin)1 VisibilityBindingSetSerde (org.apache.rya.streams.kafka.serialization.VisibilityBindingSetSerde)1 VisibilityStatementDeserializer (org.apache.rya.streams.kafka.serialization.VisibilityStatementDeserializer)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1