简体   繁体   English

Kafka:如何显示偏移量

[英]Kafka: How to Display Offsets

I'm super new to Kafka.我对卡夫卡非常陌生。 I've installed kafka and zookeeper using homebrew on my mac, and I'm playing around with the quickstart guide .我已经在我的 mac 上使用自制软件安装了 kafka 和 zookeeper,我正在玩快速入门指南

I've been able to push messages onto Kafka using the following command and STDIN我已经能够使用以下命令和 STDIN 将消息推送到 Kafka

kafka-console-producer --broker-list localhost:9092 --topic test

and I can read things off using我可以使用

kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning

What's not clear to me is how I use offsets.我不清楚的是我如何使用偏移量。 It's my understanding that each message added to a topic will have a numerical, incremental offset value.我的理解是,添加到主题的每条消息都会有一个数字增量偏移值。 However, if I try to do something like this但是,如果我尝试做这样的事情

kafka-console-consumer --bootstrap-server localhost:9092 --topic test --offset 1

I get a non-zero status code and shows me no messages (other than the usual help/usage information)我得到一个非零状态代码并且没有显示任何消息(除了通常的帮助/使用信息)

I also can't get the latest or earliest keywords to work我也无法使用最新最早的关键字

kafka-console-consumer --bootstrap-server localhost:9092 --topic test --offset earliest
kafka-console-consumer --bootstrap-server localhost:9092 --topic test --offset latest

Both of the above also return non-zero status codes.以上两者也返回非零状态代码。

Do I fundamentally misunderstand offsets?我从根本上误解了偏移量吗? If not, is there a way to list all messages with their offsets?如果没有,有没有办法列出所有带有偏移量的消息? Finally -- what's the simpliest example of the --offset flag for the kafka-console-consumer ?最后 - kafka-console-consumer--offset标志的最简单示例是什么?

If you looked at the output after you gave the offset value, it would have said that you needed to specify a partition (at the top of the help section)如果您在给出偏移值后查看输出,它会说您需要指定一个分区(在帮助部分的顶部)

Topics are subdivided into partitions, and an offset of 1 could only exist on one partition out of potentially hundreds, so you must specify it主题被细分为多个分区,偏移量 1 只能存在于可能数百个分区中的一个分区上,因此您必须指定它

Regarding displaying the offsets , lookup the GetOffsetShell command syntax关于显示偏移量,查找GetOffsetShell命令语法

A nice command line tool that can display the offset with each message is kafkacat . kafkacat是一个很好的命令行工具,可以显示每条消息的偏移量。

kafkacat -b localhost:9092 -C -t test -f 'Topic %t [%p] at offset %o: key %k: %s\n'

It will print out something like它会打印出类似的东西

Topic test [5] at offset 111: key "0171bf8102007900e33": {"Message": "1"} 
Topic test [2] at offset 123: key "070021b0f001f614c1b": {"Message": "2"}

Since GetOffsetShell only works with PLAINTEXT , it can be inconvenient for many.由于GetOffsetShell与工作PLAINTEXT ,它可以是不方便多。
Good news is that additional properties, including print.offset seems to have made their way into 2.7 based on the communication in 9099 PR .好消息是,基于9099 PR 中的通信,包括print.offset在内的其他属性似乎已进入 2.7。
It must be possible to use print.offset=true now!现在必须可以使用print.offset=true

使用offset时需要分区

.\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --partition 0 --offset 0 --property print.key=true --property print.timestamp=true

You have to give partition value as well with offset in above command like this您必须像这样在上面的命令中提供分区值和偏移量

kafka-console-consumer --bootstrap-server localhost:9092 --topic test --partition 0 --offset 1 kafka-console-consumer --bootstrap-server localhost:9092 --topic test --partition 0 --offset 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM