Skip to content

Instantly share code, notes, and snippets.

@agavra
Last active January 24, 2022 21:45
Show Gist options
  • Select an option

  • Save agavra/30a2f56ef7f9351104190fd368ff9b71 to your computer and use it in GitHub Desktop.

Select an option

Save agavra/30a2f56ef7f9351104190fd368ff9b71 to your computer and use it in GitHub Desktop.
ksqlDB Pull Query Bash Demo
#
# Example Usage:
# ./get-and-subscribe.sh -t foo -s "user = 'User_6'" will get all rows with `user = 'User_6'` from the foo table
# ./get-and-subscribe.sh -f -t foo -s "user = 'User_6'" will issue the above query, and also subscribe to changes
usage() { echo "Usage: $0 [-f follow] [-t table] [-s <sql where clause>]" 1>&2; exit 1; }
while getopts "ft:s:" o; do
case "${o}" in
t)
table=${OPTARG}
;;
f)
follow=true
;;
s)
s="${OPTARG}"
;;
*)
usage
;;
esac
done
echo ${s}
# SET VARS
HOSTNAME="pksqlc-gkgjv.us-west4.gcp.confluent.cloud"
# escape any string literals - this is a bit hacky
# if there's a string literal with quotes in it, but
# hopefully this works for most cases
sql_escaped=${s//\'/\'\\\'\'}
cmd="curl -H 'Content-Type: json' --http2 -X POST "
cmd+="--user '${KSQL_API_KEY}:${KSQL_API_SECRET}' "
cmd+="-d '{\"sql\":\"SELECT * FROM ${table} WHERE ${sql_escaped};\"}' "
cmd+="https://${HOSTNAME}/query-stream"
eval "$cmd"
if [ "$follow" = true ]; then
cmd="curl --no-buffer -s -H 'Content-Type: json' --http2 -X POST "
cmd+="--user '${KSQL_API_KEY}:${KSQL_API_SECRET}' "
cmd+="-d '{\"sql\":\"SELECT * FROM ${table} WHERE ${sql_escaped} EMIT CHANGES;\"}' "
cmd+="https://${HOSTNAME}/query-stream"
eval "${cmd} | grep \"^\[\""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment