繁体   English   中英

用kafka go读__consumer_offsets

[英]read __consumer_offsets with kafka go

我想阅读使用此库的主题__consumer_offsets: https//github.com/segmentio/kafka-go

我的问题是,除非我指定一个分区,似乎没有任何事情发生。 默认情况下,这个主题有100个分区,查询kafka查找分区列表然后循环遍历读取它们似乎是不合理的,我希望库中预先存在的方法从所有分区读取消息在主题中。

目前以下工作,在我用kafkacat验证__consumer_offsets主题的分区15中有消息之后:

  r := kafka.NewReader(kafka.ReaderConfig{
    Brokers:   []string{"kafka:9092"},
    Topic:     "__consumer_offsets",
    Partition: 15
  })
  r.SetOffset(0)

  for {
    m, err := r.ReadMessage(context.Background())
    if err != nil {
      log.Println("Error while trying to read message")
      log.Fatal(err)
      break
    }
    log.Printf("message at offset %d\n", m.Offset)
  }

  r.Close()

我认为除非需要,否则分区选择应该在用户级别上是透明的。 我错了吗?

有没有办法从主题读取,无论消息在哪个分区? 或者改写,从所有分区读取?

使用使用者组API,您不需要提供分区。

https://github.com/segmentio/kafka-go#consumer-groups

 // GroupID holds the optional consumer group id. If GroupID is specified, then // Partition should NOT be specified eg 0 GroupID string // Partition to read messages from. Either Partition or GroupID may // be assigned, but not both Partition int 

https://godoc.org/github.com/segmentio/kafka-go#ReaderConfig

暂无
暂无

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

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