Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kafka - Consumer group creation with specific offset?

After creating a topic in Kafka, you can create an arbitrary number of consumer groups just by trying to use those groups to read from the topic.

I would like to create an extra consumer group for monitoring the message content of the real consumer groups - one used to peek at their messages. So, GUI would let you click "peek" on any consumer group, and the "peeker" group would have its offset updated to the offset of the group being monitored, then it would show you the message from that offset.

I'm confused though, because you can't explicitly create a consumer group the first time; you seem to have to read a message to get the offset node created in zookeeper.

My Question

Is there a way to explicitly create a consumer group pointed at a specific offset, or is it okay to manually create the zookeeper node for a consumer group that has't been used yet so that it is initialized to the correct offset value? Or will this auto-creation mess up the consumer group allocation process?

like image 966
John Humphreys Avatar asked Jan 26 '15 18:01

John Humphreys


People also ask

How do I set offset in consumer group?

How to change consumer offset? Use the kafka-consumer-groups.sh to change or reset the offset. You would have to specify the topic, consumer group and use the –reset-offsets flag to change the offset.

How do you manually commit specific offset in Kafka?

Method Summary Manually assign a list of partition to this consumer. Get the set of partitions currently assigned to this consumer. Close the consumer, waiting indefinitely for any needed cleanup. Commit offsets returned on the last poll() for all the subscribed list of topics and partition.

What is Kafka consumer group offset?

Defining Kafka Consumer Offset The consumer offset is a way of tracking the sequential order in which messages are received by Kafka topics. Keeping track of the offset, or position, is important for nearly all Kafka use cases and can be an absolute necessity in certain instances, such as financial services.


3 Answers

For readers, Kafka Web Console is no longer supported. Please consider Kafka Manager instead.

like image 95
user1578872 Avatar answered Sep 22 '22 12:09

user1578872


You could have a look at the Kafka Web Console project which already does something similar to what you describe.

If you want to do this yourself, you'd need to use the simple consumer API and manually handle offsets for a new consumer group (stored in Zookeeper or elsewhere). You could get the current offset from the existing consumer group and then read messages using the same offset for your peek group. As long as the group ids are different, they should not interfere with each other or mess anything up.

like image 37
Lundahl Avatar answered Sep 23 '22 12:09

Lundahl


As noted above Kafka Manager has a really nice interface and would be well worth your time to setup. But if you want the CLI version, as I needed, the below should work:

groupId="legitGroupId"
kafka="localhost:9092"
declare -a topics=(
    "topic1" 
    "topic2"
    )

# Create a single consumer of all the topics which starts starts at each topics latest offset
# Use --dry-run instead of --execute to see how the end results will look
for topic in "${topics[@]}"; do
    echo "Adding consumer to $topic"
    kafka-consumer-groups --bootstrap-server $kafka --topic $topic --group $groupId --execute --reset-offsets --to-latest
    echo ""
done
like image 28
3ygun Avatar answered Sep 23 '22 12:09

3ygun