ORCファイルを扱う
Apache ORCは、クエリの高速化のための最適化を提供するカラム型ファイル形式です。CSVやJSONよりも効率的です。Databricksは、Apache Sparkとの連携において、読み書き両方でORCをサポートしています。詳細については、 Apache SparkのORCファイルに関するドキュメントを参照してください。
前提条件
Databricksでは、ORCファイルを使用するために追加の設定は必要ありません。ただし、 ORCファイルをストリームするには、 Auto Loader必要です。
DataFrame API で ORC を設定して使用する
スキーマ、パーティショニング、書き込み動作を完全に制御する必要がある場合はApache Spark DataFrame APIを使用してwrite.orcファイルを読み書きしてください。
読み取りおよび書き込みオプション
サポートされているDataFrame APIの読み書きオプションについては、以下のApache Sparkリファレンス記事を参照してください。
orcファイルのwrite.orc
例えば、 data.orc DataFrame dfに読み込み、 orc_outputに書き込みます。
- Python
- Scala
- SQL
# 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")
// 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")
-- 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;
スキーマ仕様を含むread.orcファイルを読み込みます。
ORCファイルを読み込む際にスキーマを指定することで、スキーマ推論のオーバーヘッドを回避できます。例えば、 name 、 age 、 cityフィールドを持つスキーマを定義し、 data.orc DataFrame dfに読み込みます。
- Python
- Scala
- SQL
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()
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()
-- 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ファイルを作成します。例えば、列がyear 、 month 、 name 、 amountである DataFrame dfを作成し、 yearとmonthで分割されたpartitioned_orcに書き込みます。
- Python
- Scala
- SQL
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")
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")
-- 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.orc
read_filesを使用すると、テーブルを作成せずに SQL を使用してクラウド ストレージから ORC ファイルを直接クエリできます。例えば、クラウドストレージに保存されているORCファイルを、ファイルへのパスとorcフォーマット指定子を使用してクエリします。
SELECT * FROM read_files(
's3://<bucket>/<path>/<file>.orc',
format => 'orc'
)
ORC圧縮を設定する
compressionオプションを使用してORC圧縮を設定します。サポートされているコーデックにはnone 、 snappy 、 zlib 、およびlzoが含まれます。例えば、 zlib圧縮を使用してdfをcompressed_orcに書き込むか、 snappy圧縮を使用してsnappy_orcに書き込む。
- Python
- Scala
- SQL
# 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")
// 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")
-- 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');