pub struct Writer<W: Write, F: AvroFormat> {
writer: W,
schema: Arc<Schema>,
format: F,
compression: Option<CompressionCodec>,
capacity: usize,
encoder: RecordEncoder,
}展开描述
通用 Avro 写入器。
此类型是关于输出写入器 (W) 和 Avro 格式 (F) 的泛型。你通常会使用具体的别名:
AvroWriter用于 OCF (自描述容器文件)AvroStreamWriter用于 SOE Avro 流
字段§
§writer: W§schema: Arc<Schema>§format: F§compression: Option<CompressionCodec>§capacity: usize§encoder: RecordEncoder实现§
源§impl<W: Write> Writer<W, AvroOcfFormat>
impl<W: Write> Writer<W, AvroOcfFormat>
源代码pub fn new(writer: W, schema: Schema) -> Result<Self, ArrowError>
pub fn new(writer: W, schema: Schema) -> Result<Self, ArrowError>
便捷构造函数 – 与使用 AvroOcfFormat 的 WriterBuilder::build 相同。
§示例
use std::sync::Arc;
use arrow_array::{ArrayRef, Int32Array, RecordBatch};
use arrow_schema::{DataType, Field, Schema};
use arrow_avro::writer::AvroWriter;
let schema = Schema::new(vec![Field::new("id", DataType::Int32, false)]);
let batch = RecordBatch::try_new(
Arc::new(schema.clone()),
vec![Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef],
)?;
let buf: Vec<u8> = Vec::new();
let mut w = AvroWriter::new(buf, schema)?;
w.write(&batch)?;
w.finish()?;
let bytes = w.into_inner();
assert!(!bytes.is_empty());源代码pub fn sync_marker(&self) -> Option<&[u8; 16]>
pub fn sync_marker(&self) -> Option<&[u8; 16]>
返回为此文件生成的 16 字节同步标记的引用。
源§impl<W: Write> Writer<W, AvroSoeFormat>
impl<W: Write> Writer<W, AvroSoeFormat>
源代码pub fn new(writer: W, schema: Schema) -> Result<Self, ArrowError>
pub fn new(writer: W, schema: Schema) -> Result<Self, ArrowError>
创建新的 AvroStreamWriter 的便捷构造函数。
生成的流包含 单一对象编码(无 OCF 头部/同步)。
§示例
use std::sync::Arc;
use arrow_array::{ArrayRef, Int64Array, RecordBatch};
use arrow_schema::{DataType, Field, Schema};
use arrow_avro::writer::AvroStreamWriter;
let schema = Schema::new(vec![Field::new("x", DataType::Int64, false)]);
let batch = RecordBatch::try_new(
Arc::new(schema.clone()),
vec![Arc::new(Int64Array::from(vec![10, 20])) as ArrayRef],
)?;
let sink: Vec<u8> = Vec::new();
let mut w = AvroStreamWriter::new(sink, schema)?;
w.write(&batch)?;
w.finish()?;
let bytes = w.into_inner();
assert!(!bytes.is_empty());源§impl<W: Write, F: AvroFormat> Writer<W, F>
impl<W: Write, F: AvroFormat> Writer<W, F>
源代码pub fn write_batches( &mut self, batches: &[&RecordBatch], ) -> Result<(), ArrowError>
pub fn write_batches( &mut self, batches: &[&RecordBatch], ) -> Result<(), ArrowError>
一个方便的方法,用于写入 [RecordBatch] 的切片。
这等同于对切片中的每个批次调用 write。
源代码pub fn into_inner(self) -> W
pub fn into_inner(self) -> W
消耗写入器,返回底层输出对象。