如何从每个Kafka主题中读取一条记录?

时间:2019-03-22 12:49:09

标签: java apache-kafka

我有卡夫卡消费者:

public void consumeKafka(Collection<String> topics) {

    Properties props = new Properties();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class.getName());
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class.getName());
    props.put(KafkaAvroDeserializerConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryUrl);
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 1000);
    consumer = new KafkaConsumer<>(props);

    consumer.subscribe(topics);

    int i = 0;
    int iterationValue = 5;

        while (i++ < iterationValue) {
            ConsumerRecords<Object, Object> records = consumer.poll(1000);
            System.out.println(records.partitions());
        }
}

我需要从每个主题中读取一条记录。我怎样才能做到这一点? 当我指定props.put的值(ConsumerConfig.MAX_POLL_RECORDS_CONFIG,1)时,我什么也没得到。

P.S。如果池很大,每次迭代时控制台中的结果都会有所不同。数据存在。

示例结果System.out.println(records.partitions());:

1 [] 
2 [TSDocumentExpress-0, TSDocMSTask-0] 
3 [TSDocRouteSheet-0, TSDocTMSTask-0] 
4 [TSDoctTransferStatus-0, TSDocRouteSheet-0] 
5 [TSDocTransferStatus-0]

0 个答案:

没有答案