-
Notifications
You must be signed in to change notification settings - Fork 915
Support encoding with fastavro #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5d00428
Support encoding with fastavro
rnpridgeon 8d6b670
consolidate buffer wites, remove create encoder comment
rnpridgeon 6a345b0
Add comment for magic byte in message_serializer
rnpridgeon 715631e
Consolidate message_serializer encoder comments
rnpridgeon 2dbf7d2
Big verbosity comments
rnpridgeon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ | |
|
||
HAS_FAST = False | ||
try: | ||
from fastavro import schemaless_reader | ||
from fastavro import schemaless_reader, schemaless_writer | ||
|
||
HAS_FAST = True | ||
except ImportError: | ||
|
@@ -75,9 +75,13 @@ def __init__(self, registry_client, reader_key_schema=None, reader_value_schema= | |
self.reader_key_schema = reader_key_schema | ||
self.reader_value_schema = reader_value_schema | ||
|
||
''' | ||
|
||
''' | ||
# Encoder support | ||
def _get_encoder_func(self, writer_schema): | ||
if HAS_FAST: | ||
schema = writer_schema.to_json() | ||
return lambda record, fp: schemaless_writer(fp, schema, record) | ||
writer = avro.io.DatumWriter(writer_schema) | ||
return lambda record, fp: writer.write(record, avro.io.BinaryEncoder(fp)) | ||
|
||
def encode_record_with_schema(self, topic, schema, record, is_key=False): | ||
""" | ||
|
@@ -103,7 +107,7 @@ def encode_record_with_schema(self, topic, schema, record, is_key=False): | |
raise serialize_err(message) | ||
|
||
# cache writer | ||
self.id_to_writers[schema_id] = avro.io.DatumWriter(schema) | ||
self.id_to_writers[schema_id] = self._get_encoder_func(schema) | ||
|
||
return self.encode_record_with_schema_id(schema_id, record, is_key=is_key) | ||
|
||
|
@@ -126,29 +130,19 @@ def encode_record_with_schema_id(self, schema_id, record, is_key=False): | |
schema = self.registry_client.get_by_id(schema_id) | ||
if not schema: | ||
raise serialize_err("Schema does not exist") | ||
self.id_to_writers[schema_id] = avro.io.DatumWriter(schema) | ||
self.id_to_writers[schema_id] = self._get_encoder_func(schema) | ||
except ClientError: | ||
exc_type, exc_value, exc_traceback = sys.exc_info() | ||
raise serialize_err(repr(traceback.format_exception(exc_type, exc_value, exc_traceback))) | ||
|
||
# get the writer | ||
writer = self.id_to_writers[schema_id] | ||
with ContextStringIO() as outf: | ||
# write the header | ||
# magic byte | ||
|
||
outf.write(struct.pack('b', MAGIC_BYTE)) | ||
|
||
# write the schema ID in network byte order (big end) | ||
|
||
outf.write(struct.pack('>I', schema_id)) | ||
# Write the magic byte and schema ID in network byte order (big endian) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great fix! |
||
outf.write(struct.pack('>bI', MAGIC_BYTE, schema_id)) | ||
|
||
# write the record to the rest of it | ||
# Create an encoder that we'll write to | ||
encoder = avro.io.BinaryEncoder(outf) | ||
# write the magic byte | ||
# write the object in 'obj' as Avro to the fake file... | ||
writer.write(record, encoder) | ||
# write the record to the rest of the buffer | ||
writer(record, outf) | ||
|
||
return outf.getvalue() | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this comment still applies though