06 Consuming messages with Kafka Consumers and Consumer groups 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # 06 Consuming messages with Kafka Consumers and Consumer Groups
  2. Very similar to producer.
  3. ## Creating a consumer
  4. - Properties:
  5. - `bootstrap.servers`
  6. - `key.serializer`
  7. - `value.serializer`
  8. - and others
  9. - => `KafkaProducer`
  10. - [Consumer config doc](http://kafka.apache.org/documentation.html#consumerconfigs)
  11. - See [KafkaConsumerApp](../src/main/java/KafkaConsumerApp.java)
  12. ## (Un)Subscribing
  13. - A single consumer can subscribe to any number of topics
  14. - subscribe takes either a list of strings or a regex string,
  15. - each call replaces the previous subscription list
  16. - They can also unsubscribe all topics at once.
  17. ## Subscribing/assigning
  18. ### consumer.subscribe()
  19. - automatic/dynamic partition assignment
  20. - that single consumer listens to every partition on every listed topic
  21. - entirely managed by the `KafkaConsumer`
  22. - will get newly added partitions to subscribed topics (`SubscriptionState`)
  23. - now needs a valid `group.id` == `ConsumerConfig.GROUP_ID_CONFIG`
  24. ### consumer.assign()
  25. - used to subscribe to explicit partitions
  26. - one or more partitions, regardless of topic
  27. - manual, self-administering mode
  28. - more of an advanced case
  29. - does not needs a `group.id`
  30. - blocks instead of erroring if one of the partitions does not exist, in
  31. `updateFetchPositions.updateFetchPositions()`.
  32. ## The poll loop
  33. - primary function of the `KafkaConsumer`
  34. - continuously poll the brokers for data
  35. - single API for handling all Consumer <-> Broker interactions
  36. - a lot of interactions beyond message retrieval