我正在尝试编写一个Groupf为foo
的Kafka使用者,它订阅某个主题,并且从一开始就进行读取(即使有先前的偏移量< / strong>)。我尝试在重新平衡回调中使用Subscribe
,但似乎从未调用过它(已设置go.application
设置)。
有没有例子可以使这项工作成功?
编辑: 添加了更多详细信息
答案 0 :(得分:0)
此示例来自Confluent Kafka go Github,您可能只需要将auto.offset.reset
的值设置为kafka.OffsetBeginning.String()
:
package main
/**
* Copyright 2016 Confluent Inc.
*/
// consumer_example implements a consumer using the non-channel Poll() API
// to retrieve messages and events.
import (
"fmt"
"github.com/confluentinc/confluent-kafka-go/kafka"
"os"
"os/signal"
"syscall"
)
func main() {
broker := "YOUR_BROKER"
group := "YOUR_GROUP"
topics := "YOUR_TOPICS"
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
c, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": broker,
"group.id": group,
"session.timeout.ms": 6000,
"auto.offset.reset": kafka.OffsetBeginning.String()})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create consumer: %s\n", err)
os.Exit(1)
}
fmt.Printf("Created Consumer %v\n", c)
err = c.SubscribeTopics(topics, nil)
run := true
for run == true {
select {
case sig := <-sigchan:
fmt.Printf("Caught signal %v: terminating\n", sig)
run = false
default:
ev := c.Poll(100)
if ev == nil {
continue
}
switch e := ev.(type) {
case *kafka.Message:
fmt.Printf("%% Message on %s:\n%s\n",
e.TopicPartition, string(e.Value))
if e.Headers != nil {
fmt.Printf("%% Headers: %v\n", e.Headers)
}
case kafka.Error:
// Errors should generally be considered as informational, the client will try to automatically recover
fmt.Fprintf(os.Stderr, "%% Error: %v\n", e)
default:
fmt.Printf("Ignored %v\n", e)
}
}
}
fmt.Printf("Closing consumer\n")
c.Close()
}
答案 1 :(得分:0)
我们现在将enable.auto.commit
设置为false
。这样,将不会存储任何偏移量,并且每次运行都从头开始就消耗掉了。