Search in sources :

Example 1 with GlobalKTableImpl

use of org.apache.kafka.streams.kstream.internals.GlobalKTableImpl 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

GlobalKTableImpl (org.apache.kafka.streams.kstream.internals.GlobalKTableImpl)1 KTableSource (org.apache.kafka.streams.kstream.internals.KTableSource)1 StateStore (org.apache.kafka.streams.processor.StateStore)1