Werken met ORC-bestanden

Apache ORC is een kolombestandsindeling die optimalisaties biedt voor het versnellen van query's. Het is efficiƫnter dan CSV of JSON. Azure Databricks ondersteunt ORC voor zowel lezen als schrijven met Apache Spark. Zie de Apache Spark-documentatie over ORC-bestanden voor meer informatie.

Vereiste voorwaarden

Azure Databricks vereist geen aanvullende configuratie voor het gebruik van ORC-bestanden. Als u ORC-bestanden wilt streamen, hebt u echter automatische laadprogramma's nodig.

ORC configureren en gebruiken met de DataFrame-API

Gebruik de Apache Spark DataFrame-API om ORC-bestanden te lezen en te schrijven wanneer u volledige controle nodig hebt over het schema, partitionering of schrijfgedrag.

Opties voor lezen en schrijven

Raadpleeg de volgende Apache Spark-referentieartikelen voor ondersteunde lees- en schrijfopties voor de DataFrame-API.

ORC-bestanden lezen en schrijven

Lees bijvoorbeeld data.orc in een DataFrame df en schrijf het naar 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-bestanden lezen met schemaspecificatie

Geef een schema op bij het lezen van ORC-bestanden om de overhead van schemadeductie te voorkomen. Definieer bijvoorbeeld een schema met de velden name, age en city, en lees data.orc in een 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;

Gepartitioneerde ORC-bestanden schrijven

Schrijf gepartitioneerde ORC-bestanden voor geoptimaliseerde queryprestaties op grote gegevenssets. Maak bijvoorbeeld een DataFrame df met year, month, name en amount kolommen en schrijf deze naar partitioned_orc, gepartitioneerd door year en 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);

ORC-bestanden lezen met SQL

Gebruik read_files dit om query's uit te voeren op ORC-bestanden rechtstreeks vanuit cloudopslag met behulp van SQL zonder een tabel te maken. U kunt bijvoorbeeld een query uitvoeren op een ORC-bestand dat is opgeslagen in cloudopslag met behulp van het pad naar het bestand en de orc indelingsaanduiding.

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

ORC-compressie instellen

ORC-compressie configureren met behulp van de compression optie. Ondersteunde codecs zijn onder andere none, snappyen zliblzo. Schrijf bijvoorbeeld df naar compressed_orc met behulp van zlib compressie, of naar snappy_orc met behulp van snappy compressie.

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');