Bekerja dengan file ORC

Apache ORC adalah format file kolom yang menyediakan pengoptimalan untuk mempercepat kueri. Ini lebih efisien daripada CSV atau JSON. Azure Databricks mendukung ORC untuk membaca dan menulis dengan Apache Spark. Untuk informasi selengkapnya, lihat dokumentasi Apache Spark pada File ORC.

Prasyarat

Azure Databricks tidak memerlukan konfigurasi tambahan untuk menggunakan file ORC. Namun, untuk melakukan streaming file ORC, Anda memerlukan Auto Loader.

Mengonfigurasi dan menggunakan ORC dengan API DataFrame

Gunakan Apache Spark DataFrame API untuk membaca dan menulis file ORC saat Anda memerlukan kontrol penuh atas skema, partisi, atau perilaku tulis.

Opsi baca dan tulis

Tinjau artikel referensi Apache Spark berikut untuk opsi baca dan tulis Api DataFrame yang didukung.

Membaca dan menulis file ORC

Misalnya, baca data.orc ke dalam DataFrame df dan tulis ke orc_output.

Python

# Read an ORC file into a DataFrame
df = spark.read.format("orc").load("/tmp/data.orc")
df.show()

# Write a DataFrame to ORC format
df.write.format("orc").save("/tmp/orc_output")

# Write with overwrite mode
df.write.format("orc").mode("overwrite").save("/tmp/orc_output")

Scala

// Read an ORC file into a DataFrame
val df = spark.read.format("orc").load("/tmp/data.orc")
df.show()

// Write a DataFrame to ORC format
df.write.format("orc").save("/tmp/orc_output")

// Write with overwrite mode
df.write.format("orc").mode("overwrite").save("/tmp/orc_output")

SQL

-- Query ORC files directly
SELECT * FROM orc.`/tmp/data.orc`;

-- Create a table from ORC files
CREATE TABLE orc_table
USING ORC
OPTIONS (path "/tmp/data.orc");

SELECT * FROM orc_table;

Membaca file ORC dengan spesifikasi skema

Tentukan skema saat membaca file ORC untuk menghindari overhead inferensi skema. Misalnya, tentukan skema dengan name, age, dan city dan membaca data.orc ke dalam DataFrame df.

Python

from pyspark.sql.types import StructType, StructField, StringType, IntegerType

schema = StructType([
    StructField("name", StringType(), True),
    StructField("age", IntegerType(), True),
    StructField("city", StringType(), True)
])

df = spark.read.format("orc").schema(schema).load("/tmp/data.orc")
df.printSchema()
df.show()

Scala

import org.apache.spark.sql.types.{StructType, StructField, StringType, IntegerType}

val schema = StructType(Array(
  StructField("name", StringType, nullable = true),
  StructField("age", IntegerType, nullable = true),
  StructField("city", StringType, nullable = true)
))

val df = spark.read.format("orc").schema(schema).load("/tmp/data.orc")
df.printSchema()
df.show()

SQL

-- Create a table with an explicit schema from ORC files
CREATE TABLE orc_table (
  name STRING,
  age INT,
  city STRING
)
USING ORC
OPTIONS (path "/tmp/data.orc");

SELECT * FROM orc_table;

Menulis file ORC yang dipartisi

Tulis file ORC yang dipartisi untuk performa kueri yang dioptimalkan pada himpunan data besar. Misalnya, buat DataFrame df dengan kolom year, month, name, dan amount dan tulis ke partitioned_orc yang dipartisi oleh year dan month.

Python

df = spark.createDataFrame(
    [
        (2023, 1, "Alice", 100),
        (2023, 1, "Bob", 200),
        (2023, 2, "Alice", 150),
        (2024, 1, "Alice", 300),
    ],
    ["year", "month", "name", "amount"]
)

# Write partitioned by year and month
df.write.format("orc").partitionBy("year", "month").save("/tmp/partitioned_orc")

Scala

val df = Seq(
  (2023, 1, "Alice", 100),
  (2023, 1, "Bob", 200),
  (2023, 2, "Alice", 150),
  (2024, 1, "Alice", 300)
).toDF("year", "month", "name", "amount")

// Write partitioned by year and month
df.write.format("orc").partitionBy("year", "month").save("/tmp/partitioned_orc")

SQL

-- Create a partitioned ORC table
CREATE TABLE partitioned_orc_table (
  name STRING,
  amount INT
)
USING ORC
PARTITIONED BY (year INT, month INT);

Membaca file ORC menggunakan SQL

Gunakan read_files untuk mengkueri file ORC langsung dari penyimpanan cloud menggunakan SQL tanpa membuat tabel. Misalnya, kueri file ORC yang disimpan di penyimpanan cloud menggunakan jalur ke file dan penentu orc format.

SELECT * FROM read_files(
  's3://<bucket>/<path>/<file>.orc',
  format => 'orc'
)

Menyetel kompresi ORC

Konfigurasikan kompresi ORC menggunakan compression opsi . Codec yang didukung meliputi none, snappy, zlib, dan lzo. Misalnya, tulis df ke compressed_orc menggunakan kompresi zlib, atau ke snappy_orc menggunakan kompresi snappy.

Python

# Write with zlib compression
df.write.format("orc").option("compression", "zlib").save("/tmp/compressed_orc")

# Write with snappy compression (default)
df.write.format("orc").option("compression", "snappy").save("/tmp/snappy_orc")

Scala

// Write with zlib compression
df.write.format("orc").option("compression", "zlib").save("/tmp/compressed_orc")

// Write with snappy compression (default)
df.write.format("orc").option("compression", "snappy").save("/tmp/snappy_orc")

SQL

-- Create an ORC table with zlib compression
CREATE TABLE compressed_orc_table (
  name STRING,
  age INT,
  city STRING
)
USING ORC
TBLPROPERTIES ('orc.compress' = 'ZLIB');

-- Create an ORC table with snappy compression
CREATE TABLE snappy_orc_table (
  name STRING,
  age INT,
  city STRING
)
USING ORC
TBLPROPERTIES ('orc.compress' = 'SNAPPY');