同时使用多个主题作为kafka消费者

Rah*_*hul 1 java multithreading apache-kafka

我正在关注此网址https://cwiki.apache.org/confluence/display/KAFKA/Consumer+Group+Example中有关并发消费kafka主题的示例.

在创建线程池部分时,它们具有以下代码

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(a_numThreads);

    // now create an object to consume the messages
    //
    int threadNumber = 0;
    for (final KafkaStream stream : streams) {
        executor.submit(new ConsumerTest(stream, threadNumber));
        threadNumber++;
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以向topicCountMap添加更多主题.例如,

topicCountMap.put("channel1", new Integer(a_numThreads));
topicCountMap.put("channe2", new Integer(a_numThreads));
topicCountMap.put("channel3", new Integer(a_numThreads));
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,在我看来,streams对象只映射到其中一个主题

List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
Run Code Online (Sandbox Code Playgroud)

我不完全确定如何创建多个流对象,每个对象映射到给定的主题,然后遍历它们以从每个通道获取数据并将它们提交给执行者.

Lez*_*lid 5

假设你有:

String topic1 = "channel1";
String topic2 = "channel2";
String topic3 = "channel3";
Run Code Online (Sandbox Code Playgroud)

那么,你的确可以做到:

topicCountMap.put(topic1, new Integer(a_numThreads_topic1));
topicCountMap.put(topic2, new Integer(a_numThreads_topic2));
topicCountMap.put(topic3, new Integer(a_numThreads_topic3));
Run Code Online (Sandbox Code Playgroud)

获得consumerMap后(执行该操作的代码不会更改),您将能够检索每个主题的流:

List<KafkaStream<byte[], byte[]>> topic1_streams = consumerMap.get(topic1);
List<KafkaStream<byte[], byte[]>> topic2_streams = consumerMap.get(topic2);
List<KafkaStream<byte[], byte[]>> topic3_streams = consumerMap.get(topic3);
Run Code Online (Sandbox Code Playgroud)

要从流中使用,您需要创建正确数量的执行程序:

executors_topic1 = Executors.newFixedThreadPool(a_numThreads_topic1);
executors_topic2 = Executors.newFixedThreadPool(a_numThreads_topic2);
executors_topic3 = Executors.newFixedThreadPool(a_numThreads_topic3);
Run Code Online (Sandbox Code Playgroud)

最后:

int threadNumber = 0;
for (final KafkaStream stream : topic1_streams) {
    executors_topic1.submit(new ConsumerTest(streams, threadNumber));
    threadNumber++;
}
for (final KafkaStream stream : topic2_streams) {
    executors_topic2.submit(new ConsumerTest(stream, threadNumber));
    threadNumber++;
}
for (final KafkaStream stream : topic3_streams) {
    executor_topic3.submit(new ConsumerTest(stream, threadNumber));
    threadNumber++;
}
Run Code Online (Sandbox Code Playgroud)

当然,这只是给你一个想法.显然,代码可以改进.