Created
December 17, 2020 14:11
-
-
Save irajhedayati/df7fc8bf22b0c0cb5d2ca0ed936df77f to your computer and use it in GitHub Desktop.
Register an Avro schema using command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
usage() { | |
echo " Register Avro schema from a file in a Schema Registry" | |
echo "" | |
echo " Usage:" | |
echo " register-avro-schema.sh <Options>" | |
echo "" | |
echo " Options:" | |
echo " -r the URL of the Schema Registry e.g. http://localhost:8081" | |
echo " -s subject name" | |
echo " -f path to the file that contains the Avro schema" | |
} | |
while getopts "r:s:f:" opt; do | |
case ${opt} in | |
r) SCHEMA_REGISTRY_URL=$OPTARG ;; | |
s) SUBJECT=$OPTARG ;; | |
f) FILE_PATH=$OPTARG ;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
usage | |
exit 1; | |
;; | |
esac | |
done | |
if [[ -z ${SCHEMA_REGISTRY_URL} ]]; then | |
echo "The URL of the schema registry is not provided" | |
usage | |
exit 1 | |
fi | |
if [[ -z ${SUBJECT} ]]; then | |
echo "The subject is not provided" | |
usage | |
exit 1 | |
fi | |
if [[ -z ${FILE_PATH} ]]; then | |
echo "The file path to Avro schema is not provided" | |
usage | |
exit 1 | |
fi | |
SCHEMA='{"schema":"'$(cat ${FILE_PATH} | tr '\n' '\r' | sed 's/"/\\"/g' | sed 's/\s//g')'"}' | |
echo $SCHEMA > ./tmp.json | |
curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" --data @./tmp.json ${SCHEMA_REGISTRY_URL}/subjects/${SUBJECT}/versions | |
rm -f ./tmp.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment