Spaces:
Runtime error
Runtime error
Create schemaregistry.py
Browse files- schemaregistry.py +35 -0
schemaregistry.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import logging
|
3 |
+
from confluent_kafka.schema_registry import SchemaRegistryClient
|
4 |
+
from confluent_kafka.schema_registry.error import SchemaRegistryError
|
5 |
+
|
6 |
+
class SchemaClient:
|
7 |
+
def __init__(self, schema_url, schema_subject_name):
|
8 |
+
self.schema_url = schema_url
|
9 |
+
self.schema_subject_name = schema_subject_name
|
10 |
+
self.schema_registry_client = SchemaRegistryClient({"url": self.schema_url})
|
11 |
+
|
12 |
+
def get_schema_str(self):
|
13 |
+
try:
|
14 |
+
schema_version = self.schema_registry_client.get_latest_version(
|
15 |
+
self.schema_subject_name
|
16 |
+
)
|
17 |
+
schema_id = schema_version.schema_id
|
18 |
+
schema = self.schema_registry_client.get_schema(schema_id)
|
19 |
+
logging.info(
|
20 |
+
f"Schema ID for {self.schema_subject_name}: {schema_id}"
|
21 |
+
)
|
22 |
+
return schema.schema_str
|
23 |
+
except SchemaRegistryError as e:
|
24 |
+
logging.error(f"Error fetching schema: {e}")
|
25 |
+
return None
|
26 |
+
|
27 |
+
def set_compatibility(self, compatibility_level):
|
28 |
+
try:
|
29 |
+
self.schema_registry_client.set_compatibility(
|
30 |
+
self.schema_subject_name, compatibility_level
|
31 |
+
)
|
32 |
+
logging.info(f"Compatibility level set to {compatibility_level}")
|
33 |
+
except SchemaRegistryError as e:
|
34 |
+
logging.error(e)
|
35 |
+
exit(1)
|