@InterfaceStability.Evolving public class TopologyBuilder extends java.lang.Object
ProcessorTopology
. A topology contains an acyclic graph of sources, processors,
and sinks. A source
is a node in the graph that consumes one or more Kafka topics and forwards them to
its child nodes. A processor
is a node in the graph that receives input records from upstream nodes,
processes that records, and optionally forwarding new records to one or all of its children. Finally, a sink
is a node in the graph that receives records from upstream nodes and writes them to a Kafka topic. This builder allows you
to construct an acyclic graph of these nodes, and the builder is then passed into a new KafkaStreams
instance that will then begin consuming, processing, and producing records
.Modifier and Type | Class and Description |
---|---|
static class |
TopologyBuilder.AutoOffsetReset
|
static class |
TopologyBuilder.TopicsInfo |
Constructor and Description |
---|
TopologyBuilder()
Create a new builder.
|
Modifier and Type | Method and Description |
---|---|
TopologyBuilder |
addGlobalStore(StateStoreSupplier<KeyValueStore> storeSupplier,
java.lang.String sourceName,
Deserializer keyDeserializer,
Deserializer valueDeserializer,
java.lang.String topic,
java.lang.String processorName,
ProcessorSupplier stateUpdateSupplier)
Adds a global
StateStore to the topology. |
TopologyBuilder |
addGlobalStore(StateStoreSupplier<KeyValueStore> storeSupplier,
java.lang.String sourceName,
TimestampExtractor timestampExtractor,
Deserializer keyDeserializer,
Deserializer valueDeserializer,
java.lang.String topic,
java.lang.String processorName,
ProcessorSupplier stateUpdateSupplier)
Adds a global
StateStore to the topology. |
TopologyBuilder |
addInternalTopic(java.lang.String topicName)
Adds an internal topic
|
TopologyBuilder |
addProcessor(java.lang.String name,
ProcessorSupplier supplier,
java.lang.String... parentNames)
Add a new processor node that receives and processes records output by one or more parent source or processor node.
|
<K,V> TopologyBuilder |
addSink(java.lang.String name,
java.lang.String topic,
Serializer<K> keySerializer,
Serializer<V> valSerializer,
StreamPartitioner<? super K,? super V> partitioner,
java.lang.String... parentNames)
Add a new sink that forwards records from upstream parent processor and/or source nodes to the named Kafka topic.
|
TopologyBuilder |
addSink(java.lang.String name,
java.lang.String topic,
Serializer keySerializer,
Serializer valSerializer,
java.lang.String... parentNames)
Add a new sink that forwards records from upstream parent processor and/or source nodes to the named Kafka topic.
|
TopologyBuilder |
addSink(java.lang.String name,
java.lang.String topic,
StreamPartitioner partitioner,
java.lang.String... parentNames)
Add a new sink that forwards records from upstream parent processor and/or source nodes to the named Kafka topic, using
the supplied partitioner.
|
TopologyBuilder |
addSink(java.lang.String name,
java.lang.String topic,
java.lang.String... parentNames)
Add a new sink that forwards records from upstream parent processor and/or source nodes to the named Kafka topic.
|
TopologyBuilder |
addSource(java.lang.String name,
Deserializer keyDeserializer,
Deserializer valDeserializer,
java.util.regex.Pattern topicPattern)
Add a new source that consumes from topics matching the given pattern
and forwards the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(java.lang.String name,
Deserializer keyDeserializer,
Deserializer valDeserializer,
java.lang.String... topics)
Add a new source that consumes the named topics and forwards the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(java.lang.String name,
java.util.regex.Pattern topicPattern)
Add a new source that consumes from topics matching the given pattern
and forward the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(java.lang.String name,
java.lang.String... topics)
Add a new source that consumes the named topics and forward the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(TimestampExtractor timestampExtractor,
java.lang.String name,
java.util.regex.Pattern topicPattern)
Add a new source that consumes from topics matching the given pattern
and forward the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(TimestampExtractor timestampExtractor,
java.lang.String name,
java.lang.String... topics)
Add a new source that consumes the named topics and forward the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(TopologyBuilder.AutoOffsetReset offsetReset,
java.lang.String name,
Deserializer keyDeserializer,
Deserializer valDeserializer,
java.util.regex.Pattern topicPattern)
Add a new source that consumes from topics matching the given pattern
and forwards the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(TopologyBuilder.AutoOffsetReset offsetReset,
java.lang.String name,
java.util.regex.Pattern topicPattern)
Add a new source that consumes from topics matching the given pattern
and forward the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(TopologyBuilder.AutoOffsetReset offsetReset,
java.lang.String name,
java.lang.String... topics)
Add a new source that consumes the named topics and forward the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(TopologyBuilder.AutoOffsetReset offsetReset,
java.lang.String name,
TimestampExtractor timestampExtractor,
Deserializer keyDeserializer,
Deserializer valDeserializer,
java.util.regex.Pattern topicPattern)
Add a new source that consumes from topics matching the given pattern
and forwards the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(TopologyBuilder.AutoOffsetReset offsetReset,
java.lang.String name,
TimestampExtractor timestampExtractor,
Deserializer keyDeserializer,
Deserializer valDeserializer,
java.lang.String... topics)
Add a new source that consumes the named topics and forwards the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(TopologyBuilder.AutoOffsetReset offsetReset,
TimestampExtractor timestampExtractor,
java.lang.String name,
java.util.regex.Pattern topicPattern)
Add a new source that consumes from topics matching the given pattern
and forward the records to child processor and/or sink nodes.
|
TopologyBuilder |
addSource(TopologyBuilder.AutoOffsetReset offsetReset,
TimestampExtractor timestampExtractor,
java.lang.String name,
java.lang.String... topics)
Add a new source that consumes the named topics and forward the records to child processor and/or sink nodes.
|
TopologyBuilder |
addStateStore(StateStoreSupplier supplier,
java.lang.String... processorNames)
Adds a state store
|
ProcessorTopology |
build(java.lang.Integer topicGroupId)
Build the topology for the specified topic group.
|
ProcessorTopology |
buildGlobalStateTopology()
Builds the topology for any global state stores
|
TopologyBuilder |
connectProcessorAndStateStores(java.lang.String processorName,
java.lang.String... stateStoreNames)
Connects the processor and the state stores
|
TopologyBuilder |
connectProcessors(java.lang.String... processorNames)
Connects a list of processors.
|
protected TopologyBuilder |
connectSourceStoreAndTopic(java.lang.String sourceStoreName,
java.lang.String topic)
This is used only for KStreamBuilder: when adding a KTable from a source topic,
we need to add the topic as the KTable's materialized state store's changelog.
|
java.util.Collection<java.util.Set<java.lang.String>> |
copartitionGroups()
Returns the copartition groups.
|
TopologyBuilder |
copartitionSources(java.util.Collection<java.lang.String> sourceNodes)
Asserts that the streams of the specified source nodes must be copartitioned.
|
java.util.regex.Pattern |
earliestResetTopicsPattern()
Get the Pattern to match all topics requiring to start reading from earliest available offset
|
java.util.Map<java.lang.String,StateStore> |
globalStateStores()
Get any global
StateStore s that are part of the
topology |
java.util.regex.Pattern |
latestResetTopicsPattern()
Get the Pattern to match all topics requiring to start reading from latest available offset
|
java.util.Map<java.lang.Integer,java.util.Set<java.lang.String>> |
nodeGroups()
Returns the map of node groups keyed by the topic group id.
|
TopologyBuilder |
setApplicationId(java.lang.String applicationId)
Set the applicationId to be used for auto-generated internal topics.
|
java.util.regex.Pattern |
sourceTopicPattern() |
java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
stateStoreNameToSourceTopics() |
SubscriptionUpdates |
subscriptionUpdates() |
java.util.Map<java.lang.Integer,TopologyBuilder.TopicsInfo> |
topicGroups()
Returns the map of topic groups keyed by the group id.
|
void |
updateSubscriptions(SubscriptionUpdates subscriptionUpdates,
java.lang.String threadId) |
public final TopologyBuilder setApplicationId(java.lang.String applicationId)
topicGroups()
, copartitionSources(java.util.Collection<java.lang.String>)
,
stateStoreNameToSourceTopics
and build(Integer)
.applicationId
- the streams applicationId. Should be the same as set by
StreamsConfig.APPLICATION_ID_CONFIG
public final TopologyBuilder addSource(java.lang.String name, java.lang.String... topics)
default key deserializer
and
default value deserializer
specified in the
stream configuration
.
The default TimestampExtractor
as specified in the config
is used.name
- the unique name of the source used to reference this node when
adding processor children
.topics
- the name of one or more Kafka topics that this source is to consumepublic final TopologyBuilder addSource(TopologyBuilder.AutoOffsetReset offsetReset, java.lang.String name, java.lang.String... topics)
default key deserializer
and
default value deserializer
specified in the
stream configuration
.
The default TimestampExtractor
as specified in the config
is used.offsetReset
- the auto offset reset policy to use for this source if no committed offsets found; acceptable values earliest or latestname
- the unique name of the source used to reference this node when
adding processor children
.topics
- the name of one or more Kafka topics that this source is to consumepublic final TopologyBuilder addSource(TimestampExtractor timestampExtractor, java.lang.String name, java.lang.String... topics)
default key deserializer
and
default value deserializer
specified in the
stream configuration
.timestampExtractor
- the stateless timestamp extractor used for this source,
if not specified the default extractor defined in the configs will be usedname
- the unique name of the source used to reference this node when
adding processor children
.topics
- the name of one or more Kafka topics that this source is to consumepublic final TopologyBuilder addSource(TopologyBuilder.AutoOffsetReset offsetReset, TimestampExtractor timestampExtractor, java.lang.String name, java.lang.String... topics)
default key deserializer
and
default value deserializer
specified in the
stream configuration
.offsetReset
- the auto offset reset policy to use for this source if no committed offsets found;
acceptable values earliest or latesttimestampExtractor
- the stateless timestamp extractor used for this source,
if not specified the default extractor defined in the configs will be usedname
- the unique name of the source used to reference this node when
adding processor children
.topics
- the name of one or more Kafka topics that this source is to consumepublic final TopologyBuilder addSource(java.lang.String name, java.util.regex.Pattern topicPattern)
default key deserializer
and
default value deserializer
specified in the
stream configuration
.
The default TimestampExtractor
as specified in the config
is used.name
- the unique name of the source used to reference this node when
adding processor children
.topicPattern
- regular expression pattern to match Kafka topics that this source is to consumepublic final TopologyBuilder addSource(TopologyBuilder.AutoOffsetReset offsetReset, java.lang.String name, java.util.regex.Pattern topicPattern)
default key deserializer
and
default value deserializer
specified in the
stream configuration
.
The default TimestampExtractor
as specified in the config
is used.offsetReset
- the auto offset reset policy value for this source if no committed offsets found; acceptable values earliest or latest.name
- the unique name of the source used to reference this node when
adding processor children
.topicPattern
- regular expression pattern to match Kafka topics that this source is to consumepublic final TopologyBuilder addSource(TimestampExtractor timestampExtractor, java.lang.String name, java.util.regex.Pattern topicPattern)
default key deserializer
and
default value deserializer
specified in the
stream configuration
.timestampExtractor
- the stateless timestamp extractor used for this source,
if not specified the default extractor defined in the configs will be usedname
- the unique name of the source used to reference this node when
adding processor children
.topicPattern
- regular expression pattern to match Kafka topics that this source is to consumepublic final TopologyBuilder addSource(TopologyBuilder.AutoOffsetReset offsetReset, TimestampExtractor timestampExtractor, java.lang.String name, java.util.regex.Pattern topicPattern)
default key deserializer
and
default value deserializer
specified in the
stream configuration
.offsetReset
- the auto offset reset policy value for this source if no committed offsets found;
acceptable values earliest or latest.timestampExtractor
- the stateless timestamp extractor used for this source,
if not specified the default extractor defined in the configs will be usedname
- the unique name of the source used to reference this node when
adding processor children
.topicPattern
- regular expression pattern to match Kafka topics that this source is to consumepublic final TopologyBuilder addSource(java.lang.String name, Deserializer keyDeserializer, Deserializer valDeserializer, java.lang.String... topics)
TimestampExtractor
as specified in the config
is used.name
- the unique name of the source used to reference this node when
adding processor children
keyDeserializer
- key deserializer used to read this source, if not specified the default
key deserializer defined in the configs will be usedvalDeserializer
- value deserializer used to read this source,
if not specified the default value deserializer defined in the configs will be usedtopics
- the name of one or more Kafka topics that this source is to consumeTopologyBuilderException
- if processor is already added or if topics have already been registered by another sourcepublic final TopologyBuilder addSource(TopologyBuilder.AutoOffsetReset offsetReset, java.lang.String name, TimestampExtractor timestampExtractor, Deserializer keyDeserializer, Deserializer valDeserializer, java.lang.String... topics)
offsetReset
- the auto offset reset policy to use for this stream if no committed offsets found;
acceptable values are earliest or latest.name
- the unique name of the source used to reference this node when
adding processor children
.timestampExtractor
- the stateless timestamp extractor used for this source,
if not specified the default extractor defined in the configs will be usedkeyDeserializer
- key deserializer used to read this source, if not specified the default
key deserializer defined in the configs will be usedvalDeserializer
- value deserializer used to read this source,
if not specified the default value deserializer defined in the configs will be usedtopics
- the name of one or more Kafka topics that this source is to consumeTopologyBuilderException
- if processor is already added or if topics have already been registered by another sourcepublic TopologyBuilder addGlobalStore(StateStoreSupplier<KeyValueStore> storeSupplier, java.lang.String sourceName, Deserializer keyDeserializer, Deserializer valueDeserializer, java.lang.String topic, java.lang.String processorName, ProcessorSupplier stateUpdateSupplier)
StateStore
to the topology. The StateStore
sources its data
from all partitions of the provided input topic. There will be exactly one instance of this
StateStore
per Kafka Streams instance.
A SourceNode
with the provided sourceName will be added to consume the data arriving
from the partitions of the input topic.
The provided ProcessorSupplier
will be used to create an ProcessorNode
that will
receive all records forwarded from the SourceNode
. This
ProcessorNode
should be used to keep the StateStore
up-to-date.
The default TimestampExtractor
as specified in the config
is used.
storeSupplier
- user defined state store suppliersourceName
- name of the SourceNode
that will be automatically addedkeyDeserializer
- the Deserializer
to deserialize keys withvalueDeserializer
- the Deserializer
to deserialize values withtopic
- the topic to source the data fromprocessorName
- the name of the ProcessorSupplier
stateUpdateSupplier
- the instance of ProcessorSupplier
public TopologyBuilder addGlobalStore(StateStoreSupplier<KeyValueStore> storeSupplier, java.lang.String sourceName, TimestampExtractor timestampExtractor, Deserializer keyDeserializer, Deserializer valueDeserializer, java.lang.String topic, java.lang.String processorName, ProcessorSupplier stateUpdateSupplier)
StateStore
to the topology. The StateStore
sources its data
from all partitions of the provided input topic. There will be exactly one instance of this
StateStore
per Kafka Streams instance.
A SourceNode
with the provided sourceName will be added to consume the data arriving
from the partitions of the input topic.
The provided ProcessorSupplier
will be used to create an ProcessorNode
that will
receive all records forwarded from the SourceNode
. This
ProcessorNode
should be used to keep the StateStore
up-to-date.
storeSupplier
- user defined state store suppliersourceName
- name of the SourceNode
that will be automatically addedtimestampExtractor
- the stateless timestamp extractor used for this source,
if not specified the default extractor defined in the configs will be usedkeyDeserializer
- the Deserializer
to deserialize keys withvalueDeserializer
- the Deserializer
to deserialize values withtopic
- the topic to source the data fromprocessorName
- the name of the ProcessorSupplier
stateUpdateSupplier
- the instance of ProcessorSupplier
public final TopologyBuilder addSource(java.lang.String name, Deserializer keyDeserializer, Deserializer valDeserializer, java.util.regex.Pattern topicPattern)
TimestampExtractor
as specified in the config
is used.name
- the unique name of the source used to reference this node when
adding processor children
keyDeserializer
- key deserializer used to read this source, if not specified the default
key deserializer defined in the configs will be usedvalDeserializer
- value deserializer used to read this source,
if not specified the default value deserializer defined in the configs will be usedtopicPattern
- regular expression pattern to match Kafka topics that this source is to consumeTopologyBuilderException
- if processor is already added or if topics have already been registered by namepublic final TopologyBuilder addSource(TopologyBuilder.AutoOffsetReset offsetReset, java.lang.String name, TimestampExtractor timestampExtractor, Deserializer keyDeserializer, Deserializer valDeserializer, java.util.regex.Pattern topicPattern)
offsetReset
- the auto offset reset policy to use for this stream if no committed offsets found;
acceptable values are earliest or latestname
- the unique name of the source used to reference this node when
adding processor children
.timestampExtractor
- the stateless timestamp extractor used for this source,
if not specified the default extractor defined in the configs will be usedkeyDeserializer
- key deserializer used to read this source, if not specified the default
key deserializer defined in the configs will be usedvalDeserializer
- value deserializer used to read this source,
if not specified the default value deserializer defined in the configs will be usedtopicPattern
- regular expression pattern to match Kafka topics that this source is to consumeTopologyBuilderException
- if processor is already added or if topics have already been registered by namepublic final TopologyBuilder addSource(TopologyBuilder.AutoOffsetReset offsetReset, java.lang.String name, Deserializer keyDeserializer, Deserializer valDeserializer, java.util.regex.Pattern topicPattern)
offsetReset
- the auto offset reset policy to use for this stream if no committed offsets found;
acceptable values are earliest or latestname
- the unique name of the source used to reference this node when
adding processor children
keyDeserializer
- key deserializer used to read this source, if not specified the default
key deserializer defined in the configs will be usedvalDeserializer
- value deserializer used to read this source,
if not specified the default value deserializer defined in the configs will be usedtopicPattern
- regular expression pattern to match Kafka topics that this source is to consumeTopologyBuilderException
- if processor is already added or if topics have already been registered by namepublic final TopologyBuilder addSink(java.lang.String name, java.lang.String topic, java.lang.String... parentNames)
default key serializer
and
default value serializer
specified in the
stream configuration
.name
- the unique name of the sinktopic
- the name of the Kafka topic to which this sink should write its recordsparentNames
- the name of one or more source or processor nodes whose output records this sink should consume
and write to its topicaddSink(String, String, StreamPartitioner, String...)
,
addSink(String, String, Serializer, Serializer, String...)
,
addSink(String, String, Serializer, Serializer, StreamPartitioner, String...)
public final TopologyBuilder addSink(java.lang.String name, java.lang.String topic, StreamPartitioner partitioner, java.lang.String... parentNames)
default key serializer
and
default value serializer
specified in the
stream configuration
.
The sink will also use the specified StreamPartitioner
to determine how records are distributed among
the named Kafka topic's partitions. Such control is often useful with topologies that use
state stores
in its processors. In most other cases, however, a partitioner needs not be specified and Kafka will automatically distribute
records among partitions using Kafka's default partitioning logic.
name
- the unique name of the sinktopic
- the name of the Kafka topic to which this sink should write its recordspartitioner
- the function that should be used to determine the partition for each record processed by the sinkparentNames
- the name of one or more source or processor nodes whose output records this sink should consume
and write to its topicaddSink(String, String, String...)
,
addSink(String, String, Serializer, Serializer, String...)
,
addSink(String, String, Serializer, Serializer, StreamPartitioner, String...)
public final TopologyBuilder addSink(java.lang.String name, java.lang.String topic, Serializer keySerializer, Serializer valSerializer, java.lang.String... parentNames)
name
- the unique name of the sinktopic
- the name of the Kafka topic to which this sink should write its recordskeySerializer
- the key serializer
used when consuming records; may be null if the sink
should use the default key serializer
specified in the
stream configuration
valSerializer
- the value serializer
used when consuming records; may be null if the sink
should use the default value serializer
specified in the
stream configuration
parentNames
- the name of one or more source or processor nodes whose output records this sink should consume
and write to its topicaddSink(String, String, String...)
,
addSink(String, String, StreamPartitioner, String...)
,
addSink(String, String, Serializer, Serializer, StreamPartitioner, String...)
public final <K,V> TopologyBuilder addSink(java.lang.String name, java.lang.String topic, Serializer<K> keySerializer, Serializer<V> valSerializer, StreamPartitioner<? super K,? super V> partitioner, java.lang.String... parentNames)
name
- the unique name of the sinktopic
- the name of the Kafka topic to which this sink should write its recordskeySerializer
- the key serializer
used when consuming records; may be null if the sink
should use the default key serializer
specified in the
stream configuration
valSerializer
- the value serializer
used when consuming records; may be null if the sink
should use the default value serializer
specified in the
stream configuration
partitioner
- the function that should be used to determine the partition for each record processed by the sinkparentNames
- the name of one or more source or processor nodes whose output records this sink should consume
and write to its topicTopologyBuilderException
- if parent processor is not added yet, or if this processor's name is equal to the parent's nameaddSink(String, String, String...)
,
addSink(String, String, StreamPartitioner, String...)
,
addSink(String, String, Serializer, Serializer, String...)
public final TopologyBuilder addProcessor(java.lang.String name, ProcessorSupplier supplier, java.lang.String... parentNames)
name
- the unique name of the processor nodesupplier
- the supplier used to obtain this node's Processor
instanceparentNames
- the name of one or more source or processor nodes whose output records this processor should receive
and processTopologyBuilderException
- if parent processor is not added yet, or if this processor's name is equal to the parent's namepublic final TopologyBuilder addStateStore(StateStoreSupplier supplier, java.lang.String... processorNames)
supplier
- the supplier used to obtain this state store StateStore
instanceTopologyBuilderException
- if state store supplier is already addedpublic final TopologyBuilder connectProcessorAndStateStores(java.lang.String processorName, java.lang.String... stateStoreNames)
processorName
- the name of the processorstateStoreNames
- the names of state stores that the processor usesprotected final TopologyBuilder connectSourceStoreAndTopic(java.lang.String sourceStoreName, java.lang.String topic)
public final TopologyBuilder connectProcessors(java.lang.String... processorNames)
processorNames
- the name of the processorsTopologyBuilderException
- if less than two processors are specified, or if one of the processors is not added yetpublic final TopologyBuilder addInternalTopic(java.lang.String topicName)
topicName
- the name of the topicpublic final TopologyBuilder copartitionSources(java.util.Collection<java.lang.String> sourceNodes)
sourceNodes
- a set of source node namespublic java.util.Map<java.lang.Integer,java.util.Set<java.lang.String>> nodeGroups()
public ProcessorTopology build(java.lang.Integer topicGroupId)
KafkaStreams.KafkaStreams(TopologyBuilder, org.apache.kafka.streams.StreamsConfig)
constructor.public ProcessorTopology buildGlobalStateTopology()
public java.util.Map<java.lang.String,StateStore> globalStateStores()
StateStore
s that are part of the
topologyStateStore
spublic java.util.Map<java.lang.Integer,TopologyBuilder.TopicsInfo> topicGroups()
public java.util.regex.Pattern earliestResetTopicsPattern()
public java.util.regex.Pattern latestResetTopicsPattern()
public java.util.Map<java.lang.String,java.util.List<java.lang.String>> stateStoreNameToSourceTopics()
public java.util.Collection<java.util.Set<java.lang.String>> copartitionGroups()
public SubscriptionUpdates subscriptionUpdates()
public java.util.regex.Pattern sourceTopicPattern()
public void updateSubscriptions(SubscriptionUpdates subscriptionUpdates, java.lang.String threadId)