Package io.debezium.kafka
Class KafkaCluster
java.lang.Object
io.debezium.kafka.KafkaCluster
An embeddable cluster of Kafka servers and a single Zookeeper server. This may be useful when creating a complete environment
within a single process, but doing so offers limited durability and fault tolerance compared to the normal approach of
using an external cluster of Kafka servers and Zookeeper servers with proper replication and fault tolerance.
- Author:
- Randall Hauch
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA simple interactive Kafka consumer for use with the cluster.static interfaceA simple interactive Kafka producer for use with the cluster.classA set of methods to use a running KafkaCluster. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Filestatic final booleanstatic final booleanprivate booleanprivate booleanprivate Propertiesprivate final ConcurrentMap<Integer,KafkaServer> private static final org.slf4j.Loggerprivate final AtomicLongprivate booleanprivate intprivate final ZookeeperServer -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddBrokers(int count) Add a number of new Kafka broker to the cluster.Get the list of brokers.voidcreateTopic(String topic, int numPartitions, int replicationFactor) Create the specified topic.voidcreateTopics(int numPartitions, int replicationFactor, String... topics) Create the specified topics.voidcreateTopics(int numPartitions, int replicationFactor, Set<String> topics) Create the specified topics.voidcreateTopics(String... topics) Create the specified topics.voidcreateTopics(Set<String> topics) Create the specified topics.deleteDataPriorToStartup(boolean delete) Specify whether the data is to be deleted prior tostartup().deleteDataUponShutdown(boolean delete) Specify whether the data is to be deleted uponshutdown().booleanDetermine if the cluster is running.(package private) voidonEachDirectory(Consumer<File> consumer) Perform the supplied function on each directory used by this cluster.shutdown()Shutdown the embedded Zookeeper server and the Kafka serversin the cluster.private voidshutdownReliably(KafkaServer server) startup()Start the embedded Zookeeper server and the Kafka serversin the cluster.useTo()Obtain the interface for using this cluster.usingDirectory(File dataDir) Set the parent directory where the brokers logs and server's logs and snapshots will be kept.withKafkaConfiguration(Properties properties) Set the configuration properties for each of the brokers.withPorts(int zkPort, int firstKafkaPort) Set the port numbers for Zookeeper and the Kafka brokers.intzkPort()Get the Zookeeper port.
-
Field Details
-
DEFAULT_DELETE_DATA_UPON_SHUTDOWN
public static final boolean DEFAULT_DELETE_DATA_UPON_SHUTDOWN- See Also:
-
DEFAULT_DELETE_DATA_PRIOR_TO_STARTUP
public static final boolean DEFAULT_DELETE_DATA_PRIOR_TO_STARTUP- See Also:
-
LOGGER
private static final org.slf4j.Logger LOGGER -
kafkaServers
-
zkServer
-
dataDir
-
deleteDataUponShutdown
private volatile boolean deleteDataUponShutdown -
deleteDataPriorToStartup
private volatile boolean deleteDataPriorToStartup -
running
private volatile boolean running -
kafkaConfig
-
startingKafkaPort
private volatile int startingKafkaPort -
nextKafkaPort
-
-
Constructor Details
-
KafkaCluster
public KafkaCluster()Create a new embedded cluster.
-
-
Method Details
-
deleteDataUponShutdown
Specify whether the data is to be deleted uponshutdown().- Parameters:
delete- true if the data is to be deleted upon shutdown, or false otherwise- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is running
-
deleteDataPriorToStartup
Specify whether the data is to be deleted prior tostartup().- Parameters:
delete- true if the data is to be deleted upon shutdown, or false otherwise- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is running
-
addBrokers
Add a number of new Kafka broker to the cluster. The broker IDs will be generated.- Parameters:
count- the number of new brokers to add- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is running
-
usingDirectory
Set the parent directory where the brokers logs and server's logs and snapshots will be kept.- Parameters:
dataDir- the parent directory for the server's logs and snapshots; may be null if a temporary directory will be used- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is runningIllegalArgumentException- if the supplied file is not a directory or not writable
-
withKafkaConfiguration
Set the configuration properties for each of the brokers. This method does nothing if the supplied properties are null or empty.- Parameters:
properties- the Kafka configuration properties- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is running
-
withPorts
Set the port numbers for Zookeeper and the Kafka brokers.- Parameters:
zkPort- the port number that Zookeeper should use; may be -1 if an available port should be discoveredfirstKafkaPort- the port number for the first Kafka broker (additional brokers will use subsequent port numbers); may be -1 if available ports should be discovered- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is running
-
isRunning
public boolean isRunning()Determine if the cluster is running.- Returns:
- true if the cluster is running, or false otherwise
-
startup
Start the embedded Zookeeper server and the Kafka serversin the cluster. This method does nothing if the cluster is already running.- Returns:
- this instance to allow chaining methods; never null
- Throws:
IOException- if there is an error during startup
-
shutdown
Shutdown the embedded Zookeeper server and the Kafka serversin the cluster. This method does nothing if the cluster is not running.- Returns:
- this instance to allow chaining methods; never null
-
createTopics
Create the specified topics.- Parameters:
topics- the names of the topics to create- Throws:
IllegalStateException- if the cluster is not running
-
createTopics
Create the specified topics.- Parameters:
topics- the names of the topics to create- Throws:
IllegalStateException- if the cluster is not running
-
createTopics
Create the specified topics.- Parameters:
numPartitions- the number of partitions for each topicreplicationFactor- the replication factor for each topictopics- the names of the topics to create
-
createTopics
Create the specified topics.- Parameters:
numPartitions- the number of partitions for each topicreplicationFactor- the replication factor for each topictopics- the names of the topics to create
-
createTopic
Create the specified topic.- Parameters:
topic- the name of the topic to createnumPartitions- the number of partitions for the topicreplicationFactor- the replication factor for the topic
-
onEachDirectory
Perform the supplied function on each directory used by this cluster.- Parameters:
consumer- the consumer function; may not be null
-
brokerList
Get the list of brokers.- Returns:
- the broker list
-
zkPort
public int zkPort()Get the Zookeeper port.- Returns:
- the Zookeeper port
-
shutdownReliably
-
useTo
Obtain the interface for using this cluster.- Returns:
- the usage interface; never null
- Throws:
IllegalStateException- if the cluster is not running
-