Condividi tramite


Usare i file ORC

Apache ORC è un formato di file a colonne che fornisce ottimizzazioni per velocizzare le query. È più efficiente rispetto a CSV o JSON. Azure Databricks supporta ORC sia per la lettura che per la scrittura con Apache Spark. Per altre informazioni, vedere la documentazione di Apache Spark sui file ORC.

Prerequisiti

Azure Databricks non richiede una configurazione aggiuntiva per l'uso di file ORC. Tuttavia, per trasmettere i file ORC, è necessario il caricatore automatico.

Configurare e usare ORC con l'API DataFrame

Usare l'API DataFrame di Apache Spark per leggere e scrivere file ORC quando è necessario il controllo completo sullo schema, il partizionamento o il comportamento di scrittura.

Opzioni di lettura e scrittura

Vedere gli articoli di riferimento di Apache Spark seguenti per le opzioni di lettura e scrittura dell'API DataFrame supportate.

Leggere e scrivere file ORC

Ad esempio, leggere data.orc in un dataframe df e scriverlo in 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;

Leggere i file ORC con la specifica dello schema

Specificare uno schema durante la lettura dei file ORC per evitare il sovraccarico dell'inferenza dello schema. Ad esempio, definire uno schema con i campi name, age e city e leggere data.orc in un 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;

Scrivere file ORC partizionati

Scrivere file ORC partizionati per ottimizzare le prestazioni delle query in set di dati di grandi dimensioni. Ad esempio, creare un DataFrame df con le colonne year, month, name e amount e scriverlo su partitioned_orc, partizionato per year e 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);

Leggere i file ORC con SQL

Usare read_files per eseguire query sui file ORC direttamente dall'archiviazione cloud usando SQL senza creare una tabella. Ad esempio, eseguire una query su un file ORC archiviato nell'archiviazione cloud usando il percorso del file e l'identificatore di orc formato.

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

Impostare la compressione ORC

Configurare la compressione ORC usando l'opzione compression . I codec supportati includono none, snappy, zlibe lzo. Ad esempio, scrivere df in compressed_orc usando la zlib compressione o per snappy_orc usare la snappy compressione.

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