KafkaConsumer Java客户端未从主题获取记录,但正在检索命令行

时间:2018-11-11 02:02:47

标签: apache-kafka kafka-consumer-api

我在kafka集群中有一个具有代理列表kafka1.com:9092,kafka2.com:9092,kafka3.com:9092的customer-avro主题。

我能够使用以下命令从Java生成该主题的记录,并从控制台中检索它。

    $ sudo bin/kafka-avro-console-consumer     --bootstrap-server kafka1.com:9092,kafka2.com:9092,kafka3.com:9092     --topic customer-avro     --property              schema.registry.url=http://schemaregistry1.com:8081     --from-beginning
    OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
    {"first_name":"Mottu","last_name":"kalidasan","age":24,"height":5.4,"weight":6.4,"automated_email":false}

在上面的命令中,很明显该主题中有一条记录。因此,我在消费者逻辑下面编写了用于检索记录的

    package com.example;

    import io.confluent.kafka.serializers.KafkaAvroDeserializer;
    import org.apache.kafka.clients.consumer.ConsumerRecord;
    import org.apache.kafka.clients.consumer.ConsumerRecords;
    import org.apache.kafka.clients.consumer.KafkaConsumer;
    import org.apache.kafka.common.serialization.StringDeserializer;
    import java.util.Collections;
    import java.util.Properties;

    public class KafkaConsumerV1 {
        public static void main(String[] args) {
            Properties props = new Properties();
            props.setProperty("bootstrap.servers", "kafka1.com:9092,kafka2.com:9092,kafka3.com:9092");
            props.setProperty("group.id", "my-avro-consumer");
            props.setProperty("enable.auto.commit", "false");
            props.setProperty("auto.offset.reset", "earliest");
            props.setProperty("key.deserializer", StringDeserializer.class.getName());
            props.setProperty("value.deserializer", KafkaAvroDeserializer.class.getName());
            props.setProperty("schema.registry.url", "http://schemaregistry1.com:8081");
            props.setProperty("specific.avro.reader", "true");

            try(KafkaConsumer<String, Customer> consumer = new KafkaConsumer<String, Customer>(props)) {
                String topic = "customer-avro";
                consumer.subscribe(Collections.singleton(topic));
                System.out.println("Waiting for data...");
                while (true) {
                    ConsumerRecords<String, Customer> records = consumer.poll(500);
                    for (ConsumerRecord<String, Customer> record : records) {
                        record.key();
                        Customer customer = record.value();
                        System.out.println("customer "+ customer);
                    }
                    consumer.commitSync();

                }
            }
        }
    }

但是使用者没有检索记录,并且始终显示“等待数据”。

我的Java代码中有什么错误。如何使用Java使用者检索数据。

0 个答案:

没有答案