arrow系列22---读写ORC文件
作者:yunjinqi   类别:    日期:2023-10-16 20:40:46    阅读:126 次   消耗积分:0 分    


读写ORC文件 

Apache ORC项目提供了一个标准的开源列式存储格式,用于数据分析系统。最初是为了在Apache Hadoop中使用,随后被Apache Drill、Apache Hive、Apache Impala和Apache Spark等系统采用为高性能数据IO的共享标准。

Apache Arrow是用于读取或写入ORC文件的理想内存表示层。

另请参见

ORC读取器/写入器API参考。

支持的ORC特性 ORC格式有许多特性,我们支持其中的一部分。

数据类型 

以下是ORC类型和映射的Arrow类型的列表。

截图 2023-10-16 20-38-11.png

  • (1) 在读取时,ORC类型将被读取为表中表中的第一个对应的Arrow类型。

  • (2) 在写入时,如果提供了时区,ORC TIMESTAMP_INSTANT 将被使用,否则将使用ORC TIMESTAMP。在读取时,ORC TIMESTAMP和TIMESTAMP_INSTANT 类型都将被读取为Arrow Timestamp类型,arrow::TimeUnit::NANO,并且时区仅对ORC TIMESTAMP_INSTANT类型设置为UTC。

  • (3) 在读取时,ORC CHAR和VARCHAR类型都将被读取为Arrow String类型。ORC CHAR和VARCHAR类型不支持写入。

压缩 

压缩编解码器

  • SNAPPY

  • GZIP/ZLIB

  • LZ4

  • ZSTD

  • 不支持的压缩编解码器:LZO。

读取ORC文件 

ORCFileReader类将整个文件或条纹的数据读取到一个::arrow::Table中。

ORCFileReader ORCFileReader类需要一个表示输入文件的::arrow::io::RandomAccessFile实例。

#include <arrow/adapters/orc/adapter.h>{    // ...
    arrow::Status st;
    arrow::MemoryPool* pool = default_memory_pool();
    std::shared_ptr<arrow::io::RandomAccessFile> input = ...;    // 打开ORC文件读取器
    auto maybe_reader = arrow::adapters::orc::ORCFileReader::Open(input, pool);    
    if (!maybe_reader.ok()) {        // 处理文件读取器实例化错误...
    }
    std::unique_ptr<arrow::adapters::orc::ORCFileReader> reader = maybe_reader.ValueOrDie();    // 将整个文件读取为单个Arrow表
    auto maybe_table = reader->Read();    
    if (!maybe_table.ok()) {        // 处理读取ORC数据错误...
    }
    std::shared_ptr<arrow::Table> table = maybe_table.ValueOrDie();
}

写入ORC文件 ORCFileWriter 将ORC文件写入OutputStream。

#include <arrow/adapters/orc/adapter.h>{    // 单次写入
    // ...
    std::shared_ptr<arrow::io::OutputStream> output = ...;    
    auto writer_options = WriterOptions();    
    auto maybe_writer = arrow::adapters::orc::ORCFileWriter::Open(output.get(), writer_options);    
    if (!maybe_writer.ok()) {        // 处理文件写入器实例化错误...
    }
    std::unique_ptr<arrow::adapters::orc::ORCFileWriter> writer = maybe_writer.ValueOrDie();    
    if (!(writer->Write(*input_table)).ok()) {        // 处理写入错误...
    }    
    if (!(writer->Close()).ok()) {        // 处理关闭错误...
    }
}


版权所有,转载本站文章请注明出处:云子量化, http://www.woniunote.com/article/355
上一篇:arrow系列21---读写Arrow IPC格式
下一篇:arrow系列23---读写Parquet文件