Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@karakays
Last active January 22, 2022 11:37
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 karakays/b4a026d2932f02ea75368a3aad031802 to your computer and use it in GitHub Desktop.
Save karakays/b4a026d2932f02ea75368a3aad031802 to your computer and use it in GitHub Desktop.

Tools

Config

kafkacat

echo "bootstrap.servers=127.0.0.1:9092" > ~/.config/kafkacat.conf

Query

➞ list topics

kt topic | jq -r '.name' | sort

➞ list topics by name

kt topic -filter '.*queue\.tx.*' | jq -r '.name' | sort

➞ list topics and partitions along with topic offsets (oldest/newest)

kt topic -partitions -filter '.*queue\.tx.*' | jq -r '.name as $name | .partitions[] | [$name, .id, .oldest, .newest] | @tsv' | sort

➞ list topic partitions with leaders and in-sync replicas

kt topic -partitions -leaders -replicas

➞ list consumer groups, topics subscribed and offsets for each topic/partition

kt group

➞ list consumer group offsets by topic

kt group -topic foo

➞ list consumer group offsets by topic

kt group -topic us-east-1.communication.queue.tx-email-job  | jq -r '.name as $name | .offsets[] | [$name, .partition, .offset, .lag] | @tsv' | sort

➞ sabotage offsets

kt group -reset 23 -topic fav-topic -group specials -partitions 2

Consume

➞ live consume topic foo from start and exit

kafkacat -Cet foo -K, -o -1

➞ consume topic foo from start and exit

kafkacat -Cet foo

➞ live consume

kafkacat -Ct foo 

➞ consume in given format

kafkacat -Cet foo -f 'key=%k, offset=%o, headers=%h, partition=%p, payload=%s\n'
kafkacat -Ct foo -f '--START\noffset=%o, headers=%h, partition=%p\nkey=%k\npayload=%s\n'

Produce

➞ produce from stdin with key delimiter =

kafkacat -Pt foo -K=
1=first-message
2=second-message
3=third
Ctrl-D

➞ produce to partition 0

kafkacat -Pp 0 -t foo
something
Ctrl-D

➞ produce multiple messages delimited by space from sdtin

echo 1,a 2,b 3,c | kafkacat -Pt foo -K, -D " "

➞ produce from file

kafkacat -Pt daily-flight-topic flights.json

➞ pipe from topic to another

...

delete

➞ purge topic by deleting all partitions with it

kafka-topics --delete --zookeeper 127.0.0.1:2181 --topic us-east-1.communication.queue.tx-notification-request

➞ delete records

$ kafka-delete-records.sh --bootstrap-server localhost:9092 --offset-json-file share/offset.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment