[英]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.