Package com.bakdata.kafka.util
Class TopicClient
- java.lang.Object
-
- com.bakdata.kafka.util.TopicClient
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class TopicClient extends java.lang.Object implements java.io.CloseableThis class offers helpers to interact with Kafka topics.
-
-
Constructor Summary
Constructors Constructor Description TopicClient(@NonNull org.apache.kafka.clients.admin.AdminClient adminClient, @NonNull java.time.Duration timeout)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()static TopicClientcreate(java.util.Map<java.lang.String,java.lang.Object> configs, java.time.Duration timeout)Creates a newTopicClientusing the specified configuration.static TopicClientcreate(java.util.Properties configs, java.time.Duration timeout)Creates a newTopicClientusing the specified configuration.voidcreateIfNotExists(java.lang.String topicName, TopicSettings settings, java.util.Map<java.lang.String,java.lang.String> config)Creates a new Kafka topic with the specified number of partitions if it does not yet exist.voidcreateTopic(java.lang.String topicName, TopicSettings settings, java.util.Map<java.lang.String,java.lang.String> config)Creates a new Kafka topic with the specified number of partitions.voiddeleteTopic(java.lang.String topicName)Delete a Kafka topic.voiddeleteTopicIfExists(java.lang.String topic)Delete a Kafka topic only if it exists.TopicSettingsdescribe(java.lang.String topicName)Describes the current configuration of a Kafka topic.booleanexists(java.lang.String topicName)Checks whether a Kafka topic exists.java.util.Collection<java.lang.String>listTopics()List Kafka topics.
-
-
-
Method Detail
-
create
public static TopicClient create(java.util.Map<java.lang.String,java.lang.Object> configs, java.time.Duration timeout)
Creates a newTopicClientusing the specified configuration.- Parameters:
configs- properties passed toAdminClient.create(Map)timeout- timeout for waiting for Kafka admin calls- Returns:
TopicClient
-
create
public static TopicClient create(java.util.Properties configs, java.time.Duration timeout)
Creates a newTopicClientusing the specified configuration.- Parameters:
configs- properties passed toAdminClient.create(Properties)timeout- timeout for waiting for Kafka admin calls- Returns:
TopicClient
-
createIfNotExists
public void createIfNotExists(java.lang.String topicName, TopicSettings settings, java.util.Map<java.lang.String,java.lang.String> config)Creates a new Kafka topic with the specified number of partitions if it does not yet exist. If the topic exists, its configuration is not updated.- Parameters:
topicName- the topic namesettings- settings for number of partitions and replicationFactorconfig- topic configuration- See Also:
createTopic(String, TopicSettings, Map),exists(String)
-
deleteTopic
public void deleteTopic(java.lang.String topicName)
Delete a Kafka topic.- Parameters:
topicName- the topic name
-
describe
public TopicSettings describe(java.lang.String topicName)
Describes the current configuration of a Kafka topic.- Parameters:
topicName- the topic name- Returns:
- settings of topic including number of partitions and replicationFactor
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
exists
public boolean exists(java.lang.String topicName)
Checks whether a Kafka topic exists.- Parameters:
topicName- the topic name- Returns:
- whether a Kafka topic with the specified name exists or not
-
createTopic
public void createTopic(java.lang.String topicName, TopicSettings settings, java.util.Map<java.lang.String,java.lang.String> config)Creates a new Kafka topic with the specified number of partitions.- Parameters:
topicName- the topic namesettings- settings for number of partitions and replicationFactorconfig- topic configuration
-
listTopics
public java.util.Collection<java.lang.String> listTopics()
List Kafka topics.- Returns:
- name of all existing Kafka topics
-
deleteTopicIfExists
public void deleteTopicIfExists(java.lang.String topic)
Delete a Kafka topic only if it exists.- Parameters:
topic- the topic name
-
-