Read & Write APIs Reference
Comprehensive file reader & writer configurations for CSV, JSON, Parquet, ORC, Delta Lake, and JDBC databases.
Configures and reads CSV datasets with explicit options for headers, schema inferencing, delimiters, and mode.
Used In: Raw File Ingestion, Log Parsing
spark.read.format('csv')\
.option('header', 'true')\
.option('inferSchema', 'true')\
.option('delimiter', ',')\
.option('mode', 'PERMISSIVE')\
.option('nullValue', 'NA')\
.load(path)from pyspark.sql.types import StructType, StructField, StringType, IntegerType
custom_schema = StructType([
StructField('id', IntegerType(), True),
StructField('name', StringType(), True)
])
df = spark.read.format('csv')\
.option('header', 'true')\
.schema(custom_schema)\
.load('data/*.csv')Parsed DataFrame matching custom_schemaReads JSON documents (JSON Lines format where each line is a valid JSON object) or multiline JSON arrays.
Used In: API Response Ingestion, Event Log Processing
spark.read.option('multiLine', 'true').json(path)df = spark.read\
.option('multiLine', 'true')\
.option('pruneUrl', 'true')\
.json('records.json')DataFrame with infered struct/array column typesReads columnar Parquet or ORC files with automatic metadata inspection, schema merging, and filter pushdown.
Used In: Data Lakes, Analytical Data Warehouses
spark.read.option('mergeSchema', 'true').parquet('path/*.parquet')df = spark.read.parquet('s3a://my-bucket/events/year=2024/')High-speed columnar DataFrameConnects to external SQL databases (PostgreSQL, MySQL, Oracle, SQL Server) over JDBC.
Used In: RDBMS Data Extraction, Data Warehouse Ingestion
spark.read.format('jdbc')\
.option('url', 'jdbc:postgresql://host:5432/dbname')\
.option('dbtable', 'orders')\
.option('user', 'admin')\
.option('password', 'secret')\
.option('partitionColumn', 'order_id')\
.option('lowerBound', '1')\
.option('upperBound', '1000000')\
.option('numPartitions', '20')\
.load()jdbc_df = spark.read.format('jdbc')\
.option('url', 'jdbc:mysql://localhost:3306/prod')\
.option('dbtable', '(SELECT id, amount FROM orders WHERE status="PAID") as filtered_orders')\
.option('user', 'root')\
.option('password', 'pass')\
.load()DataFrame fetched from relational DBPersists DataFrame contents to storage disk with targeted save modes and compression codecs.
Used In: ETL Output Persistence, Data Lake Storage
df.write.mode('overwrite|append|ignore|errorifexists')\
.format('parquet')\
.option('compression', 'snappy|gzip|zstd')\
.save(path)df.write.mode('overwrite')\
.format('parquet')\
.option('compression', 'snappy')\
.save('/mnt/output/clean_data')Saved Parquet files in directoryPartitions output files into sub-directories based on column values (`/year=2024/month=08/`).
Used In: Data Lake Organization, Partition Pruning
df.write.partitionBy('year', 'month').parquet(path)df.write.mode('append')\
.partitionBy('country', 'status')\
.parquet('s3a://data-lake/orders/')Folder structure: orders/country=US/status=PAID/part-000.parquetWrites DataFrame to Delta Lake storage format with ACID transaction guarantees and schema enforcement.
Used In: Lakehouse Architecture, Delta Tables, Medallion Architecture (Bronze/Silver/Gold)
df.write.format('delta')\
.mode('append')\
.option('mergeSchema', 'true')\
.save(path)df.write.format('delta')\
.mode('overwrite')\
.option('overwriteSchema', 'true')\
.save('/delta/events_table')Delta Lake table with `_delta_log/` transaction log