讀取和寫入 ORC 檔案#

Apache ORC 專案提供了一個標準化的開放原始碼欄狀儲存格式,用於資料分析系統中。它最初是為在 Apache Hadoop 中與 Apache DrillApache HiveApache ImpalaApache Spark 等系統一起使用而建立的,這些系統採用它作為高效能資料 IO 的共享標準。

Apache Arrow 是讀取或寫入 ORC 檔案的理想記憶體內表示層。

支援的 ORC 功能#

ORC 格式有許多功能,我們僅支援其中一部分。

資料類型#

以下是 ORC 類型和對應的 Arrow 類型列表。

邏輯類型

對應的 Arrow 類型

註解

BOOLEAN

布林值

BYTE

Int8

SHORT

Int16

INT

Int32

LONG

Int64

FLOAT

Float32

DOUBLE

Float64

STRING

字串/大型字串

(1)

BINARY

二進位/大型二進位/固定大小二進位

(1)

TIMESTAMP

時間戳記/Date64

(1) (2)

TIMESTAMP_INSTANT

時間戳記

(2)

LIST

列表/大型列表/固定大小列表

(1)

MAP

映射

STRUCT

結構

UNION

稀疏聯合/密集聯合

(1)

DECIMAL

Decimal128/Decimal256

(1)

DATE

Date32

VARCHAR

字串

(3)

CHAR

字串

(3)

  • (1) 在讀取端,ORC 類型會讀取為表格中第一個對應的 Arrow 類型。

  • (2) 在寫入端,當提供時區時,會使用 ORC TIMESTAMP_INSTANT,否則使用 ORC TIMESTAMP。在讀取端,ORC TIMESTAMP 和 TIMESTAMP_INSTANT 類型都會讀取為具有 arrow::TimeUnit::NANO 的 Arrow Timestamp 類型,且僅針對 ORC TIMESTAMP_INSTANT 類型將時區設定為 UTC。

  • (3) 在讀取端,ORC CHAR 和 VARCHAR 類型都會讀取為 Arrow 字串類型。寫入端不支援 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 = ...;

    // Open ORC file reader
    auto maybe_reader = arrow::adapters::orc::ORCFileReader::Open(input, pool);
    if (!maybe_reader.ok()) {
        // Handle error instantiating file reader...
    }
    std::unique_ptr<arrow::adapters::orc::ORCFileReader> reader = maybe_reader.ValueOrDie();

    // Read entire file as a single Arrow table
    auto maybe_table = reader->Read();
    if (!maybe_table.ok()) {
        // Handle error reading ORC data...
    }
    std::shared_ptr<arrow::Table> table = maybe_table.ValueOrDie();
}

寫入 ORC 檔案#

ORCFileWriter#

ORC 檔案會寫入到 OutputStream

#include <arrow/adapters/orc/adapter.h>
{
    // Oneshot write
    // ...
    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()) {
       // Handle error instantiating file writer...
    }
    std::unique_ptr<arrow::adapters::orc::ORCFileWriter> writer = maybe_writer.ValueOrDie();
    if (!(writer->Write(*input_table)).ok()) {
        // Handle write error...
    }
    if (!(writer->Close()).ok()) {
        // Handle close error...
    }
}