Skip to content

Instantly share code, notes, and snippets.

@jhansonhpe
Forked from aseigneurin/register_schema.py
Last active September 29, 2020 14:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhansonhpe/ded2d76cf958e3e4340d57386d8795e3 to your computer and use it in GitHub Desktop.
Save jhansonhpe/ded2d76cf958e3e4340d57386d8795e3 to your computer and use it in GitHub Desktop.
Register an Avro schema against the Confluent Schema Registry
#!/usr/bin/python
# Licensed under the Apache 2.0 license
import os
import sys
import requests
schema_registry_url = sys.argv[1]
topic = sys.argv[2]
schema_file = sys.argv[3]
aboslute_path_to_schema = os.path.join(os.getcwd(), schema_file)
print("Schema Registry URL: " + schema_registry_url)
print("Topic: " + topic)
print("Schema file: " + schema_file)
print
with open(aboslute_path_to_schema, 'r') as content_file:
schema = content_file.read()
payload = "{ \"schema\": \"" \
+ schema.replace("\"", "\\\"").replace("\t", "").replace("\n", "") \
+ "\" }"
url = schema_registry_url + "/subjects/" + topic + "-value/versions"
headers = {"Content-Type": "application/vnd.schemaregistry.v1+json"}
r = requests.post(url, headers=headers, data=payload, cert=('/run/secrets/schemaregistry.certificate.pem', '/run/secrets/schemaregistry.key'), verify='/run/secrets/km-ca-1.crt')
if r.status_code == requests.codes.ok:
print("Success")
else:
r.raise_for_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment