forked from dask/distributed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom serialization support for pyarrow
Closes dask#2103
- Loading branch information
dhirschf
committed
Jul 13, 2018
1 parent
c0f842d
commit 5bac442
Showing
2 changed files
with
58 additions
and
0 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
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,53 @@ | ||
from __future__ import print_function, division, absolute_import | ||
|
||
from .serialize import register_serialization | ||
|
||
|
||
def serialize_batch(batch): | ||
import pyarrow as pa | ||
sink = pa.BufferOutputStream() | ||
writer = pa.RecordBatchStreamWriter(sink, batch.schema) | ||
writer.write_batch(batch) | ||
writer.close() | ||
buf = sink.get_result() | ||
header = {} | ||
frames = [buf.to_pybytes()] | ||
return header, frames | ||
|
||
|
||
def deserialize_batch(header, frames): | ||
import pyarrow as pa | ||
blob = frames[0] | ||
reader = pa.RecordBatchStreamReader(pa.BufferReader(blob)) | ||
return reader.read_next_batch() | ||
|
||
|
||
def serialize_table(tbl): | ||
import pyarrow as pa | ||
sink = pa.BufferOutputStream() | ||
writer = pa.RecordBatchStreamWriter(sink, tbl.schema) | ||
writer.write_table(tbl) | ||
writer.close() | ||
buf = sink.get_result() | ||
header = {} | ||
frames = [buf.to_pybytes()] | ||
return header, frames | ||
|
||
|
||
def deserialize_table(header, frames): | ||
import pyarrow as pa | ||
blob = frames[0] | ||
reader = pa.RecordBatchStreamReader(pa.BufferReader(blob)) | ||
return reader.read_all() | ||
|
||
|
||
register_serialization( | ||
'pyarrow.lib.RecordBatch', | ||
serialize_batch, | ||
deserialize_batch | ||
) | ||
register_serialization( | ||
'pyarrow.lib.Table', | ||
serialize_batch, | ||
deserialize_table | ||
) |