天天看點

Kafka的進階消費者與低級消費者

Kafka的進階消費者與低級消費者 2017年11月20日 12:08:03 閱讀數:1222

          在Kafka實戰章節,我們寫的例子都是Kafka的進階消費執行個體,可以看到在消息消費者的程式中,我們隻需要指定zookeeper、及消費群組的groupId即可實作從消息隊列中消費消息,屏蔽了大量的底層細節:如消息的偏移量等資訊都不在程式中維護。Kafka的進階消費執行個體,滿足以下幾點規則:

(1)同一個消費群組中,如果線程數大于Topic分區數,那麼一些線程永遠接收不到消息;

(2)同一個消費群組中,如果線程數小于Topic分區數,部分線程将從多個分區接收消息;

(3)對于從多個分區接收消息的線程,消費每個分區内的消息是有序的,但消費多個分區之間的消息是無序的;

        明白了Kafka的進階消費執行個體的過程之後,如果我們想進一步控制一個消費者消費哪個分區怎麼辦呢?比如多次讀取同一個消息。答案是使用低級消費者執行個體,即在程式中指定Topic的Partition的Leader broker,并在程式中跟蹤消息的偏移量offset值。其步驟大緻如下:

(1)指定消費Topic Partition的Leader broker及備份broker;

(2)構造并發送請求資料;

(3)處理leader broker的變更;

        執行個體如下:

