CCDAK · Question #60
A consumer wants to read messages from partitions 0 and 1 of a topic topic1. Code snippet is shown below. consumer.subscribe(Arrays.asList("topic1")); List<TopicPartition> pc = new ArrayList<>()…
The correct answer is B. Throws IllegalStateException. subscribe() and assign() cannot be called by the same consumer, subscribe() is used to leverage the consumer group mechanism, while assign() is used to manually control partition assignment and reads assignment.
Question
A consumer wants to read messages from partitions 0 and 1 of a topic topic1. Code snippet is shown below. consumer.subscribe(Arrays.asList("topic1")); List<TopicPartition> pc = new ArrayList<>(); pc.add(new PartitionTopic("topic1", 0)); pc.add(new PartitionTopic("topic1", 1)); consumer.assign(pc);
Options
- AThis works fine. subscribe() will subscribe to the topic and assign() will assign partitions to the
- BThrows IllegalStateException
How the community answered
(37 responses)- A19% (7)
- B81% (30)
Explanation
subscribe() and assign() cannot be called by the same consumer, subscribe() is used to leverage the consumer group mechanism, while assign() is used to manually control partition assignment and reads assignment.
Topics
Community Discussion
No community discussion yet for this question.