{"id":63375,"date":"2020-08-28T06:08:04","date_gmt":"2020-08-28T06:08:04","guid":{"rendered":"https:\/\/www.baeldung.com\/?p=86282"},"modified":"2020-08-28T06:08:04","modified_gmt":"2020-08-28T06:08:04","slug":"listing-kafka-topics","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2020\/08\/28\/listing-kafka-topics\/","title":{"rendered":"Listing Kafka Topics"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<h2 data-id=\"overview-1\">1. Overview<\/h2>\n<div class=\"bd-anchor\" id=\"overview-1\"><\/div>\n<p>In this quick tutorial, we&#8217;re going to see how we can list all topics in an Apache Kafka cluster.<\/p>\n<p>First, we&#8217;ll set up a single-node Apache Kafka and <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-zookeeper\">Zookeeper<\/a> cluster. Then, we&#8217;ll ask that cluster about its topics.<\/p>\n<h2 data-id=\"setting-up-kafka\">2. Setting Up Kafka<\/h2>\n<div class=\"bd-anchor\" id=\"setting-up-kafka\"><\/div>\n<p>Before listing all the topics in a Kafka cluster, let&#8217;s set up a test single-node Kafka cluster in three steps:<\/p>\n<ul>\n<li>Downloading Kafka and Zookeeper<\/li>\n<li>Starting Zookeeper Service<\/li>\n<li>Starting Kafka Service<\/li>\n<\/ul>\n<p>First, we should make sure to download the right Kafka version from the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/kafka.apache.org\/downloads\">Apache<\/a>\u00a0site.\u00a0Once the download finishes, we should extract the downloaded archive:<\/p>\n<pre><code class=\"language-bash\">$ tar xvf kafka_2.13-2.6.0.tgz<\/code><\/pre>\n<p>Kafka is using Apache Zookeeper to manage its cluster metadata, so we need a running Zookeeper cluster.<\/p>\n<p>For test purposes, we can run a single-node Zookeeper instance using the <em>zookeeper-server-start.sh<\/em>\u00a0script in the\u00a0<em>bin<\/em>\u00a0directory:<\/p>\n<pre><code class=\"language-bash\">$ cd kafka_2.13-2.6.0 # extracted directory\r\n$ .\/bin\/zookeeper-server-start.sh config\/zookeeper.properties<\/code><\/pre>\n<p>This will start a Zookeeper service listening on port 2181. After this, we can use another script to run the Kafka server:<\/p>\n<pre><code class=\"language-bash\">$ .\/bin\/kafka-server-start.sh config\/server.properties<\/code><\/pre>\n<p>After a while, a Kafka broker will start. Let&#8217;s <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/kafka.apache.org\/documentation\/#basic_ops_add_topic\">add a few topics<\/a> to this simple cluster:<\/p>\n<pre><code class=\"language-bash\">$ bin\/kafka-topics.sh --create --topic users.registrations --replication-factor 1 \\\r\n  --partitions 2  --zookeeper localhost:2181\r\n$ bin\/kafka-topics.sh --create --topic users.verfications --replication-factor 1 \\\r\n  --partitions 2  --zookeeper localhost:2181<\/code><\/pre>\n<p>Now that everything is ready, let&#8217;s see how we can list Kafka topics.<\/p>\n<h2 data-id=\"listing-topics\">3. Listing Topics<\/h2>\n<div class=\"bd-anchor\" id=\"listing-topics\"><\/div>\n<p>To list all Kafka topics in a cluster, we can use the\u00a0<em>bin\/kafka-topics.sh\u00a0<\/em>shell script bundled in the downloaded Kafka distribution. <strong>All we have to do is to pass the\u00a0<em>&#8211;list\u00a0<\/em>option along with the information about the cluster<\/strong>. For instance, we can pass the Zookeeper service address:<\/p>\n<pre><code class=\"language-bash\">$ bin\/kafka-topics.sh --list --zookeeper localhost:2181\r\nusers.registrations\r\nusers.verfications<\/code><\/pre>\n<p>As shown above,\u00a0the\u00a0<em>&#8211;list<\/em> option tells the <em>kafka-topics.sh<\/em>\u00a0shell script to list all the topics. In this case, we have two topics to store user-related events. If there is no topic in the cluster, then the command will return silently without any result.<\/p>\n<p>Also, <strong>in order to talk to the Kafka cluster, we need to pass the Zookeeper service URL using the <em>&#8211;zookeeper\u00a0<\/em>option<\/strong>.<\/p>\n<p><strong>It&#8217;s even possible to pass the Kafka cluster address directly using the <em>&#8211;bootstrap-server<\/em> option<\/strong>:<\/p>\n<pre><code class=\"language-bash\">$ .\/bin\/kafka-topics.sh --bootstrap-server=localhost:9092 --list\r\nusers.registrations\r\nusers.verfications<\/code><\/pre>\n<p>Our single-instance Kafka cluster listens to the 9092 port, so we specified <em>&#8220;localhost:9092&#8221;<\/em>\u00a0as the bootstrap server. Put simply, bootstrap servers are Kafka brokers.<\/p>\n<p>If we don&#8217;t pass the information necessary to talk to a Kafka cluster, the <em>kafka-topics.sh<\/em>\u00a0shell script will complain with an error:<\/p>\n<pre><code class=\"language-bash\">$ .\/bin\/kafka-topics.sh --list\r\nException in thread \"main\" java.lang.IllegalArgumentException: Only one of --bootstrap-server or --zookeeper must be specified\r\n        at kafka.admin.TopicCommand$TopicCommandOptions.checkArgs(TopicCommand.scala:721)\r\n        at kafka.admin.TopicCommand$.main(TopicCommand.scala:52)\r\n        at kafka.admin.TopicCommand.main(TopicCommand.scala)<\/code><\/pre>\n<p>As shown above, the shell scripts require us to pass either the <em>&#8211;bootstrap-server<\/em>\u00a0or\u00a0<em>&#8211;zookeeper<\/em>\u00a0option.<\/p>\n<h2 data-id=\"topic-details\">4. Topic Details<\/h2>\n<div class=\"bd-anchor\" id=\"topic-details\"><\/div>\n<p>Once we&#8217;ve found a list of topics, we can take a peek at the details of one specific topic. To do that, <strong>we can use the\u00a0<em>&#8220;<\/em><em>&#8211;describe &#8211;topic &lt;topic name&gt;&#8221;<\/em>\u00a0combination of options<\/strong>:<\/p>\n<pre><code class=\"language-bash\">$ .\/bin\/kafka-topics.sh --bootstrap-server=localhost:9092 --describe --topic users.registrations\r\nTopic: users.registrations      PartitionCount: 2       ReplicationFactor: 1    Configs: segment.bytes=1073741824\r\n        Topic: users.registrations      Partition: 0    Leader: 0       Replicas: 0     Isr: 0\r\n        Topic: users.registrations      Partition: 1    Leader: 0       Replicas: 0     Isr: 0<\/code><\/pre>\n<p>These details include information about the specified topic such as the number of partitions and replicas, among others. Similar to other commands, we must pass the cluster information or Zookeeper address. Otherwise, we won&#8217;t be able to talk to the cluster.<\/p>\n<h2 data-id=\"conclusion-2\">5. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion-2\"><\/div>\n<p>In this short tutorial, we learned how to list all topics in a Kafka cluster. Along the way, we saw how to set up a simple, single-node Kafka cluster.<\/p>\n<p>Currently, Apache Kafka is using Zookeeper to manage its cluster metadata. This, however, will change shortly as part of <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/cwiki.apache.org\/confluence\/display\/KAFKA\/KIP-500%3A+Replace+ZooKeeper+with+a+Self-Managed+Metadata+Quorum\">KIP-500<\/a>, as Kafka is going to have its own metadata quorum.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/634675368\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/634675368\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/634675368\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/634675368\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/634675368\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/634675368\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/634675368\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/devops\/kafka-list-topics#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/devops\/kafka-list-topics\/feed\/\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Learn how to list all kafka topics using the command line.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/634675368\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>&nbsp;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/634675368\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\"><\/a>&nbsp;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/634675368\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>&nbsp;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/634675368\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\"><\/a>&nbsp;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/634675368\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>&nbsp;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/634675368\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>&nbsp;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/devops\/kafka-list-topics#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&nbsp;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/devops\/kafka-list-topics\/feed\/\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&nbsp;<\/div>\n<\/div>","protected":false},"author":760,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Listing Kafka Topics - ITTeacherITFreelance.hk","description":"Learn how to list all kafka topics using the command line. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"},"footnotes":""},"categories":[6],"tags":[4913],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/63375"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/760"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=63375"}],"version-history":[{"count":8,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/63375\/revisions"}],"predecessor-version":[{"id":66049,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/63375\/revisions\/66049"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=63375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=63375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=63375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}