Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Support for Kafka Schema Registry in producer / consumer #182

Open
robd003 opened this issue May 30, 2024 · 2 comments
Open

Comments

@robd003
Copy link

robd003 commented May 30, 2024

It would be great to be able to dynamically load schemas from the Kafka Schema Registry when producing / consuming records from Kafka.

At the moment you have to hard code everything and this can easily create a compatibility issue between systems inadvertently using outdated schema.

@bradenneal1
Copy link

Agree this should be built into the library.

FWIW, I've used this library and built schema support separately. The gist to get this running is

1. Extract Schema ID
Each Avro message starts with a 5 byte header, which includes the Schema ID

def deserialise(data):
    message_bytes = io.BytesIO(data)
    # The serialised avro text has 5 leading bytes, representing
    # 0. Magic byte. Confluent serialization format; currently always 0
    # 1-4. Schema ID as returned by Schema Registry
    # See https://stackoverflow.com/questions/44407780
    message_bytes.seek(1)
    schema_id = struct.unpack("!i", message_bytes.read(4))[0]

2. Fetch Schema
Once the Schema ID is known, it can be fetched and parsed using requests, fastavro and json libraries

req = requests.get(
    posixpath.join(url, "schemas", "ids", str(schema_id)),
    cert=(ssl_certfile, ssl_keyfile),
)

req.raise_for_status()
return fastavro.schema.parse_schema(json.loads(req.json()["schema"]))

Where url, ssl_certfile and ssl_keyfile are variables specific to your setup.

It would be great if someone could integrate this into the library itself.

@alberttwong
Copy link

maybe this could solve it. DisasterAWARE/aws-glue-schema-registry-python#26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants