kafka:java生产者客户端获取记录的偏移量

yqkkidmi  于 2021-06-08  发布在  Kafka
关注(0)|答案(0)|浏览(280)

我正在尝试用java实现一个kafka生产者-消费者。下面是片段。我知道这不是一个好的代码(可能会扼杀整个异步的使用),我只想一个手上和测试。

public void publishMessage(RequestFeedback requestFeedback) {
    LOGGER.info("Going to push Feedback:{}", requestFeedback);
    try {
      ProducerRecord<Object, Object> record = new ProducerRecord<>(TOPIC_NAME,
          objectMapper.writeValueAsString(requestFeedback));
      Future<RecordMetadata> recordMetadataFuture = producer.send(record);
      try {
        RecordMetadata recordMetadata = recordMetadataFuture.get();
        LOGGER.info("Record:{} pushed successfully to Kafka with offset:{}",
          record,recordMetadata.offset());
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      }
    } catch (JsonProcessingException e) {
      LOGGER.error("Error in converting object:{} into JSON String because of:",
          requestFeedback, e);
    }
}

日志:

{
  "@timestamp": "2017-08-09T15:20:47.839+00:00",
  "@version": 1,
  "message": "Record:ProducerRecord(topic=FEEDBACK_SENTIMENT, partition=null, key=null, value={\"id\":8,\"instanceId\":7,\"userId\":61,\"userResponseId\":12,\"feedbackText\":\"Hello\"} pushed successfully to Kafka with offset:-1",
  "logger_name": "com.kafkaProducer.v3.utils.kafka.FeedbackProducer",
  "thread_name": "main",
  "level": "INFO",
  "level_value": 20000,
  "HOSTNAME": "Shubhams-MacBook-Pro.local",
  "app_name": "kafkaProducer",
  "log_type": "app"
}

问题是偏移量是-1。当我检查消费者日志的偏移量时,情况就不同了。我错过了什么?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题