Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make kafka consumer to read from last consumed offset but not from beginning

I am new to kafka and trying to understand if there is a way to read messages from last consumed offset, but not from beginning.

I am writing an example case, so that my intention will not get deviate.

Eg:
1) I produced 5 messages at 7:00 PM and console consumer consumed those.
2) I stopped consumer at 7:10 PM
3) I produced 10 message at 7:20 PM. No consumer had read those messages.
4) Now, i have started console consumer at 7:30 PM, without from-beginning.
5) Now, it Will read the messages produced after it has started. Not the earlier ones, which were produced at 7.20 PM

Is there a way to get the messages produced from last consumed offset.?

like image 326
Srini Avatar asked Nov 12 '15 16:11

Srini


People also ask

How do I set Kafka offset to latest?

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 read a Kafka topic from the beginning?

If you want to process a topic from its beginning, you can simple start a new consumer group (i.e., choose an unused group.id ) and set auto. offset. reset = earliest . Because there are no committed offsets for a new group, auto offset reset will trigger and the topic will be consumed from its beginning.

How do you consume the last message from a Kafka topic?

If you want to consume only the latest message from kafka topic, Please set “Auto Offset Reset” to “LATEST” and keep other values as default. If you want to consume all the message published from kafka topic Please set “Auto Offset Reset” to “EARLIEST” and keep other values as default.

How to read messages from last consumed offset in Kafka?

I am new to kafka and trying to understand if there is a way to read messages from last consumed offset, but not from beginning. Yes, it is possible to use console consumer to read from the last consumed offset. You have to add consumer.config flag while invoking kafka-console-consumer.

What are __consumer_offsets in Kafka?

Kafka brokers use an internal topic named __consumer_offsets that keeps track of what messages a given consumer group last successfully processed. As we know, each message in a Kafka topic has a partition ID and an offset ID attached to it.

How to scale data consumption from a Kafka topic?

The main way we scale data consumption from a Kafka topic is by adding more consumers to a consumer group. It is common for Kafka consumers to do high-latency operations such as write to a database or a time-consuming computation on the data.

How does Kafka-Python read from last produced message after restart?

kafka-python read from last produced message after a consumer restart 1 Kafka-Python consume last unread message 0 Kafka consumer stops consuming if one of brokers become unavailable 1 Kafka Consumer not consuming from last commited offset after restart


1 Answers

I am new to kafka and trying to understand if there is a way to read messages from last consumed offset, but not from beginning.

Yes, it is possible to use console consumer to read from the last consumed offset. You have to add consumer.config flag while invoking kafka-console-consumer.

Example:-

[root@sandbox bin]# ./kafka-console-consumer.sh --topic test1 --zookeeper localhost:2181 --consumer.config /home/mrnakumar/consumer.properties

Here /home/mrnakumar/consumer.properties is a file containing group.id. Here is how the /home/mrnakumar/consumer.properties looks:-

group.id=consoleGroup

Withoug using consumer.config, it is possible to read either from beginning [ by using --from-beginning] or end of the Log only. End of the Log means all the messages published after consumer start.

like image 81
mrnakumar Avatar answered Oct 26 '22 18:10

mrnakumar