|
29 | 29 | import avro.io
|
30 | 30 |
|
31 | 31 | from confluent_kafka.avro import ClientError
|
32 |
| -from . import SerializerError |
| 32 | +from confluent_kafka.avro.serializer import (SerializerError, |
| 33 | + KeySerializerError, |
| 34 | + ValueSerializerError) |
33 | 35 |
|
34 | 36 | log = logging.getLogger(__name__)
|
35 | 37 |
|
@@ -84,46 +86,52 @@ def encode_record_with_schema(self, topic, schema, record, is_key=False):
|
84 | 86 | @:param topic : Topic name
|
85 | 87 | @:param schema : Avro Schema
|
86 | 88 | @:param record : A dictionary object
|
| 89 | + @:param is_key : If the record is a key |
87 | 90 | @:returns : Encoded record with schema ID as bytes
|
88 | 91 | """
|
89 | 92 | if not isinstance(record, dict):
|
90 | 93 | raise SerializerError("record must be a dictionary")
|
| 94 | + serialize_err = KeySerializerError if is_key else ValueSerializerError |
| 95 | + |
91 | 96 | subject_suffix = ('-key' if is_key else '-value')
|
92 | 97 | # get the latest schema for the subject
|
93 | 98 | subject = topic + subject_suffix
|
94 | 99 | # register it
|
95 | 100 | schema_id = self.registry_client.register(subject, schema)
|
96 | 101 | if not schema_id:
|
97 | 102 | message = "Unable to retrieve schema id for subject %s" % (subject)
|
98 |
| - raise SerializerError(message) |
| 103 | + raise serialize_err(message) |
99 | 104 |
|
100 | 105 | # cache writer
|
101 | 106 | self.id_to_writers[schema_id] = avro.io.DatumWriter(schema)
|
102 | 107 |
|
103 |
| - return self.encode_record_with_schema_id(schema_id, record) |
| 108 | + return self.encode_record_with_schema_id(schema_id, record, is_key=is_key) |
104 | 109 |
|
105 |
| - def encode_record_with_schema_id(self, schema_id, record): |
| 110 | + def encode_record_with_schema_id(self, schema_id, record, is_key=False): |
106 | 111 | """
|
107 | 112 | Encode a record with a given schema id. The record must
|
108 | 113 | be a python dictionary.
|
109 | 114 | @:param: schema_id : integer ID
|
110 | 115 | @:param: record : A dictionary object
|
| 116 | + @:param is_key : If the record is a key |
111 | 117 | @:returns: decoder function
|
112 | 118 | """
|
113 | 119 | if not isinstance(record, dict):
|
114 | 120 | raise SerializerError("record must be a dictionary")
|
| 121 | + serialize_err = KeySerializerError if is_key else ValueSerializerError |
| 122 | + |
115 | 123 | # use slow avro
|
116 | 124 | if schema_id not in self.id_to_writers:
|
117 | 125 | # get the writer + schema
|
118 | 126 |
|
119 | 127 | try:
|
120 | 128 | schema = self.registry_client.get_by_id(schema_id)
|
121 | 129 | if not schema:
|
122 |
| - raise SerializerError("Schema does not exist") |
| 130 | + raise serialize_err("Schema does not exist") |
123 | 131 | self.id_to_writers[schema_id] = avro.io.DatumWriter(schema)
|
124 | 132 | except ClientError as e:
|
125 | 133 | exc_type, exc_value, exc_traceback = sys.exc_info()
|
126 |
| - raise SerializerError("Error fetching schema from registry:" + repr( |
| 134 | + raise serialize_err( + repr( |
127 | 135 | traceback.format_exception(exc_type, exc_value, exc_traceback)))
|
128 | 136 |
|
129 | 137 | # get the writer
|
|
0 commit comments