Search code examples
pythonapache-kafkaavroconfluent-schema-registrykafka-python

Unable to register schema using register() in python


I am trying to register schema to confluent schema registry using python.

from schema_registry.client import SchemaRegistryClient

subject_name = "new-schema"
schema_url = "https://{{ schemaRegistry }}:8081" 
sr = SchemaRegistryClient(schema_url)

schema = {"namespace": "example.avro",
 "type": "record",
 "name": "user",
 "fields": [
     {"name": "fname", "type": "string"},
     {"name": "favorite_number",  "type": "int"}
 ]
}

my_schema = sr.register(subject_name, schema)

I am getting error as

AttributeError: 'dict' object has no attribute 'name'

This is a valid avro schema. Still getting this error. What is that I am missing in here?

Any help would be appreciated.


Solution

  • Instead of a dict, try to pass schema_registry.client.schema.AvroSchema:

    from schema_registry.client import SchemaRegistryClient, schema
    
    
    schema_ = schema.AvroSchema({
        "namespace": "example.avro",
        "type": "record",
        "name": "user",
        "fields": [
            {"name": "fname", "type": "string"},
            {"name": "favorite_number",  "type": "int"}
        ]
    })
    
    
    my_schema = sr.register(subject_name, schema_)