您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何使用Kafka的High Level Consumer,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
為什么使用High Level Consumer
在某些應用場景,我們希望通過多線程讀取消息,而我們并不關心從Kafka消費消息的順序,我們僅僅關心數(shù)據(jù)能被消費就行。High Level 就是用于抽象這類消費動作的。
消息消費已Consumer Group為單位,每個Consumer Group中可以有多個consumer,每個consumer是一個線程,topic的每個partition同時只能被某一個consumer讀取,Consumer Group對應的每個partition都有一個最新的offset的值,存儲在zookeeper上的。所以不會出現(xiàn)重復消費的情況。
設計High Level Consumer High Level Consumer 可以并且應該被使用在多線程的環(huán)境,線程模型中線程的數(shù)量(也代表group中consumer的數(shù)量)和topic的partition數(shù)量有關,下面列舉一些規(guī)則:
當提供的線程數(shù)量多于partition的數(shù)量,則部分線程將不會接收到消息;
當提供的線程數(shù)量少于partition的數(shù)量,則部分線程將從多個partition接收消息;
當某個線程從多個partition接收消息時,不保證接收消息的順序;可能出現(xiàn)從partition3接收5條消息,從partition4接收6條消息,接著又從partition3接收10條消息;
當添加更多線程時,會引起kafka做re-balance, 可能改變partition和線程的對應關系。
代碼示例 ConsumerGroupExample
package com.test.groups; import kafka.consumer.ConsumerConfig; import kafka.consumer.KafkaStream; import kafka.javaapi.consumer.ConsumerConnector; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ConsumerGroupExample { private final ConsumerConnector consumer; private final String topic; private ExecutorService executor; public ConsumerGroupExample(String a_zookeeper, String a_groupId, String a_topic) { consumer = kafka.consumer.Consumer.createJavaConsumerConnector( createConsumerConfig(a_zookeeper, a_groupId)); this.topic = a_topic; } public void shutdown() { if (consumer != null) consumer.shutdown(); if (executor != null) executor.shutdown(); } 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++; } } private static ConsumerConfig createConsumerConfig(String a_zookeeper, String a_groupId) { Properties props = new Properties(); props.put("zookeeper.connect", a_zookeeper); props.put("group.id", a_groupId); props.put("zookeeper.session.timeout.ms", "400"); props.put("zookeeper.sync.time.ms", "200"); props.put("auto.commit.interval.ms", "1000"); return new ConsumerConfig(props); } public static void main(String[] args) { String zooKeeper = args[0]; String groupId = args[1]; String topic = args[2]; int threads = Integer.parseInt(args[3]); ConsumerGroupExample example = new ConsumerGroupExample(zooKeeper, groupId, topic); example.run(threads); try { Thread.sleep(10000); } catch (InterruptedException ie) { } example.shutdown(); } }
ConsumerTest
package com.test.groups; import kafka.consumer.ConsumerIterator; import kafka.consumer.KafkaStream; public class ConsumerTest implements Runnable { private KafkaStream m_stream; private int m_threadNumber; public ConsumerTest(KafkaStream a_stream, int a_threadNumber) { m_threadNumber = a_threadNumber; m_stream = a_stream; } public void run() { ConsumerIterator<byte[], byte[]> it = m_stream.iterator(); while (it.hasNext()) System.out.println("Thread " + m_threadNumber + ": " + new String(it.next().message())); System.out.println("Shutting down Thread: " + m_threadNumber); } }
以上就是如何使用Kafka的High Level Consumer,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。