注意的多個分區數據:使用kafka_2.11-0.9.0.1爲什麼卡夫卡消費者API不獲取由一個主題
我已經創建了一個名爲卡夫卡話題:consumer-tutorials
與3 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.