简体   繁体   English

Kafka 如何从 __consumer_offsets 主题中读取

[英]Kafka how to read from __consumer_offsets topic

I'm trying to find out which offsets my current High-Level consumers are working off.我试图找出我当前的高级消费者正在解决哪些抵消。 I use Kafka 0.8.2.1, with no "offset.storage" set in the server.properties of Kafka - which, I think, means that offsets are stored in Kafka.我使用 Kafka 0.8.2.1,在 Kafka 的 server.properties 中没有设置“offset.storage”——我认为这意味着偏移量存储在 Kafka 中。 (I also verified that no offsets are stored in Zookeeper by checking this path in the Zk shell: /consumers/consumer_group_name/offsets/topic_name/partition_number ) (我还通过在 Zk shell 中检查此路径来验证 Zookeeper 中没有存储偏移量: /consumers/consumer_group_name/offsets/topic_name/partition_number

I tried to listen to the __consumer_offsets topic to see which consumer saves what value of offsets, but it did not work...我试着听__consumer_offsets话题,看看哪个消费者保存了多少偏移量,但是没有用……

I tried the following:我尝试了以下方法:

created a config file for console consumer as following:为控制台使用者创建了一个配置文件,如下所示:

=> more kafka_offset_consumer.config 

 exclude.internal.topics=false

and tried two versions of the console consumer scripts:并尝试了两个版本的控制台使用者脚本:

#1:
bin/kafka-console-consumer.sh --consumer.config kafka_offset_consumer.config --topic __consumer_offsets --zookeeper localhost:2181

#2
./bin/kafka-simple-consumer-shell.sh --topic __consumer_offsets --partition 0 --broker-list localhost:9092 --formatter "kafka.server.OffsetManager\$OffsetsMessageFormatter" --consumer.config kafka_offset_consumer.config

Neither worked - it just sits there but does not print anything, even though the consumers are actively consuming/saving offsets.两者都不起作用 - 它只是坐在那里但不打印任何东西,即使消费者正在积极消费/保存抵消。

Am I missing some other configuration/properties ?我是否缺少其他一些配置/属性?

thanks!谢谢!

Marina码头

I came across this question when trying to also consume from the __consumer_offsets topic.我在尝试从__consumer_offsets主题中消费时遇到了这个问题。 I managed to figure it out for different Kafka versions and thought I'd share what I'd found我设法为不同的 Kafka 版本找出了答案,并认为我会分享我的发现

For Kafka 0.8.2.x对于 Kafka 0.8.2.x

Note : This uses Zookeeper connection注意:这使用 Zookeeper 连接

#Create consumer config
echo "exclude.internal.topics=false" > /tmp/consumer.config
#Consume all offsets
./kafka-console-consumer.sh --consumer.config /tmp/consumer.config \
--formatter "kafka.server.OffsetManager\$OffsetsMessageFormatter" \
--zookeeper localhost:2181 --topic __consumer_offsets --from-beginning

For Kafka 0.9.xx and 0.10.xx对于 Kafka 0.9.xx 和 0.10.xx

#Create consumer config
echo "exclude.internal.topics=false" > /tmp/consumer.config
#Consume all offsets
./kafka-console-consumer.sh --new-consumer --consumer.config /tmp/consumer.config \
--formatter "kafka.coordinator.GroupMetadataManager\$OffsetsMessageFormatter" \
--bootstrap-server localhost:9092 --topic __consumer_offsets --from-beginning

For 0.11.xx - 2.x对于 0.11.xx - 2.x

#Create consumer config
echo "exclude.internal.topics=false" > /tmp/consumer.config
#Consume all offsets
./kafka-console-consumer.sh --consumer.config /tmp/consumer.config \
--formatter "kafka.coordinator.group.GroupMetadataManager\$OffsetsMessageFormatter" \
--bootstrap-server localhost:9092 --topic __consumer_offsets --from-beginning

As of Kafka 0.11, the (Scala) source code can be found here从 Kafka 0.11 开始, 可以在此处找到(Scala)源代码

