2016-06-21 102 views
0

注意的多個分區數據:使用kafka_2.11-0.9.0.1爲什麼卡夫卡消費者API不獲取由一個主題

我已經創建了一個名爲卡夫卡話題:consumer-tutorials3 Partitions如下:

C:\kafka_2.11-0.9.0.1>.\bin\windows\kafka-topics.bat --describe --topic consumer-tutorial 
--zookeeper localhost:2181 
Topic:consumer-tutorial PartitionCount:3  ReplicationFactor:1  Configs: 
     Topic: consumer-tutorial  Partition: 0 Leader: 0  Replicas: 0  Isr: 0 
     Topic: consumer-tutorial  Partition: 1 Leader: 0  Replicas: 0  Isr: 0 
     Topic: consumer-tutorial  Partition: 2 Leader: 0  Replicas: 0  Isr: 0 

一旦創建過主題,我使用Producer API按照以下生產的每個分區中的一些數據:

KafkaProducer<String,String> prod = new KafkaProducer<String,String>(props); 

for(int i =0; i < 3; i++) 
{ 
    for(int x=start; x<end; x++) 
    { 
     prod.send(new ProducerRecord<String,String>("consumer-tutorial",i,Integer.toString(x),Integer.toString(rnd.nextInt(100))));  } 
    start=end; 
    end = start + 10; 
} 
prod.close(); 

現在,當我試圖獲取記錄/ CON使用以下Consumer API這個話題廟消息:

KafkaConsumer<String,String> consumer = new KafkaConsumer<String,String>(props); 
consumer.subscribe(Arrays.asList("consumer-tutorial")); 

while(true) 
{ 
    ConsumerRecords<String, String> records = consumer.poll(100); 

    for(ConsumerRecord<String,String> record: records) 
    { 
     System.out.printf("Key: %s, Value = %s", record.key(), record.value()); 
    } 

} 

同時運行此代碼,我沒有得到任何記錄。我檢查record變量,但沒有KEY:VALUE對從Poll

Watch on "records" shows nothing

未來誰能幫助我,爲什麼我沒有得到任何要顯示的數據。

NOTE: It works well when I have single partition topic. 

回答

0

當你訂閱主題,內部大量的操作會發生這樣的1.查找組協調員2.發送到組請求,並獲取組長3領導人將確保任何一個加入該組或離開團隊成員4.領導者根據分配策略(範圍/ RoundRobin)將分區劃分給成員。 5.然後每個消費者成員從組協調員獲取元數據。

那麼它將取的記錄。所以最初的幾次迭代在輪詢時不會得到任何數據。

0

據我所知,它會採取前幾個迭代在我的情況然而循環取記錄進入無限的'Poll'保持輪詢不休。我今天晚些時候發現,當我用一些新名稱重新設置組名(我使用UUID來隨機化組名)時,它會使用不同的分區從主題獲取數據。因此,得出結論:偏移量需要重置到開始位置,以便消費者可以從頭開始獲取數據,並在每次new保持偏移指針開始時保持組ID。現在我想知道什麼是設置偏移的話題(不分區)開始的方式..我嘗試使用'props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");「和seekseekToBeginning但他們都不會工作,除非消費者已經失去了偏移軌道..那麼有沒有辦法通過我可以設置偏移指針開始?