Class TopicClient

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public final class TopicClient
    extends java.lang.Object
    implements java.io.Closeable
    This 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
      void close()  
      static TopicClient create​(java.util.Map<java.lang.String,​java.lang.Object> configs, java.time.Duration timeout)
      Creates a new TopicClient using the specified configuration.
      static TopicClient create​(java.util.Properties configs, java.time.Duration timeout)
      Creates a new TopicClient using the specified configuration.
      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.
      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.
      void deleteTopic​(java.lang.String topicName)
      Delete a Kafka topic.
      void deleteTopicIfExists​(java.lang.String topic)
      Delete a Kafka topic only if it exists.
      TopicSettings describe​(java.lang.String topicName)
      Describes the current configuration of a Kafka topic.
      boolean exists​(java.lang.String topicName)
      Checks whether a Kafka topic exists.
      java.util.Collection<java.lang.String> listTopics()
      List Kafka topics.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TopicClient

        public TopicClient​(@NonNull
                           @NonNull org.apache.kafka.clients.admin.AdminClient adminClient,
                           @NonNull
                           @NonNull java.time.Duration timeout)
    • Method Detail

      • create

        public static TopicClient create​(java.util.Map<java.lang.String,​java.lang.Object> configs,
                                         java.time.Duration timeout)
        Creates a new TopicClient using the specified configuration.
        Parameters:
        configs - properties passed to AdminClient.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 new TopicClient using the specified configuration.
        Parameters:
        configs - properties passed to AdminClient.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 name
        settings - settings for number of partitions and replicationFactor
        config - 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:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.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 name
        settings - settings for number of partitions and replicationFactor
        config - 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