How to programmatically update subject schema and compatibility in Confluent Schema Registry

Viewed 1461

I have a schema already registered in schema registry, which I was able to do using register() like this,

from schema_registry.client import SchemaRegistryClient, schema

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

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)

Now I need to update this same subject with a new field, so I will get new schema id, and version = 2.

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

I tried using sr.register(subject_name, updated_schema), it throws error for same subject:

AttributeError: 'ClientError' object has no attribute '_get_object_id'
ClientError: Incompatible Avro schema

Yes this function is to register new schema not to update. I didn't get any update function and I don't know how can I do this. SO how can I update schema? Any help would be appreciated.

1 Answers

Schema Registry enforces certain compatibility rules when new schemas are registered in a subject. Therefore, you need to make sure that the compatibility mode of the subject matches the schema evolution that you are looking for.


Using confluent-kafka-python

from confluent_kafka.schema_registry import SchemaRegistryClient


sr = SchemaRegistryClient("https://schema-registry-host:8081")

# Options are: 
#   - NONE, FULL, BACKWARD, FORWARD, 
#   - BACKWARD_TRANSITIVE, FORWARD_TRANSITIVE, FULL_TRANSITIVE
sr.set_compatibility("yourSubjectName", "NONE")

Using python-schema-registry-client

from schema_registry.client import SchemaRegistryClient


sr = SchemaRegistryClient("https://schema-registry-host:8081")
sr.update_compatibility(level="NONE", subject="yourSubjectName")

For a full list of compatibility types, refer to Confluent Documentation.

Related