consumer.How to specify partition to read? [kafka]

余生颓废 提交于 2019-11-30 02:47:26

问题


I am introducing with kafka and I want to know how to specify partition when I consume messages from topic.

I have found several picture like this:

It means that 1 consumer can consume messages from several partitions but 1 partition can be read by single consumer(within consumer group)

Also I have read several examples for consumer and it looks like this:

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "consumer-tutorial");
props.put("key.deserializer", StringDeserializer.class.getName());
props.put("value.deserializer", StringDeserializer.class.getName());
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); 

and:

1.subscribe:

consumer.subscribe(Arrays.asList(“foo”, “bar”)); 

2. poll

 try {
      while (running) {
        ConsumerRecords<String, String> records = consumer.poll(1000);
        for (ConsumerRecord<String, String> record : records)
          System.out.println(record.offset() + ": " + record.value());
      }
    } finally {
      consumer.close();
    }

How does this work? From which partition will I read messages?


回答1:


There are two ways to tell what topic/partitions you want to consume: KafkaConsumer#assign() (you specify the partition you want and the offset where you begin) and subscribe (you join a consumer group, and partition/offset will be dynamically assigned by group coordinator depending of consumers in the same consumer group, and may change during runtime)

In both case, you need to poll to receive data.

See https://kafka.apache.org/0110/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html, especially paragraphs Consumer Groups and Topic Subscriptions and Manual Partition Assignment



来源:https://stackoverflow.com/questions/46492707/consumer-how-to-specify-partition-to-read-kafka

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!