-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kafka json to arrow support (#50)
- Loading branch information
Showing
4 changed files
with
79 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import dataclasses | ||
import io | ||
import json | ||
|
||
import confluent_kafka | ||
import pyarrow as pa | ||
import pyarrow.json | ||
|
||
from beavers.kafka import ( | ||
KafkaMessageDeserializer, | ||
KafkaMessageSerializer, | ||
KafkaProducerMessage, | ||
) | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class JsonDeserializer(KafkaMessageDeserializer[pa.Table]): | ||
schema: pa.Schema | ||
|
||
def __call__(self, messages: confluent_kafka.Message) -> pa.Table: | ||
if messages: | ||
with io.BytesIO() as buffer: | ||
for message in messages: | ||
buffer.write(message.value()) | ||
buffer.write(b"\n") | ||
buffer.seek(0) | ||
return pyarrow.json.read_json( | ||
buffer, | ||
parse_options=pyarrow.json.ParseOptions( | ||
explicit_schema=self.schema | ||
), | ||
) | ||
else: | ||
return self.schema.empty_table() | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class JsonSerializer(KafkaMessageSerializer[pa.Table]): | ||
topic: str | ||
|
||
def __call__(self, table: pa.Table): | ||
return [ | ||
KafkaProducerMessage( | ||
self.topic, | ||
key=None, | ||
value=json.dumps(message, default=str).encode("utf-8"), | ||
) | ||
for message in table.to_pylist() | ||
] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from beavers.pyarrow_kafka import JsonDeserializer, JsonSerializer | ||
from tests.test_kafka import mock_kafka_message | ||
from tests.test_util import TEST_TABLE | ||
|
||
|
||
def test_json_deserializer_empty(): | ||
deserializer = JsonDeserializer(TEST_TABLE.schema) | ||
assert deserializer([]) == TEST_TABLE.schema.empty_table() | ||
|
||
|
||
def test_end_to_end(): | ||
deserializer = JsonDeserializer(TEST_TABLE.schema) | ||
serializer = JsonSerializer("topic-1") | ||
out_messages = serializer(TEST_TABLE) | ||
in_messages = [ | ||
mock_kafka_message(topic=m.topic, value=m.value) for m in out_messages | ||
] | ||
assert deserializer(in_messages) == TEST_TABLE |
This file contains 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
This file contains 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