Apache ORC 是一种列式文件格式,用于优化查询速度。 它比 CSV 或 JSON 更高效。 Azure Databricks支持 ORC 以使用 Apache Spark 进行读取和写入。 有关详细信息,请参阅 有关 ORC 文件的 Apache Spark 文档。
先决条件
Azure Databricks不需要其他配置即可使用 ORC 文件。 但是,若要流式传输 ORC 文件,需要自动加载程序。
配置和使用 ORC 通过 DataFrame API
如果需要完全控制架构、分区或写入行为,请使用 Apache Spark 数据帧 API 读取和写入 ORC 文件。
读取和写入选项
查看以下 Apache Spark 参考文章,了解支持的 DataFrame API 读取和写入选项。
读取和写入 ORC 文件
例如,读取data.orc数据帧并将其写入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读取到 DataFramedf中。
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列的数据帧amount,并将其按照partitioned_orc和year分区写入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);
使用 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_orc压缩到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');