Skip to content

Instantly share code, notes, and snippets.

@bepcyc
Created March 11, 2019 18:08
Show Gist options
  • Save bepcyc/25b37b3d502967ab1e5df385d7c768e3 to your computer and use it in GitHub Desktop.
Save bepcyc/25b37b3d502967ab1e5df385d7c768e3 to your computer and use it in GitHub Desktop.
Calculate average number of records per JSON record in Kafka topic
#!/bin/bash
# returns an average number of key-value pairs per json record
# requires jq and kafka-tools
BOOTSTRAP_SERVERS="some-server-1:9092,some-server-2:9092"
TOPIC_NAME=$1
NUM_MESSAGES=${2:-10000}
TOTAL=$(kafka-console-consumer --topic ${TOPIC_NAME} --bootstrap-server ${BOOTSTRAP_SERVERS} \
--from-beginning --max-messages ${NUM_MESSAGES} \
| jq '.[] | length' | paste -sd+ | bc)
AVG=$(echo "${TOTAL} / ${NUM_MESSAGES}" | bc -l)
printf %.f\\n ${AVG}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment