Condividi tramite


Classe DataFrameReader

Interfaccia usata per caricare un dataframe da sistemi di archiviazione esterni (ad esempio file system, archivi chiave-valore e così via).

Supporta Spark Connect

Sintassi

Usare SparkSession.read per accedere a questa interfaccia.

Methods

metodo Descrizione
format(source) Specifica il formato dell'origine dati di input.
schema(schema) Specifica lo schema di input.
option(key, value) Aggiunge un'opzione di input per l'origine dati sottostante.
options(**options) Aggiunge opzioni di input per l'origine dati sottostante.
load(path, format, schema, **options) Carica i dati da un'origine dati e lo restituisce come dataframe.
json(path, schema, ...) Carica i file JSON e restituisce i risultati come dataframe.
table(tableName) Restituisce la tabella specificata come dataframe.
parquet(*paths, **options) Carica i file Parquet, restituendo il risultato come dataframe.
text(paths, wholetext, lineSep, ...) Carica i file di testo e restituisce un dataframe il cui schema inizia con una colonna stringa denominata "value".
csv(path, schema, sep, encoding, ...) Carica un file CSV e restituisce il risultato come dataframe.
xml(path, rowTag, schema, ...) Carica un file XML e restituisce il risultato come dataframe.
excel(path, dataAddress, headerRows, ...) Carica i file di Excel, restituendo il risultato come dataframe.
orc(path, mergeSchema, pathGlobFilter, ...) Carica i file ORC, restituendo il risultato come dataframe.
jdbc(url, table, column, lowerBound, upperBound, numPartitions, predicates, properties) Costruire un dataframe che rappresenta la tabella di database denominata tabella accessibile tramite l'URL JDBC e le proprietà di connessione.

Examples

Lettura da origini dati diverse

# Access DataFrameReader through SparkSession
spark.read

# Read JSON file
df = spark.read.json("path/to/file.json")

# Read CSV file with options
df = spark.read.option("header", "true").csv("path/to/file.csv")

# Read Parquet file
df = spark.read.parquet("path/to/file.parquet")

# Read from a table
df = spark.read.table("table_name")

Uso del formato e del caricamento

# Specify format explicitly
df = spark.read.format("json").load("path/to/file.json")

# With options
df = spark.read.format("csv") \
    .option("header", "true") \
    .option("inferSchema", "true") \
    .load("path/to/file.csv")

Specifica dello schema

from pyspark.sql.types import StructType, StructField, StringType, IntegerType

# Define schema
schema = StructType([
    StructField("name", StringType(), True),
    StructField("age", IntegerType(), True)
])

# Read CSV with schema
df = spark.read.schema(schema).csv("path/to/file.csv")

# Read CSV with DDL-formatted string schema
df = spark.read.schema("name STRING, age INT").csv("path/to/file.csv")

Lettura da JDBC

# Read from database table
df = spark.read.jdbc(
    url="jdbc:postgresql://localhost:5432/mydb",
    table="users",
    properties={"user": "myuser", "password": "mypassword"}
)

# Read with partitioning for parallel loading
df = spark.read.jdbc(
    url="jdbc:postgresql://localhost:5432/mydb",
    table="users",
    column="id",
    lowerBound=1,
    upperBound=1000,
    numPartitions=10,
    properties={"user": "myuser", "password": "mypassword"}
)

Concatenamento dei metodi

# Chain multiple configuration methods
df = spark.read \
    .format("csv") \
    .option("header", "true") \
    .option("inferSchema", "true") \
    .option("delimiter", ",") \
    .schema("name STRING, age INT") \
    .load("path/to/file.csv")