Kafka常用命令操作

Create a topic

Let’s create a topic named “test” with a single partition and only one replica:

> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test

We can now see that topic if we run the list topic command:

> bin/kafka-topics.sh --list --bootstrap-server localhost:9092

test

Start some messages

Kafka comes with a command line client that will take input from a file or from standard input and send it out as messages to the Kafka cluster. By default, each line will be sent as a separate message.
Run the producer and then type a few messages into the console to send to the server.

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

This is a message
This is another message

Start a consumer

Kafka also has a command line consumer that will dump out messages to standard output.

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

This is a message
This is another message

Understand random characters in the log file of kafka

> bin/kafka-run-class.sh kafka.tools.DumpLogSegments --deep-iteration --print-data-log --files topic/*.log|index|timeindex

Inspect kafka group

> bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group hiway.group.projection --describe -- state
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
hiway.topic.order 0 6 6 0 consumer-21-d8cb9666-2d54-408b-82a6-0480a0b5a10b /172.168.5.9 consumer-21
hiway.topic.aps-billing 0 10 10 0 consumer-21-fbea0085-d5fa-4645-a2cf-c3b4e9f2837b /172.168.0.7 consumer-21
hiway.topic.account 0 23 23 0 consumer-15-24e95c62-03f9-4051-8cd0-8b40d691b0c9 /172.168.0.7 consumer-15

Lookup for assigned of partition

> bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --members --group hiway.group.projection
CONSUMER-ID HOST CLIENT-ID #PARTITIONS
consumer-27-d56cb0ba-90a8-498a-b019-9f71c6046e03 /172.168.0.7 consumer-27 0
consumer-6-cacd38dc-dffb-456c-b0d2-5badbb06f108 /172.168.0.7 consumer-6 0
consumer-24-ba6ef6b7-d0fe-4104-a0e9-5bcda354cda8 /172.168.0.7 consumer-24 1
consumer-21-d8cb9666-2d54-408b-82a6-0480a0b5a10b /172.168.5.9 consumer-21 1
consumer-21-fbea0085-d5fa-4645-a2cf-c3b4e9f2837b /172.168.0.7 consumer-21 1

關注公衆號:JAVA九點半課堂,回覆【資料】獲取2T最新技術資料,每天還有技術分享,我們一起進步,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章