{"id":7931,"date":"2022-01-25T14:56:50","date_gmt":"2022-01-25T14:56:50","guid":{"rendered":"https:\/\/www.npntraining.com\/blog\/?p=7931"},"modified":"2022-01-25T14:57:29","modified_gmt":"2022-01-25T14:57:29","slug":"kafka-hands-on-commands","status":"publish","type":"post","link":"https:\/\/www.npntraining.com\/blog\/kafka-hands-on-commands\/","title":{"rendered":"Kafka Commands"},"content":{"rendered":"<h1>Kafka Commands<\/h1>\n<h2>Starting  Zookeeper &amp; Kafka<\/h2>\n<pre><code class=\"language-shell\">[npntraining ~]$ cd \\opt\\kafka\\zookeeper-3.5.6-bin\\bin\n[npntraining ~]$ zkserver.sh<\/code><\/pre>\n<p><strong>Starting Kafka<\/strong><\/p>\n<pre><code class=\"language-shell\">[npntraining ~]$ cd \\opt\\kafka\\kafka_2.12-2.4.0\\bin\\\n[npntraining ~]$ kafka-server-start.sh \\opt\\kafka\\kafka_2.12-2.4.0\\config\\server.properties<\/code><\/pre>\n<h2>Kafka Topic<\/h2>\n<p>You can create a new Kafka topic named <code>simple-producer-consumer<\/code> as follows:<\/p>\n<pre><code class=\"language-shell\">[npntraining ~]$ cd \\opt\\kafka\\kafka_2.12-2.4.0\\bin\\\n[npntraining ~]$ kafka-topics.sh --create --zookeeper localhost:2181 --partitions 1 --replication-factor 1 --topic simple-producer-consumer<\/code><\/pre>\n<p><strong>List all topics<\/strong><\/p>\n<p>You can verify that the <code>simple-producer-consumer<\/code> topic was successfully created by listing all available topics:<\/p>\n<pre><code class=\"language-shell\">[npntraining ~]$ kafka-topics.sh --list --zookeeper localhost:2181<\/code><\/pre>\n<p><strong>Deleting Topic<\/strong><\/p>\n<p>You can delete a topic named <code>simple-producer-consumer<\/code> as follows:<\/p>\n<pre><code class=\"language-scala\">[npntraining ~]$ kafka-topics.sh --delete --zookeeper localhost:2181 --topic simple-producer-consumer\n\n[npntraining ~]$ kafka-topics.sh --delete --bootstrap-server localhost:9092 --topic simple-producer-consumer<\/code><\/pre>\n<p><strong>Describing Topic<\/strong><\/p>\n<p>You can find more details about a topic named <code>simple-producer-consumer<\/code><\/p>\n<pre><code class=\"language-shell\">[npntraining ~]$ kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic &lt;topic-name&gt; <\/code><\/pre>\n<p><strong>Creating topic with multiple partitions<\/strong><\/p>\n<p>You can add more partitions as follows:<\/p>\n<pre><code class=\"language-shell\">[npntraining ~]$ kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic simple-producer-consumer<\/code><\/pre>\n<p><strong>Under-replicated partitions<\/strong><\/p>\n<p>You can see the under-replicated partitions for all topics as follows:<\/p>\n<pre><code class=\"language-shell\">[npntraining ~]$ kafka-topics.sh --zookeeper localhost:2181\/kafka-cluster --describe --under-replicated-partitions<\/code><\/pre>\n<h2>Producing and Consuming Messages<\/h2>\n<p><strong>Starting Kafka Console Producer<\/strong><\/p>\n<p>You can produce messages from standard input as follows:<\/p>\n<pre><code class=\"language-shell\">[npntraining ~]$ kafka-console-producer.sh --broker-list localhost:9092 --topic simple-producer-consumer\nMessage 01<\/code><\/pre>\n<p><strong>Starting Kafka Console Consumer<\/strong><\/p>\n<pre><code class=\"language-shell\">[npntraining ~]$ kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic simple-producer-consumer\nMessage01<\/code><\/pre>\n<p>You can begin a consumer from the beginning of the log as follows:<\/p>\n<pre><code class=\"language-shell\">[npntraining ~]$ kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic simple-producer-consumer --from-beginning<\/code><\/pre>\n<p><strong>Running JAR from Command Line<\/strong><\/p>\n<pre><code class=\"language-shell\">java -cp kafka-producer-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.npntraining.SimpleProducer first-topic\njava -cp kafka-producer-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.npntraining.SimpleConsumer my-first-topic<\/code><\/pre>\n<h2>Producers<\/h2>\n<p>You can begin a consumer from the beginning of the log as follows:<\/p>\n<pre><code class=\"language-shell\">kafka-console-producer --broker-list localhost:9092 --topic test &lt; messages.txt<\/code><\/pre>\n<p>You can produce Avro messages as follows:<\/p>\n<pre><code class=\"language-shell\">kafka-avro-console-producer --broker-list localhost:9092 --topic my.Topic --property value.schema=&#039;{&quot;type&quot;:&quot;record&quot;,&quot;name&quot;:&quot;myrecord&quot;,&quot;fields&quot;:[{&quot;name&quot;:&quot;name&quot;,&quot;type&quot;:&quot;string&quot;}]}&#039; --property schema.registry.url=http:\/\/localhost:8081<\/code><\/pre>\n<p>You can enter a few new values from the console as follows:<\/p>\n<pre><code class=\"language-shell\">{&quot;name&quot;: &quot;Naveen&quot;}<\/code><\/pre>\n<h2>Consumers<\/h2>\n<p>You can consume a single message as follows:<\/p>\n<pre><code class=\"language-shell\">kafka-console-consumer --bootstrap-server localhost:9092 --topic my-topic  --max-messages 1<\/code><\/pre>\n<p>You can consume 10 Avro messages from a topic named <code>position-reports<\/code> as follows:<\/p>\n<pre><code class=\"language-shell\">kafka-avro-console-consumer --topic position-reports --new-consumer --bootstrap-server localhost:9092 --from-beginning --property schema.registry.url=localhost:8081 --max-messages 10<\/code><\/pre>\n<p>You can consume all existing Avro messages from a topic named <code>position-reports<\/code> as follows:<\/p>\n<pre><code class=\"language-shell\">kafka-avro-console-consumer --topic position-reports --new-consumer --bootstrap-server localhost:9092 --from-beginning --property schema.registry.url=localhost:8081<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Kafka Commands Starting Zookeeper &amp; Kafka [npntraining ~]$ cd \\opt\\kafka\\zookeeper-3.5.6-bin\\bin [npntraining ~]$ zkserver.sh Starting Kafka [npntraining ~]$ cd \\opt\\kafka\\kafka_2.12-2.4.0\\bin\\ [npntraining ~]$ kafka-server-start.sh \\opt\\kafka\\kafka_2.12-2.4.0\\config\\server.properties Kafka Topic You can create a new&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7931","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts\/7931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/comments?post=7931"}],"version-history":[{"count":2,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts\/7931\/revisions"}],"predecessor-version":[{"id":7933,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts\/7931\/revisions\/7933"}],"wp:attachment":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/media?parent=7931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/categories?post=7931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/tags?post=7931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}