Apache ORC 是一種欄位式檔案格式,提供優化以加速查詢。 它比 CSV 或 JSON 更有效率。 Azure Databricks 支援與 Apache Spark 一起使用 ORC 來讀取和寫入。 欲了解更多資訊,請參閱 Apache Spark 關於 ORC 檔案的文件。
先決條件
Azure Databricks 使用 ORC 檔案不需要額外設定。 不過,要串流 ORC 檔案,你需要 Auto Loader。
使用 DataFrame API 來設定並使用 ORC
當你需要完全控制結構、分割或寫入行為時,可以使用 Apache Spark DataFrame API 來讀寫 ORC 檔案。
閱讀與書寫選項
請參考以下 Apache Spark 參考文章,了解支援的 DataFrame API 讀寫選項。
讀取與寫入 ORC 檔案
例如,將 data.orc 讀取成一個 DataFrame df,然後寫入到 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;
讀取帶有結構規範的 ORC 檔案
讀取 ORC 檔案時指定結構,以避免結構推論的額外負擔。 例如,定義一個結構,包含 name、 age和 city 欄位,並將 讀 data.orc 入資料框架 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;
寫入分割的 ORC 檔案
撰寫分區化的 ORC 檔案,以優化大型資料集的查詢效能。 例如,建立一個包含df、year、month和name欄位的DataFrame,並將其寫入amount,按partitioned_orc 和year進行分割。
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);
使用 SQL 讀取 ORC 檔案
用 read_files SQL 直接從雲端儲存查詢 ORC 檔案,不用建立資料表。 例如,查詢儲存在雲端儲存中的 ORC 檔案,使用該檔案的路徑與 orc 格式指定符。
SELECT * FROM read_files(
's3://<bucket>/<path>/<file>.orc',
format => 'orc'
)
設置 ORC 壓縮
請使用選項 compression 設定 ORC 壓縮。 支援的編解碼器包括 none、 snappy、 zliblzo、 和 。 例如,使用df壓縮將compressed_orc寫入zlib,或使用snappy_orcsnappy壓縮將其寫入snappy_orc。
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');