Consuming
This example demonstrates how to consume using Kafunk.
Joining the consumer group: See also: ConsumerConfig
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: |
|
Consumer State
Consumer state consists of consumer group member state, as well as state particular to the consumer group protocol, such as the set of partitions assigned to the consumer. The state can be retrieved, but note that state changes when group membership changes.
1: 2: 3: 4: 5: 6: 7: |
|
Consuming
Consume with commit on every message set. In this case, offsets are committed as soon as a message set is processed. Note that this may result in needless synchronization - since the strongest delivery model supported is at-least-once, consumers have to be tolerant to receiving duplicate messages. Therefore, it is acceptable to commit offsets asynchronously.
1: 2: 3: 4: 5: 6: |
|
Consume with periodic commit. This commits offsets asynchronously thereby eliminating a synchronization in the critical path.
1: 2: 3: 4: 5: 6: |
|
Periodic Offset Commit
Consumer.consumePeriodicCommit
commits offsets periodically using the following mechanism.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: |
|
The commit queue will commit enqueued offsets periodically. It will commit the greatest offset enqueued by partition.
Streaming
In order to have explicit control of buffering and parallelism, or to perform other streaming operations, it is possible to consume messages directly using an asynchronous sequence:
1: 2: 3: 4: |
|
The resulting stream merges all of the per-broker streams covering all assigned partitions of the consumed topic. Note that offsets must be committed explicitly as described earlier.
Consumer Offsets
Consumer offsets can be committed explicitly. This can be used to reset a consumer to a particular offset when the consumer instances are offline. Note that consumer instances only fetch committed offsets when they are starting to consumer, or when rejoining.
1: 2: 3: 4: 5: 6: |
|
Fetch committed consumer offsets:
1: 2: 3: 4: 5: 6: 7: |
|
Consuming Fixed Ranges
Kafunk provides a helper to consumer a fixed range of offsets. For example, to read the range of messages in the topic at the time of invocation:
1: 2: 3: 4: 5: 6: 7: 8: 9: |
|
Consumer Progress Information
Kafunk contains a helper module which provides consumer progress information.
1: 2: 3: 4: 5: |
|
Consumer Group Information
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: |
|
Rolling Updates
Kafka supports rolling updates of consumer group member instances. This can be done by having a newever version of a consumer support both new and old versions of consumer group assignment strategies. The assignment strategy itself can change, but the code path invoked by the consumer can also change. The Kafka group coordinator ensures that all members of the group support the same protocol version. Once new versions have been deployed, all consumer instances will support the new version and Kafka will select the first version in the list.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: |
|
Contributing and copyright
The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding a new public API, please also consider adding samples that can be turned into a documentation. You might also want to read the library design notes to understand how it works.
The library is available under Apache 2.0. For more information see the License file in the GitHub repository.