Reading and Writing ORC files

The Apache ORC project provides a standardized open-source columnar storage format for use in data analysis systems. It was created originally for use in Apache Hadoop with systems like Apache Drill, Apache Hive, Apache Impala, and Apache Spark adopting it as a shared standard for high performance data IO.

Apache Arrow is an ideal in-memory representation layer for data that is being read or written with ORC files.

Supported ORC features

The ORC format has many features, and we support a subset of them.

Data types

Here are a list of ORC types and mapped Arrow types.

Logical type

Mapped Arrow type

Notes

BOOLEAN

Boolean

BYTE

Int8

SHORT

Int16

INT

Int32

LONG

Int64

FLOAT

Float32

DOUBLE

Float64

STRING

String/LargeString

(2)

BINARY

Binary/LargeBinary/FixedSizeBinary

(2)

TIMESTAMP

Timestamp/Date64

(2) (3)

LIST

List/LargeList/FixedSizeList

(2)

MAP

Map

STRUCT

Struct

UNION

DenseUnion/SparseUnion

(1)

DECIMAL

Decimal128/Decimal64

(2)

DATE

Date32

VARCHAR

String

Unsupported ORC types: CHAR, TIMESTAMP_INSTANT.

  • (1) We do not support writing UNION types.

  • (2) On the read side the ORC type is read as the first corresponding Arrow type in the table.

  • (3) On the read side the ORC TIMESTAMP type is read as the Arrow Timestamp type with arrow::TimeUnit::NANO. Also we currently don’t support timezones.

Compression

Compression codec

SNAPPY

GZIP/ZLIB

LZ4

ZSTD

Unsupported compression codec: LZO.

Reading ORC Files

The ORCFileReader class reads data for an entire file or stripe into an ::arrow::Table.

ORCFileReader

The ORCFileReader class requires a ::arrow::io::RandomAccessFile instance representing the input file.

#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();
}

Writing ORC Files

ORCFileWriter

An ORC file is written to a 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...
    }
}