For those who need a Java translation, from any Consumer process, let's say you get a ConsumerRecord<byte[], byte[]> consumerRecord , and you can use对于那些需要 Java 翻译的人,从任何 Consumer 进程,假设你得到一个ConsumerRecord<byte[], byte[]> consumerRecord ,你可以使用

  1. Get the key, (check if the key is not null first) and use GroupMetadataManager.readMessageKey(consumerRecord.key) .获取密钥,(首先检查密钥是否不为空)并使用GroupMetadataManager.readMessageKey(consumerRecord.key) That can return different types, so check if ( ... instanceof OffsetKey) , then cast it and you can get various values from that.这可以返回不同的类型,因此请检查if ( ... instanceof OffsetKey) ,然后对其进行转换,您可以从中获取各种值。

  2. To get the Kafka record value of the offsets, you can use String.valueOf(GroupMetadataManager.readOffsetMessageValue(consumerRecord.value))要获取偏移量的 Kafka 记录值,可以使用String.valueOf(GroupMetadataManager.readOffsetMessageValue(consumerRecord.value))

A minimal Java example translated from the Scala code...从 Scala 代码翻译的最小 Java 示例...

byte[] key = consumerRecord.key;
if (key != null) {
    Object o = GroupMetadataManager.readMessageKey(key);
    if (o != null && o instanceOf OffsetKey) {
        OffsetKey offsetKey = (OffsetKey) o;
        Object groupTopicPartition = offsetKey.key;
        byte[] value = consumerRecord.value;
        String formattedValue = String.valueOf(GroupMetadataManager.readOffsetMessageValue(value);
       // TODO: Print, store, or compute results with the new key and value 
    }
}

Note, it's also possible to use the AdminClient APIs to describe groups rather than consume these raw messages请注意,也可以使用 AdminClient API 来描述组而不是使用这些原始消息


Scala source code extract Scala 源代码提取

def writeTo(consumerRecord: ConsumerRecord[Array[Byte], Array[Byte]], output: PrintStream) {
  Option(consumerRecord.key).map(key => GroupMetadataManager.readMessageKey(ByteBuffer.wrap(key))).foreach {
    // Only print if the message is an offset record.
    // We ignore the timestamp of the message because GroupMetadataMessage has its own timestamp.
    case offsetKey: OffsetKey =>
      val groupTopicPartition = offsetKey.key
      val value = consumerRecord.value
      val formattedValue =
        if (value == null) "NULL"
        else GroupMetadataManager.readOffsetMessageValue(ByteBuffer.wrap(value)).toString
      output.write(groupTopicPartition.toString.getBytes(StandardCharsets.UTF_8))
      output.write("::".getBytes(StandardCharsets.UTF_8))
      output.write(formattedValue.getBytes(StandardCharsets.UTF_8))
      output.write("\n".getBytes(StandardCharsets.UTF_8))
    case _ => // no-op
  }

Ok, I have figured out what was the problem.好的,我已经弄清楚是什么问题了。 My Kafka was actually using Zookeeper as the offset storage, not Kafka .... The reason I did not detect that right away was because I was checking ZK content incorrectly:我的 Kafka 实际上使用 Zookeeper 作为偏移存储,而不是 Kafka .... 我没有立即检测到的原因是因为我错误地检查了 ZK 内容:

I was doing我在做

ls  /consumers/consumer_group_name/offsets/topic_name/partition_number

and seeing nothing there.并在那里什么也看不到。 Instead I had to 'get' content - which did show correct offsets for my consumers, like the one below:相反,我必须“获取”内容 - 它确实为我的消费者显示了正确的偏移量,如下所示:

get /consumers/consumer_group_name/offsets/topic_name/partition_number 
185530404
cZxid = 0x70789ad05
ctime = Mon Nov 23 17:49:46 GMT 2015
mZxid = 0x7216cdc5c
mtime = Thu Dec 03 20:18:57 GMT 2015
pZxid = 0x70789ad05
cversion = 0
dataVersion = 3537384
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0

If you add --from-beginning is should most likely gives you some results, at least it did when I tried myself.如果您添加--from-beginning很可能会给您一些结果,至少当我自己尝试时是这样。 And or if you don't provide that argument but read more messages (and trigger offset commits) while you have that consumer listening, that should also display messages there.或者,如果您不提供该参数,而是在让消费者收听的同时阅读更多消息(并触发偏移提交),则也应该在那里显示消息。

对于 Kafka-2.X 使用以下命令

kafka-console-consumer --bootstrap-server localhost:9092 --topic __consumer_offsets --formatter "kafka.coordinator.group.GroupMetadataManager$OffsetsMessageFormatter"

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

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