dongmao7195 2018-10-08 14:51
浏览 623
已采纳

用sarama编写Kafka生产者时的时间戳无效

I have a Kafka instance running (locally, in a Docker) and I created a producer in Go, using the sarama package.

As I want to use Kafka Streams on my topic, the producer has to embed a timestamp in the messages, or I get this ugly error message:

org.apache.kafka.streams.errors.StreamsException: Input record ConsumerRecord(topic = crawler_events, partition = 0, offset = 0, CreateTime = -1, serialized key size = -1, serialized value size = 187, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = {XXX}) has invalid (negative) timestamp. Possibly because a pre-0.10 producer client was used to write this record to Kafka without embedding a timestamp, or because the input topic was created before upgrading the Kafka cluster to 0.10+. Use a different TimestampExtractor to process this data.

Here is the portion of code sending the message in my Go program:

// Init a connection to the Kafka host,
// create the producer,
// and count successes and errors in delivery
func (c *kafkaClient) init() {
    config := sarama.NewConfig()
    config.Producer.Return.Successes = true
    c.config = *config
    var err error
    c.producer, err = sarama.NewAsyncProducer(c.hosts, &c.config)
    if err != nil {
        panic(err)
    }
    go func() {
        for range c.producer.Successes() {
            c.successes++
        }
    }()
    go func() {
        for range c.producer.Errors() {
            c.errors++
        }
    }()
}

// Send a message to the Kafka topic, WITH TIMESTAMP
func (c *kafkaClient) send(event string) {
    message := &sarama.ProducerMessage{
        Topic: c.topic,
        Value: sarama.StringEncoder(event),
        Timestamp: time.Now(),
    }
    c.producer.Input() <- message
    c.enqueued++
}

As you can see, the timestamp I try to send is time.Now().

When I run the console consumer to see the received timestamps:

docker-compose exec kafka /opt/kafka/bin/kafka-console-consumer.sh \
  --bootstrap-server localhost:9092 --topic crawler_events \
  --from-beginning --property print.timestamp=true

I see they are all "-1":

CreateTime:-1   {"XXX"}

When adding a message to the topic with the console producer, I have the expected timestamps like:

CreateTime:1539010180284        hello

What am I doing wrong? Thanks for your help.

  • 写回答

1条回答 默认 最新

  • douyou8266 2018-10-08 17:26
    关注

    Sarama defaults to Kafka Version 0.8.2. It means it will use the old 0.8.2 format requests when talking to brokers.

    As timestamp support was only added in 0.10, if you don't specify a Version >= 0.10 explicitly, your timestamp won't be forwarded to the brokers.

    You need to add config.Version = sarama.V0_10_0_0 to your code and timestamps will work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了