pub struct ReaderBuilder {
batch_size: usize,
strict_mode: bool,
utf8_view: bool,
reader_schema: Option<AvroSchema>,
writer_schema_store: Option<SchemaStore>,
active_fingerprint: Option<Fingerprint>,
}展开描述
一个用于配置和构建 Avro 读取器和解码器的构建器。
ReaderBuilder 是此模块的主要入口点。它支持
- 通过
Self::build进行 OCF 读取,返回任何BufRead上的Reader; - 通过
Self::build_decoder进行流式解码,返回一个Decoder。
§选项
batch_size: 每个RecordBatch的最大行数(默认值:1024)。请参见Self::with_batch_size。utf8_view: 对字符串列使用 ArrowStringViewArray(默认值:false)。请参见Self::with_utf8_view。strict_mode: 启用更严格的联合处理(默认值:false)。请参见Self::with_strict_mode。reader_schema: 解码值时使用的可选读取器模式(投影/演进)(默认值:None)。请参见Self::with_reader_schema。writer_schema_store: 构建用于单对象或 Confluent 帧的Decoder所必需。将指纹映射到 Avro 模式。请参见Self::with_writer_schema_store。active_fingerprint: 当第一个帧省略指纹时(罕见),用于流式解码的可选起始指纹。请参见Self::with_active_fingerprint。
§示例
以 4096 行的批次读取 OCF 文件
use std::fs::File;
use std::io::BufReader;
use arrow_avro::reader::ReaderBuilder;
let file = File::open("data.avro")?;
let mut reader = ReaderBuilder::new()
.with_batch_size(4096)
.build(BufReader::new(file))?;为 Confluent 消息构建一个 Decoder
use arrow_avro::schema::{AvroSchema, SchemaStore, Fingerprint, FingerprintAlgorithm};
use arrow_avro::reader::ReaderBuilder;
let mut store = SchemaStore::new_with_type(FingerprintAlgorithm::Id);
store.set(Fingerprint::Id(1234), AvroSchema::new(r#"{"type":"record","name":"E","fields":[]}"#.to_string()))?;
let decoder = ReaderBuilder::new()
.with_writer_schema_store(store)
.build_decoder()?;字段§
§batch_size: usize§strict_mode: bool§utf8_view: bool§reader_schema: Option<AvroSchema>§writer_schema_store: Option<SchemaStore>§active_fingerprint: Option<Fingerprint>实现§
源§impl ReaderBuilder
impl ReaderBuilder
源代码pub fn new() -> Self
pub fn new() -> Self
使用默认值创建一个新的 ReaderBuilder
batch_size = 1024strict_mode = falseutf8_view = falsereader_schema = Nonewriter_schema_store = Noneactive_fingerprint = None
fn make_record_decoder( &self, writer_schema: &Schema<'_>, reader_schema: Option<&Schema<'_>>, ) -> Result<RecordDecoder, ArrowError>
fn make_record_decoder_from_schemas( &self, writer_schema: &Schema<'_>, reader_schema: Option<&AvroSchema>, ) -> Result<RecordDecoder, ArrowError>
fn make_decoder_with_parts( &self, active_decoder: RecordDecoder, active_fingerprint: Option<Fingerprint>, cache: IndexMap<Fingerprint, RecordDecoder>, fingerprint_algorithm: FingerprintAlgorithm, ) -> Decoder
fn make_decoder( &self, header: Option<&Header>, reader_schema: Option<&AvroSchema>, ) -> Result<Decoder, ArrowError>
源代码pub fn with_batch_size(self, batch_size: usize) -> Self
pub fn with_batch_size(self, batch_size: usize) -> Self
设置基于行的批处理大小。
每次调用 Decoder::flush 或每次 Reader 迭代都会生成一个包含最多此行数的批次。较大的批次可以减少开销;较小的批次可以减少峰值内存使用和延迟。
源代码pub fn with_utf8_view(self, utf8_view: bool) -> Self
pub fn with_utf8_view(self, utf8_view: bool) -> Self
为 UTF‑8 字符串数据选择 Arrow 的 StringViewArray。
启用后,文本 Avro 字段将加载到 Arrow 的 StringViewArray 中,而不是标准的 StringArray。这可以通过减少分配来提高处理许多短字符串的工作负载的性能。
源代码pub fn use_utf8view(&self) -> bool
pub fn use_utf8view(&self) -> bool
返回是否为字符串数据启用了 StringViewArray。
源代码pub fn with_strict_mode(self, strict_mode: bool) -> Self
pub fn with_strict_mode(self, strict_mode: bool) -> Self
对某些 Avro 联合(例如 [T, "null"])启用更严格的行为。
当 true 时,原本会被强制转换的模糊或有损联合可能会改为产生描述性错误。使用此选项可在摄取早期捕获模式问题。
源代码pub fn with_reader_schema(self, schema: AvroSchema) -> Self
pub fn with_reader_schema(self, schema: AvroSchema) -> Self
设置解码期间使用的读取器模式。
如果未提供,则直接使用 OCF 头部中的写入器模式(对于 Reader)或从指纹查找的模式(对于 Decoder)。
读取器模式可用于模式演进或投影。
源代码pub fn with_writer_schema_store(self, store: SchemaStore) -> Self
pub fn with_writer_schema_store(self, store: SchemaStore) -> Self
设置用于通过指纹解析写入器模式的 SchemaStore。
在构建用于单对象编码或 Confluent 线格式的 Decoder 时,这是必需的。该存储将指纹(Rabin / MD5 / SHA‑256 / ID)映射到完整的 Avro 模式。
默认为 None。
源代码pub fn with_active_fingerprint(self, fp: Fingerprint) -> Self
pub fn with_active_fingerprint(self, fp: Fingerprint) -> Self
设置流式解码的初始模式指纹。
这对于在第一个记录主体之前不包含指纹的流(不常见)很有用。如果未设置,则使用第一个观察到的指纹。