kafka 0.10中的多个线程

时间:2016-10-24 02:12:15

标签: java kafka-consumer-api

在kafka 0.8.2中,我们可以明确定义处理多分区的线程数,但在kafka 0.10中,例如:

for (TopicPartition partition : records.partitions()) { //按partition处理
    List<ConsumerRecord<String, String>> partitionRecords = records.records(partition); //取出partition对应的Records
    for (ConsumerRecord<String, String> record : partitionRecords) {
        System.out.printf("partition = %d, offset = %d, key = %s, value = %s",record.partition(), record.offset(), record.key(), record.value());
     }
     // consumer.commitSync();
     long lastOffset = partitionRecords.get(partitionRecords.size() - 1).offset(); //取出last offset
     consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(lastOffset + 1))); //独立的sync每个partition的offset
 }

以这种方式,它如何处理多分区?单线程还是其他?

0 个答案:

没有答案