0

Let's say I have the offset and partition of specific messages in a kafkajs topic. How can I attach a consumer to this topic and consume only these specific messages? For example, if I have the offset and partition of a message in a topic, I would like to consume only that message, and no other messages. Is this possible?

William Jiang
  • 57
  • 1
  • 4

1 Answers1

0

You can seek a consumer to a partition+offset, then break the consumer loop when you get that one event, yes.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks, my understanding is that I could do something like consumer.stop after the end of eachMessage. Do I need to set the autoCommit to be false or something as well? – William Jiang Mar 16 '23 at 16:12
  • You can stop/disconnect, or pause, yes. Auto commits will depend on if you want to store the consumed offset for the group or not. For reading only one record, maybe not, but if you run this on a batch schedule, then probably, yes, but not auto-commit. You should manually commit in this use-case. – OneCricketeer Mar 16 '23 at 22:05