[java]  view plain  copy

  1. import kafka.api.FetchRequest;  
  2. import kafka.api.FetchRequestBuilder;  
  3. import kafka.api.PartitionOffsetRequestInfo;  
  4. import kafka.common.ErrorMapping;  
  5. import kafka.common.TopicAndPartition;  
  6. import kafka.javaapi.*;  
  7. import kafka.javaapi.consumer.SimpleConsumer;  
  8. import kafka.message.MessageAndOffset;  
  9. import java.nio.ByteBuffer;  
  10. import java.util.ArrayList;  
  11. import java.util.Collections;  
  12. import java.util.HashMap;  
  13. import java.util.List;  
  14. import java.util.Map;  
  15. public class SimpleConsumerDemo {  
  16.     private List<String> m_replicaBrokers = new ArrayList<>();  
  17.     public SimpleConsumerDemo(){  
  18.         m_replicaBrokers = new ArrayList<>();  
  19.     }  
  20.     public void run(long a_maxReads, String a_topic, int a_partition, List<String> a_seedBrokers, int a_port) throws Exception {  
  21.         // find the meta data about the topic and partition we are interested in  
  22.         //  
  23.         PartitionMetadata metadata = findLeader(a_seedBrokers, a_port, a_topic, a_partition);  
  24.         if (metadata == null) {  
  25.             System.out.println("Can't find metadata for Topic and Partition. Exiting");  
  26.             return;  
  27.         }  
  28.         if (metadata.leader() == null) {  
  29.             System.out.println("Can't find Leader for Topic and Partition. Exiting");  
  30.             return;  
  31.         }  
  32.         String leadBroker = metadata.leader().host();  
  33.         String clientName = "Client_" + a_topic + "_" + a_partition;  
  34.         SimpleConsumer consumer = new SimpleConsumer(leadBroker, a_port, 100000, 64 * 1024, clientName);  
  35.         long readOffset = getLastOffset(consumer,a_topic, a_partition, kafka.api.OffsetRequest.EarliestTime(), clientName);  
  36.         int numErrors = 0;  
  37.         while (a_maxReads > 0) {  
  38.             if (consumer == null) {  
  39.                 consumer = new SimpleConsumer(leadBroker, a_port, 100000, 64 * 1024, clientName);  
  40.             }  
  41.             FetchRequest req = new FetchRequestBuilder()  
  42.                     .clientId(clientName)  
  43.                     .addFetch(a_topic, a_partition, readOffset, 100000) // Note: this fetchSize of 100000 might need to be increased if large batches are written to Kafka  
  44.                     .build();  
  45.             FetchResponse fetchResponse = consumer.fetch(req);  
  46.             if (fetchResponse.hasError()) {  
  47.                 numErrors++;  
  48.                 // Something went wrong!  
  49.                 short code = fetchResponse.errorCode(a_topic, a_partition);  
  50.                 System.out.println("Error fetching data from the Broker:" + leadBroker + " Reason: " + code);  
  51.                 if (numErrors > 5) break;  
  52.                 if (code == ErrorMapping.OffsetOutOfRangeCode())  {  
  53.                     // We asked for an invalid offset. For simple case ask for the last element to reset  
  54.                     readOffset = getLastOffset(consumer,a_topic, a_partition, kafka.api.OffsetRequest.LatestTime(), clientName);  
  55.                     continue;  
  56.                 }  
  57.                 consumer.close();  
  58.                 consumer = null;  
  59.                 leadBroker = findNewLeader(leadBroker, a_topic, a_partition, a_port);  
  60.                 continue;  
  61.             }  
  62.             numErrors = 0;  
  63.             long numRead = 0;  
  64.             for (MessageAndOffset messageAndOffset : fetchResponse.messageSet(a_topic, a_partition)) {  
  65.                 long currentOffset = messageAndOffset.offset();  
  66.                 if (currentOffset < readOffset) {  
  67.                     System.out.println("Found an old offset: " + currentOffset + " Expecting: " + readOffset);  
  68.                     continue;  
  69.                 }  
  70.                 readOffset = messageAndOffset.nextOffset();  
  71.                 ByteBuffer payload = messageAndOffset.message().payload();  
  72.                 byte[] bytes = new byte[payload.limit()];  
  73.                 payload.get(bytes);  
  74.                 System.out.println(String.valueOf(messageAndOffset.offset()) + ": " + new String(bytes, "UTF-8"));  
  75.                 numRead++;  
  76.                 a_maxReads--;  
  77.             }  
  78.             if (numRead == 0) {  
  79.                 try {  
  80.                     Thread.sleep(1000);  
  81.                 } catch (InterruptedException ie) {  
  82.                 }  
  83.             }  
  84.         }  
  85.         if (consumer != null) consumer.close();  
  86.     }  
  87.     public static long getLastOffset(SimpleConsumer consumer, String topic, int partition,  
  88.                                      long whichTime, String clientName) {  
  89.         TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);  
  90.         Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();  
  91.         requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));  
  92.         kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(  
  93.                 requestInfo, kafka.api.OffsetRequest.CurrentVersion(), clientName);  
  94.         OffsetResponse response = consumer.getOffsetsBefore(request);  
  95.         if (response.hasError()) {  
  96.             System.out.println("Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic, partition) );  
  97.             return 0;  
  98.         }  
  99.         long[] offsets = response.offsets(topic, partition);  
  100.         return offsets[0];  
  101.     }  
  102.     private String findNewLeader(String a_oldLeader, String a_topic, int a_partition, int a_port) throws Exception {  
  103.         for (int i = 0; i < 3; i++) {  
  104.             boolean goToSleep = false;  
  105.             PartitionMetadata metadata = findLeader(m_replicaBrokers, a_port, a_topic, a_partition);  
  106.             if (metadata == null) {  
  107.                 goToSleep = true;  
  108.             } else if (metadata.leader() == null) {  
  109.                 goToSleep = true;  
  110.             } else if (a_oldLeader.equalsIgnoreCase(metadata.leader().host()) && i == 0) {  
  111.                 // first time through if the leader hasn't changed give ZooKeeper a second to recover  
  112.                 // second time, assume the broker did recover before failover, or it was a non-Broker issue  
  113.                 //  
  114.                 goToSleep = true;  
  115.             } else {  
  116.                 return metadata.leader().host();  
  117.             }  
  118.             if (goToSleep) {  
  119.                 try {  
  120.                     Thread.sleep(1000);  
  121.                 } catch (InterruptedException ie) {  
  122.                 }  
  123.             }  
  124.         }  
  125.         System.out.println("Unable to find new leader after Broker failure. Exiting");  
  126.         throw new Exception("Unable to find new leader after Broker failure. Exiting");  
  127.     }  
  128.     private PartitionMetadata findLeader(List<String> a_seedBrokers, int a_port, String a_topic, int a_partition) {  
  129.         PartitionMetadata returnMetaData = null;  
  130.         loop:  
  131.         for (String seed : a_seedBrokers) {  
  132.             SimpleConsumer consumer = null;  
  133.             try {  
  134.                 consumer = new SimpleConsumer(seed, a_port, 100000, 64 * 1024, "leaderLookup");  
  135.                 List<String> topics = Collections.singletonList(a_topic);  
  136.                 TopicMetadataRequest req = new TopicMetadataRequest(topics);  
  137.                 kafka.javaapi.TopicMetadataResponse resp = consumer.send(req);  
  138.                 List<TopicMetadata> metaData = resp.topicsMetadata();  
  139.                 for (TopicMetadata item : metaData) {  
  140.                     for (PartitionMetadata part : item.partitionsMetadata()) {  
  141.                         if (part.partitionId() == a_partition) {  
  142.                             returnMetaData = part;  
  143.                             break loop;  
  144.                         }  
  145.                     }  
  146.                 }  
  147.             } catch (Exception e) {  
  148.                 System.out.println("Error communicating with Broker [" + seed + "] to find Leader for [" + a_topic  
  149.                         + ", " + a_partition + "] Reason: " + e);  
  150.             } finally {  
  151.                 if (consumer != null) consumer.close();  
  152.             }  
  153.         }  
  154.         if (returnMetaData != null) {  
  155.             m_replicaBrokers.clear();  
  156.             for (kafka.cluster.Broker replica : returnMetaData.replicas()) {  
  157.                 m_replicaBrokers.add(replica.host());  
  158.             }  
  159.         }  
  160.         return returnMetaData;  
  161.     }  
  162.     public static void main(String args[]) {  
  163.         SimpleConsumerDemo example = new SimpleConsumerDemo();  
  164.         long maxReads = Long.parseLong(args[0]);  
  165.         String topic = args[1];  
  166.         int partition = Integer.parseInt(args[2]);  
  167.         List<String> seeds = new ArrayList<>();  
  168.         seeds.add(args[3]);  
  169.         int port = Integer.parseInt(args[4]);  
  170.         try {  
  171.             example.run(maxReads, topic, partition, seeds, port);  
  172.         } catch (Exception e) {  
  173.             System.out.println("Oops:" + e);  
  174.             e.printStackTrace();  
  175.         }  
  176.     }  
  177. }  

參考資料:

1、https://cwiki.apache.org/confluence/display/KAFKA/Index

2、http://www.nohup.cc/article/195/

3、http://blog.csdn.net/honglei915/article/details/37563647

4、http://orchome.com/11