Class KafkaStreams
- java.lang.Object
-
- org.apache.kafka.streams.KafkaStreams
-
@Evolving public class KafkaStreams extends java.lang.ObjectA Kafka client that allows for performing continuous computation on input coming from one or more input topics and sends output to zero, one, or more output topics.The computational logic can be specified either by using the
Topologyto define a DAG topology ofProcessors or by using theStreamsBuilderwhich provides the high-level DSL to define transformations.One
KafkaStreamsinstance can contain one or more threads specified in the configs for the processing work.A
KafkaStreamsinstance can co-ordinate with any other instances with the sameapplication ID(whether in the same process, on other processes on this machine, or on remote machines) as a single (possibly distributed) stream processing application. These instances will divide up the work based on the assignment of the input topic partitions so that all partitions are being consumed. If instances are added or fail, all (remaining) instances will rebalance the partition assignment among themselves to balance processing load and ensure that all input topic partitions are processed.Internally a
KafkaStreamsinstance contains a normalKafkaProducerandKafkaConsumerinstance that is used for reading input and writing output.A simple example might look like this:
Map<String, Object> props = new HashMap<>(); props.put(StreamsConfig.APPLICATION_ID_CONFIG, "my-stream-processing-application"); props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass()); props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass()); StreamsConfig config = new StreamsConfig(props); StreamsBuilder builder = new StreamsBuilder(); builder.<String, String>stream("my-input-topic").mapValues(value -> value.length().toString()).to("my-output-topic"); KafkaStreams streams = new KafkaStreams(builder.build(), config); streams.start();- See Also:
StreamsBuilder,Topology
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKafkaStreams.StateKafka Streams states are the possible state that a Kafka Streams instance can be in.static interfaceKafkaStreams.StateListenerListen toKafkaStreams.Statechange events.
-
Constructor Summary
Constructors Constructor Description KafkaStreams(TopologyBuilder builder, java.util.Properties props)Deprecated.useKafkaStreams(Topology, Properties)insteadKafkaStreams(TopologyBuilder builder, StreamsConfig config)Deprecated.useKafkaStreams(Topology, StreamsConfig)insteadKafkaStreams(TopologyBuilder builder, StreamsConfig config, KafkaClientSupplier clientSupplier)Deprecated.KafkaStreams(Topology topology, java.util.Properties props)Create aKafkaStreamsinstance.KafkaStreams(Topology topology, StreamsConfig config)Create aKafkaStreamsinstance.KafkaStreams(Topology topology, StreamsConfig config, org.apache.kafka.common.utils.Time time)Create aKafkaStreamsinstance.KafkaStreams(Topology topology, StreamsConfig config, KafkaClientSupplier clientSupplier)Create aKafkaStreamsinstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.util.Collection<StreamsMetadata>allMetadata()Find all currently runningKafkaStreamsinstances (potentially remotely) that use the sameapplication IDas this instance (i.e., all instances that belong to the same Kafka Streams application) and returnStreamsMetadatafor each discovered instance.java.util.Collection<StreamsMetadata>allMetadataForStore(java.lang.String storeName)Find all currently runningKafkaStreamsinstances (potentially remotely) that use the sameapplication IDas this instance (i.e., all instances that belong to the same Kafka Streams application) and that contain aStateStorewith the givenstoreNameand returnStreamsMetadatafor each discovered instance.voidcleanUp()Do a clean up of the localStateStoredirectory (StreamsConfig.STATE_DIR_CONFIG) by deleting all data with regard to theapplication ID.voidclose()Shutdown thisKafkaStreamsinstance by signaling all the threads to stop, and then wait for them to join.booleanclose(long timeout, java.util.concurrent.TimeUnit timeUnit)Shutdown thisKafkaStreamsby signaling all the threads to stop, and then wait up to the timeout for the threads to join.java.util.Set<ThreadMetadata>localThreadsMetadata()Returns runtime information about the local threads of thisKafkaStreamsinstance.<K> StreamsMetadatametadataForKey(java.lang.String storeName, K key, org.apache.kafka.common.serialization.Serializer<K> keySerializer)Find the currently runningKafkaStreamsinstance (potentially remotely) that use the sameapplication IDas this instance (i.e., all instances that belong to the same Kafka Streams application) and that contain aStateStorewith the givenstoreNameand theStateStorecontains the givenkeyand returnStreamsMetadatafor it.<K> StreamsMetadatametadataForKey(java.lang.String storeName, K key, StreamPartitioner<? super K,?> partitioner)Find the currently runningKafkaStreamsinstance (potentially remotely) that use the sameapplication IDas this instance (i.e., all instances that belong to the same Kafka Streams application) and that contain aStateStorewith the givenstoreNameand theStateStorecontains the givenkeyand returnStreamsMetadatafor it.java.util.Map<org.apache.kafka.common.MetricName,? extends org.apache.kafka.common.Metric>metrics()Get read-only handle on global metrics registry.voidsetGlobalStateRestoreListener(StateRestoreListener globalStateRestoreListener)Set the listener which is triggered whenever aStateStoreis being restored in order to resume processing.voidsetStateListener(KafkaStreams.StateListener listener)An app can set a singleKafkaStreams.StateListenerso that the app is notified when state changes.voidsetUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler eh)Set the handler invoked when ainternal threadabruptly terminates due to an uncaught exception.voidstart()Start theKafkaStreamsinstance by starting all its threads.KafkaStreams.Statestate()Return the currentKafkaStreams.Stateof thisKafkaStreamsinstance.<T> Tstore(java.lang.String storeName, QueryableStoreType<T> queryableStoreType)Get a facade wrapping the localStateStoreinstances with the providedstoreNameif the Store's type is accepted by the providedqueryableStoreType.java.lang.StringtoString()Deprecated.UselocalThreadsMetadata()to retrieve runtime information.java.lang.StringtoString(java.lang.String indent)Deprecated.UselocalThreadsMetadata()to retrieve runtime information.
-
-
-
Constructor Detail
-
KafkaStreams
@Deprecated public KafkaStreams(TopologyBuilder builder, java.util.Properties props)
Deprecated.useKafkaStreams(Topology, Properties)instead
-
KafkaStreams
@Deprecated public KafkaStreams(TopologyBuilder builder, StreamsConfig config)
Deprecated.useKafkaStreams(Topology, StreamsConfig)instead
-
KafkaStreams
@Deprecated public KafkaStreams(TopologyBuilder builder, StreamsConfig config, KafkaClientSupplier clientSupplier)
Deprecated.
-
KafkaStreams
public KafkaStreams(Topology topology, java.util.Properties props)
Create aKafkaStreamsinstance.Note: even if you never call
start()on aKafkaStreamsinstance, you still mustclose()it to avoid resource leaks.- Parameters:
topology- the topology specifying the computational logicprops- properties forStreamsConfig- Throws:
StreamsException- if any fatal error occurs
-
KafkaStreams
public KafkaStreams(Topology topology, StreamsConfig config)
Create aKafkaStreamsinstance.Note: even if you never call
start()on aKafkaStreamsinstance, you still mustclose()it to avoid resource leaks.- Parameters:
topology- the topology specifying the computational logicconfig- the Kafka Streams configuration- Throws:
StreamsException- if any fatal error occurs
-
KafkaStreams
public KafkaStreams(Topology topology, StreamsConfig config, KafkaClientSupplier clientSupplier)
Create aKafkaStreamsinstance.Note: even if you never call
start()on aKafkaStreamsinstance, you still mustclose()it to avoid resource leaks.- Parameters:
topology- the topology specifying the computational logicconfig- the Kafka Streams configurationclientSupplier- the Kafka clients supplier which provides underlying producer and consumer clients for the newKafkaStreamsinstance- Throws:
StreamsException- if any fatal error occurs
-
KafkaStreams
public KafkaStreams(Topology topology, StreamsConfig config, org.apache.kafka.common.utils.Time time)
Create aKafkaStreamsinstance.- Parameters:
topology- the topology specifying the computational logicconfig- the Kafka Streams configurationtime-Timeimplementation; cannot be null- Throws:
StreamsException- if any fatal error occurs
-
-
Method Detail
-
state
public KafkaStreams.State state()
Return the currentKafkaStreams.Stateof thisKafkaStreamsinstance.- Returns:
- the current state of this Kafka Streams instance
-
setStateListener
public void setStateListener(KafkaStreams.StateListener listener)
An app can set a singleKafkaStreams.StateListenerso that the app is notified when state changes.- Parameters:
listener- a new state listener- Throws:
java.lang.IllegalStateException- if thisKafkaStreamsinstance is not in stateCREATED.
-
setUncaughtExceptionHandler
public void setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler eh)
Set the handler invoked when ainternal threadabruptly terminates due to an uncaught exception.- Parameters:
eh- the uncaught exception handler for all internal threads;nulldeletes the current handler- Throws:
java.lang.IllegalStateException- if thisKafkaStreamsinstance is not in stateCREATED.
-
setGlobalStateRestoreListener
public void setGlobalStateRestoreListener(StateRestoreListener globalStateRestoreListener)
Set the listener which is triggered whenever aStateStoreis being restored in order to resume processing.- Parameters:
globalStateRestoreListener- The listener triggered whenStateStoreis being restored.- Throws:
java.lang.IllegalStateException- if thisKafkaStreamsinstance is not in stateCREATED.
-
metrics
public java.util.Map<org.apache.kafka.common.MetricName,? extends org.apache.kafka.common.Metric> metrics()
Get read-only handle on global metrics registry.- Returns:
- Map of all metrics.
-
start
public void start() throws java.lang.IllegalStateException, StreamsExceptionStart theKafkaStreamsinstance by starting all its threads. This function is expected to be called only once during the life cycle of the client.Because threads are started in the background, this method does not block. As a consequence, any fatal exception that happens during processing is by default only logged. If you want to be notified about dying threads, you can
register an uncaught exception handlerbefore starting theKafkaStreamsinstance.Note, for brokers with version
0.9.xor lower, the broker version cannot be checked. There will be no error and the client will hang and retry to verify the broker version until ittimes out.- Throws:
java.lang.IllegalStateException- if process was already startedStreamsException- if the Kafka brokers have version 0.10.0.x or ifexactly-onceis enabled for pre 0.11.0.x brokers
-
close
public void close()
Shutdown thisKafkaStreamsinstance by signaling all the threads to stop, and then wait for them to join. This will block until all threads have stopped.
-
close
public boolean close(long timeout, java.util.concurrent.TimeUnit timeUnit)Shutdown thisKafkaStreamsby signaling all the threads to stop, and then wait up to the timeout for the threads to join. Atimeoutof 0 means to wait forever.- Parameters:
timeout- how long to wait for the threads to shutdowntimeUnit- unit of time used for timeout- Returns:
trueif all threads were successfully stopped—falseif the timeout was reached before all threads stopped Note that this method must not be called in theonChangecallback ofKafkaStreams.StateListener.
-
toString
@Deprecated public java.lang.String toString()
Deprecated.UselocalThreadsMetadata()to retrieve runtime information.Produce a string representation containing useful information about thisKafkaStreaminstance such as thread IDs, task IDs, and a representation of the topology DAG includingStateStores (cf.TopologyandStreamsBuilder).- Overrides:
toStringin classjava.lang.Object- Returns:
- A string representation of the Kafka Streams instance.
-
toString
@Deprecated public java.lang.String toString(java.lang.String indent)
Deprecated.UselocalThreadsMetadata()to retrieve runtime information.Produce a string representation containing useful information about thisKafkaStreaminstance such as thread IDs, task IDs, and a representation of the topology DAG includingStateStores (cf.TopologyandStreamsBuilder).- Parameters:
indent- the top-level indent for each line- Returns:
- A string representation of the Kafka Streams instance.
-
cleanUp
public void cleanUp()
Do a clean up of the localStateStoredirectory (StreamsConfig.STATE_DIR_CONFIG) by deleting all data with regard to theapplication ID.May only be called either before this
KafkaStreamsinstance isstartedor after the instance isclosed.Calling this method triggers a restore of local
StateStores on the nextapplication start.- Throws:
java.lang.IllegalStateException- if thisKafkaStreamsinstance is currentlyrunningStreamsException- if cleanup failed
-
allMetadata
public java.util.Collection<StreamsMetadata> allMetadata()
Find all currently runningKafkaStreamsinstances (potentially remotely) that use the sameapplication IDas this instance (i.e., all instances that belong to the same Kafka Streams application) and returnStreamsMetadatafor each discovered instance.Note: this is a point in time view and it may change due to partition reassignment.
- Returns:
StreamsMetadatafor eachKafkaStreamsinstances of this application
-
allMetadataForStore
public java.util.Collection<StreamsMetadata> allMetadataForStore(java.lang.String storeName)
Find all currently runningKafkaStreamsinstances (potentially remotely) that- use the same
application IDas this instance (i.e., all instances that belong to the same Kafka Streams application) - and that contain a
StateStorewith the givenstoreName
StreamsMetadatafor each discovered instance.Note: this is a point in time view and it may change due to partition reassignment.
- Parameters:
storeName- thestoreNameto find metadata for- Returns:
StreamsMetadatafor eachKafkaStreamsinstances with the providestoreNameof this application
- use the same
-
metadataForKey
public <K> StreamsMetadata metadataForKey(java.lang.String storeName, K key, org.apache.kafka.common.serialization.Serializer<K> keySerializer)
Find the currently runningKafkaStreamsinstance (potentially remotely) that- use the same
application IDas this instance (i.e., all instances that belong to the same Kafka Streams application) - and that contain a
StateStorewith the givenstoreName - and the
StateStorecontains the givenkey
StreamsMetadatafor it.This will use the default Kafka Streams partitioner to locate the partition. If a
custom partitionerhas beenconfiguredviaStreamsConfigorKStream.through(String, Produced), or if the originalKTable's inputtopicis partitioned differently, please usemetadataForKey(String, Object, StreamPartitioner).Note:
- this is a point in time view and it may change due to partition reassignment
- the key may not exist in the
StateStore; this method provides a way of finding which host it would exist on - if this is for a window store the serializer should be the serializer for the record key, not the window serializer
- Type Parameters:
K- key type- Parameters:
storeName- thestoreNameto find metadata forkey- the key to find metadata forkeySerializer- serializer for the key- Returns:
StreamsMetadatafor theKafkaStreamsinstance with the providestoreNameandkeyof this application orStreamsMetadata.NOT_AVAILABLEif Kafka Streams is (re-)initializing
- use the same
-
metadataForKey
public <K> StreamsMetadata metadataForKey(java.lang.String storeName, K key, StreamPartitioner<? super K,?> partitioner)
Find the currently runningKafkaStreamsinstance (potentially remotely) that- use the same
application IDas this instance (i.e., all instances that belong to the same Kafka Streams application) - and that contain a
StateStorewith the givenstoreName - and the
StateStorecontains the givenkey
StreamsMetadatafor it.Note:
- this is a point in time view and it may change due to partition reassignment
- the key may not exist in the
StateStore; this method provides a way of finding which host it would exist on
- Type Parameters:
K- key type- Parameters:
storeName- thestoreNameto find metadata forkey- the key to find metadata forpartitioner- the partitioner to be use to locate the host for the key- Returns:
StreamsMetadatafor theKafkaStreamsinstance with the providestoreNameandkeyof this application orStreamsMetadata.NOT_AVAILABLEif Kafka Streams is (re-)initializing
- use the same
-
store
public <T> T store(java.lang.String storeName, QueryableStoreType<T> queryableStoreType)Get a facade wrapping the localStateStoreinstances with the providedstoreNameif the Store's type is accepted by the providedqueryableStoreType. The returned object can be used to query theStateStoreinstances.- Type Parameters:
T- return type- Parameters:
storeName- name of the store to findqueryableStoreType- accept only stores that are accepted byQueryableStoreType.accepts(StateStore)- Returns:
- A facade wrapping the local
StateStoreinstances - Throws:
InvalidStateStoreException- if Kafka Streams is (re-)initializing or a store withstoreNameandqueryableStoreTypedoesnt' exist
-
localThreadsMetadata
public java.util.Set<ThreadMetadata> localThreadsMetadata()
Returns runtime information about the local threads of thisKafkaStreamsinstance.- Returns:
- the set of
ThreadMetadata.
-
-