如何查看卡夫卡留言

有没有什么方法可以查看发送给卡夫卡的给定主题的信息内容?如果可能的话,可以说一些像查看这个主题的最后5条消息之类的话。

211730 次浏览

You can use console consumer to view messages produced on some topic:

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

Use the Kafka consumer provided by Kafka :

bin/kafka-console-consumer.sh --bootstrap-server BROKERS --topic TOPIC_NAME

It will display the messages as it will receive it. Add --from-beginning if you want to start from the beginning.

If you doing from windows folder, I mean if you are using the kafka from windows machine

kafka-console-consumer.bat --bootstrap-server localhost:9092 --<topic-name> test --from-beginning

If you're wondering why the original answer is not working. Well it might be that you're not in the home directory. Try this:

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

Old version includes kafka-simple-consumer-shell.sh (https://kafka.apache.org/downloads#1.1.1) which is convenient since we do not need cltr+c to exit.

For example

kafka-simple-consumer-shell.sh --broker-list $BROKERIP:$BROKERPORT --topic $TOPIC1 --property print.key=true --property key.separator=":"  --no-wait-at-logend

On server where your admin run kafka find kafka-console-consumer.sh by command find . -name kafka-console-consumer.sh then go to that directory and run for read message from your topic

./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning --max-messages 10

note that in topic may be many messages in that case I use --max-messages key

For me the easiest way is, if you are using a JetBrains IDE (I don't know if the others has that plugin), download Big Data Tools plugin.

It will be added to right toolbar. You can connect to your Kafka Server. If it is on localhost the URL of the server is set to default to 127.0.0.1:9092.

After connected successfully, the Kafka will be added your bottom toolbar. Click it and click the Add Consumer button. Select your topic to consume, and start consuming.

A simple terminal-based tool you can use is kcat.

# Display values
kcat -C -b <bootstrap_server_host:port> -t <topic>


# Display key and values separated by ':'
kcat -C -K : -b <bootstrap_server_host:port> -t <topic>

But it's limited to string deserialization since it doesn't support other options like protobuf or avro.