【发布时间】:2017-03-01 10:14:50
【问题描述】:
我有以下 kafka 消费者代码,其中 3 个线程正在从具有 3 个分区的 kafka 主题中读取。
有什么办法,只有在线程当前正在处理的消息被处理后,才会从kafka主题中读取新消息。
例如,假设主题中有 100 条消息,那么有什么方法可以一次读取并处理 3 条消息。现在,当这 3 条消息被处理后,则只应读取接下来的 3 条消息,依此类推。
public void run(int a_numThreads) {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, new Integer(a_numThreads));
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
// now launch all the threads
//
executor = Executors.newFixedThreadPool(3);
// now create an object to consume the messages
//
int threadNumber = 0;
for (final KafkaStream stream : streams) {
executor.submit(new ConsumerTest(stream, threadNumber));
threadNumber++;
}
}
【问题讨论】:
标签: java apache-kafka kafka-consumer-api bigdata