Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Apache ORC è un formato di file a colonne ottimizzato per carichi di lavoro analitici su larga scala. Usa indici e statistiche predefiniti per ignorare i dati irrilevanti durante le letture. Azure Databricks supporta ORC sia per la lettura che per la scrittura con Apache Spark, incluse le specifiche dello schema, il partizionamento e la compressione di scrittura.
Prerequisiti
Azure Databricks non richiede una configurazione aggiuntiva per l'uso di file ORC. Tuttavia, per trasmettere i file ORC, è necessario il caricatore automatico.
Options
Usare i metodi .option() e .options() di DataFrameReader e DataFrameWriter per configurare le origini dati ORC. Per un elenco completo delle opzioni supportate, vedere DataFrameReader Opzioni ORC e DataFrameWriter opzioni ORC.
Usage
Gli esempi seguenti usano il set di dati di esempio Wanderbricks per illustrare la lettura e la scrittura di file ORC usando l'API DataFrame Spark e SQL.
Leggere e scrivere file ORC
Python
# Write wanderbricks reviews to ORC format
df = spark.read.table("samples.wanderbricks.reviews")
df.write.format("orc").save("/Volumes/<catalog>/<schema>/<volume>/reviews_orc")
# Read an ORC file into a DataFrame
df = spark.read.format("orc").load("/Volumes/<catalog>/<schema>/<volume>/reviews_orc")
display(df)
# Write with overwrite mode
df.write.format("orc").mode("overwrite").save("/Volumes/<catalog>/<schema>/<volume>/reviews_orc")
Scala
// Write wanderbricks reviews to ORC format
val reviews = spark.read.table("samples.wanderbricks.reviews")
reviews.write.format("orc").save("/Volumes/<catalog>/<schema>/<volume>/reviews_orc")
// Read an ORC file into a DataFrame
val df = spark.read.format("orc").load("/Volumes/<catalog>/<schema>/<volume>/reviews_orc")
df.show()
// Write with overwrite mode
df.write.format("orc").mode("overwrite").save("/Volumes/<catalog>/<schema>/<volume>/reviews_orc")
SQL
-- Write wanderbricks reviews to ORC format
CREATE TABLE reviews_orc
USING ORC
AS SELECT * FROM samples.wanderbricks.reviews;
SELECT * FROM reviews_orc;
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.
SELECT * FROM read_files(
'/Volumes/<catalog>/<schema>/<volume>/reviews_orc',
format => 'orc'
)
Specificare uno 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 review_id, rating e comment e leggere reviews_orc in un DataFrame.
Python
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
schema = StructType([
StructField("review_id", StringType(), True),
StructField("rating", IntegerType(), True),
StructField("comment", StringType(), True)
])
df = spark.read.format("orc").schema(schema).load("/Volumes/<catalog>/<schema>/<volume>/reviews_orc")
df.printSchema()
df.show()
Scala
import org.apache.spark.sql.types.{StructType, StructField, StringType, IntegerType}
val schema = StructType(Array(
StructField("review_id", StringType, nullable = true),
StructField("rating", IntegerType, nullable = true),
StructField("comment", StringType, nullable = true)
))
val df = spark.read.format("orc").schema(schema).load("/Volumes/<catalog>/<schema>/<volume>/reviews_orc")
df.printSchema()
df.show()
SQL
-- Create a table with an explicit schema from ORC files
CREATE TABLE reviews_orc (
review_id STRING,
rating INT,
comment STRING
)
USING ORC
OPTIONS (path "/Volumes/<catalog>/<schema>/<volume>/reviews_orc");
SELECT * FROM reviews_orc;
Scrivere file ORC partizionati
Scrivere file ORC partizionati per ottimizzare le prestazioni delle query in set di dati di grandi dimensioni. Ad esempio, leggere samples.wanderbricks.bookings e scrivere in bookings_orc_partitioned, partizionato per year e month derivati dalla colonna check_in.
Python
from pyspark.sql.functions import year, month
df = spark.read.table("samples.wanderbricks.bookings")
df_with_parts = df.withColumn("year", year("check_in")).withColumn("month", month("check_in"))
df_with_parts.write.format("orc").partitionBy("year", "month").save("/Volumes/<catalog>/<schema>/<volume>/bookings_orc_partitioned")
Scala
import org.apache.spark.sql.functions.{year, month}
val bookings = spark.read.table("samples.wanderbricks.bookings")
val bookingsWithParts = bookings.withColumn("year", year(col("check_in"))).withColumn("month", month(col("check_in")))
bookingsWithParts.write.format("orc").partitionBy("year", "month").save("/Volumes/<catalog>/<schema>/<volume>/bookings_orc_partitioned")
SQL
-- Write partitioned ORC files by year and month
CREATE TABLE bookings_orc_partitioned
USING ORC
PARTITIONED BY (year, month)
AS SELECT *, year(check_in) AS year, month(check_in) AS month
FROM samples.wanderbricks.bookings;
Risorse aggiuntive
- Che cos'è Delta Lake in Azure Databricks?: se si esegue la migrazione da un ambiente Hive o Hadoop usando ORC, Delta Lake è il formato nativo di Databricks consigliato. Aggiunge transazioni ACID, imposizione dello schema, tempo di spostamento e prestazioni di lettura ottimizzate oltre all'archiviazione basata su Parquet.
- Leggere e scrivere file Parquet: se il carico di lavoro richiede la compatibilità più ampia dell'ecosistema all'esterno di Databricks, Parquet è il formato a colonne più ampiamente supportato nei motori di query e negli strumenti di archiviazione cloud.