来自Kafka消费者的数据输出不一致

时间:2018-04-24 08:25:47

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

我需要从Kafka使用者那里提取数据,然后将其传递给我的应用程序。下面是我为访问消费者而编写的代码:

public class ConsumerGroup {
    public static void main(String[] args) throws Exception {

        String topic = "kafka_topic";
        String group = "0";
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("group.id", group);
        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", "1000");
        props.put("session.timeout.ms", "30000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("auto.offset.reset", "earliest");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);

        consumer.subscribe(Arrays.asList(topic));
        System.out.println("Subscribed to topic: " + topic);

        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records)
                System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(), record.value());
        }
    }
}

当我运行此代码时,有时会生成数据,有时不会生成数据。为什么这种行为不一致?我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:1)

你的代码还可以。您启用了自动提交选项,因此在您阅读记录后,它们将自动提交给Kafka。每次运行代码时,都从最后一次处理的偏移量开始,该偏移量存储在__consumer_offsets主题中。所以你总是只读新记录,这些记录在上次运行后已经到达Kafka。要在消费者应用程序中不断打印数据,您应该不断在您的主题中添加新记录。