3

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.

Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
1689
  • 117
  • 1
  • 10

1 Answers1

3

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.

Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
  • @1622 I updated my answer. Which library are you using? – Giorgos Myrianthous May 21 '20 at 10:05
  • It still throws error, Exception: Incompatible Avro schema. I am using schema_registry.client library. Also its not recommended to use level = "NONE", I have given, sr.update_compatibility(level="FULL", subject="yourSubjectName"), but it still showing error. – 1689 May 26 '20 at 09:51
  • I added default value, along with FULL compatibility mode and it worked. Thanks :) – 1689 May 27 '20 at 06:42