Search in sources :

Example 11 with StateStore

use of org.apache.kafka.streams.processor.StateStore in project kafka by apache.

the class ProcessorStateManager method flush.

@Override
public void flush(final InternalProcessorContext context) {
    if (!this.stores.isEmpty()) {
        log.debug("{} Flushing all stores registered in the state manager", logPrefix);
        for (StateStore store : this.stores.values()) {
            try {
                log.trace("{} Flushing store={}", logPrefix, store.name());
                store.flush();
            } catch (Exception e) {
                throw new ProcessorStateException(String.format("%s Failed to flush state store %s", logPrefix, store.name()), e);
            }
        }
    }
}
Also used : StateStore(org.apache.kafka.streams.processor.StateStore) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) IOException(java.io.IOException) StreamsException(org.apache.kafka.streams.errors.StreamsException) LockException(org.apache.kafka.streams.errors.LockException)

Example 12 with StateStore

use of org.apache.kafka.streams.processor.StateStore in project kafka by apache.

the class KStreamBuilder method globalTable.

/**
     * Create a {@link GlobalKTable} for the specified topic.
     * The default key and value deserializers as specified in the {@link StreamsConfig config} are used.
     * Input {@link KeyValue records} with {@code null} key will be dropped.
     * <p>
     * The resulting {@link GlobalKTable} 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);
     * }</pre>
     * Note that {@link GlobalKTable} always applies {@code "auto.offset.reset"} strategy {@code "earliest"}
     * regardless of the specified value in {@link StreamsConfig}.
     *
     * @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 GlobalKTable} for the specified topic
     */
@SuppressWarnings("unchecked")
public <K, V> GlobalKTable<K, V> globalTable(final Serde<K> keySerde, final Serde<V> valSerde, final String topic, final String storeName) {
    final String sourceName = newName(KStreamImpl.SOURCE_NAME);
    final String processorName = newName(KTableImpl.SOURCE_NAME);
    final KTableSource<K, V> tableSource = new KTableSource<>(storeName);
    final Deserializer<K> keyDeserializer = keySerde == null ? null : keySerde.deserializer();
    final Deserializer<V> valueDeserializer = valSerde == null ? null : valSerde.deserializer();
    final StateStore store = new RocksDBKeyValueStoreSupplier<>(storeName, keySerde, valSerde, false, Collections.<String, String>emptyMap(), true).get();
    addGlobalStore(store, sourceName, keyDeserializer, valueDeserializer, topic, processorName, tableSource);
    return new GlobalKTableImpl(new KTableSourceValueGetterSupplier<>(storeName));
}
Also used : KTableSource(org.apache.kafka.streams.kstream.internals.KTableSource) StateStore(org.apache.kafka.streams.processor.StateStore) GlobalKTableImpl(org.apache.kafka.streams.kstream.internals.GlobalKTableImpl)

Aggregations

StateStore (org.apache.kafka.streams.processor.StateStore)12 Test (org.junit.Test)5 IOException (java.io.IOException)3 LockException (org.apache.kafka.streams.errors.LockException)3 StreamsException (org.apache.kafka.streams.errors.StreamsException)3 ProcessorTopology (org.apache.kafka.streams.processor.internals.ProcessorTopology)3 File (java.io.File)2 MockTime (org.apache.kafka.common.utils.MockTime)2 ProcessorStateException (org.apache.kafka.streams.errors.ProcessorStateException)2 NoOpProcessorContext (org.apache.kafka.test.NoOpProcessorContext)2 NoOpRecordCollector (org.apache.kafka.test.NoOpRecordCollector)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 PartitionInfo (org.apache.kafka.common.PartitionInfo)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 Metrics (org.apache.kafka.common.metrics.Metrics)1 StreamsConfig (org.apache.kafka.streams.StreamsConfig)1 StreamsMetrics (org.apache.kafka.streams.StreamsMetrics)1 GlobalKTableImpl (org.apache.kafka.streams.kstream.internals.GlobalKTableImpl)1