AvroWriter

Type Alias AvroWriter 

Source
pub type AvroWriter<W> = Writer<W, AvroOcfFormat>;
Expand description

Alias for an Avro Object Container File writer.

§Quickstart (runnable)

use std::io::Cursor;
use std::sync::Arc;
use arrow_array::{ArrayRef, Int64Array, StringArray, RecordBatch};
use arrow_schema::{DataType, Field, Schema};
use arrow_avro::writer::AvroWriter;
use arrow_avro::reader::ReaderBuilder;

// Writer schema: { id: long, name: string }
let writer_schema = Schema::new(vec![
    Field::new("id", DataType::Int64, false),
    Field::new("name", DataType::Utf8, false),
]);

// Build a RecordBatch with two rows
let batch = RecordBatch::try_new(
    Arc::new(writer_schema.clone()),
    vec![
        Arc::new(Int64Array::from(vec![1, 2])) as ArrayRef,
        Arc::new(StringArray::from(vec!["a", "b"])) as ArrayRef,
    ],
)?;

// Write an Avro **Object Container File** (OCF) to memory
let mut w = AvroWriter::new(Vec::<u8>::new(), writer_schema.clone())?;
w.write(&batch)?;
w.finish()?;
let bytes = w.into_inner();

// Build a Reader and decode the batch back
let mut r = ReaderBuilder::new().build(Cursor::new(bytes))?;
let out = r.next().unwrap()?;
assert_eq!(out.num_rows(), 2);

Aliased Type§

pub struct AvroWriter<W> {
    writer: W,
    schema: Arc<Schema>,
    format: AvroOcfFormat,
    compression: Option<CompressionCodec>,
    capacity: usize,
    encoder: RecordEncoder,
}

Fields§

§writer: W§schema: Arc<Schema>§format: AvroOcfFormat§compression: Option<CompressionCodec>§capacity: usize§encoder: RecordEncoder