Skip to content

Instantly share code, notes, and snippets.

@Godoy
Last active September 14, 2023 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Godoy/a869e7fa25e04b4673de65a60fd849e8 to your computer and use it in GitHub Desktop.
Save Godoy/a869e7fa25e04b4673de65a60fd849e8 to your computer and use it in GitHub Desktop.
Using kcat with jq to read a kafka topic message

Given a message like this:

{ "message": {"accountId":1,"timestamp":"1694709688878","amount":1001,"sentFromClientAt":1694709688913}, "delivered_at": 1694709688954 }

The kcat command can be used with jq to format message, even a calculated attribute:

kcat -C -b localhost:31090 -f '{ "message": %s, "delivered_at": %T } \n' -t input-topic -u | jq '. + { "took": "\(.delivered_at - .message.sentFromClientAt) ms" }'

And this can be used as an "alias" function putting this in your .zshrc, for example:

kmsg() {
    kcat -C -b localhost:31090 -f '{ "message": %s, "delivered_at": %T } \n' -t $1 -u | jq '. + { "took": "\(.delivered_at - .message.sentFromClientAt) ms" }'
}

So you can do:

kmsg my-kafka-topic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment