Kafka:获得Java Producer Client对Record的偏移

时间:2017-08-09 15:53:31

标签: java apache-kafka kafka-producer-api

我正在尝试用Java实现Kafka Producer-Consumer。以下是片段。我知道这不是一个好的代码(可能会杀死整个异步的使用),我只是想亲自动手并进行测试。

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。当我检查消费者日志的偏移时,它会变得与众不同。我缺少什么?

0 个答案:

没有答案