跳至内容

Apache Arrow 定义了两种用于序列化进程间通信 (IPC) 数据的格式:一种是“流”格式,另一种是“文件”格式,称为 Feather。RecordBatchStreamWriterRecordBatchFileWriter 分别是用于将记录批写入这些格式的接口。

有关如何使用这些类的指南,请参阅示例部分。

工厂

RecordBatchFileWriter$create()RecordBatchStreamWriter$create() 工厂方法实例化对象并采用以下参数

  • sink 一个 OutputStream

  • schema 要写入数据的Schema

  • use_legacy_format 逻辑值:写入格式化的数据,以便 Arrow 库版本 0.14 及更低版本可以读取它。默认值为 FALSE。您还可以通过设置环境变量 ARROW_PRE_0_15_IPC_FORMAT=1 来启用此功能。

  • metadata_version:一个类似于“V5”的字符串或表示 Arrow IPC MetadataVersion 的等效整数。默认值 (NULL) 将使用最新版本,除非环境变量 ARROW_PRE_1_0_METADATA_VERSION=1,在这种情况下它将为 V4。

方法

  • $write(x):写入RecordBatchTabledata.frame,适当地分派到以下方法

  • $write_batch(batch):将 RecordBatch 写入流

  • $write_table(table):将 Table 写入流

  • $close():关闭流。请注意,这表示文件结尾或流结尾——它不会关闭与 sink 的连接。那需要单独关闭。

另请参阅

write_ipc_stream()write_feather() 为将数据写入这些格式提供了更简单的接口,并且足以满足许多用例。write_to_raw() 是将数据序列化到缓冲区的版本。

示例

tf <- tempfile()
on.exit(unlink(tf))

batch <- record_batch(chickwts)

# This opens a connection to the file in Arrow
file_obj <- FileOutputStream$create(tf)
# Pass that to a RecordBatchWriter to write data conforming to a schema
writer <- RecordBatchFileWriter$create(file_obj, batch$schema)
writer$write(batch)
# You may write additional batches to the stream, provided that they have
# the same schema.
# Call "close" on the writer to indicate end-of-file/stream
writer$close()
# Then, close the connection--closing the IPC message does not close the file
file_obj$close()

# Now, we have a file we can read from. Same pattern: open file connection,
# then pass it to a RecordBatchReader
read_file_obj <- ReadableFile$create(tf)
reader <- RecordBatchFileReader$create(read_file_obj)
# RecordBatchFileReader knows how many batches it has (StreamReader does not)
reader$num_record_batches
#> [1] 1
# We could consume the Reader by calling $read_next_batch() until all are,
# consumed, or we can call $read_table() to pull them all into a Table
tab <- reader$read_table()
# Call as.data.frame to turn that Table into an R data.frame
df <- as.data.frame(tab)
# This should be the same data we sent
all.equal(df, chickwts, check.attributes = FALSE)
#> [1] TRUE
# Unlike the Writers, we don't have to close RecordBatchReaders,
# but we do still need to close the file connection
read_file_obj$close()