Read and write files and JDBC sources
Choose schemas, source options and save modes that preserve the input and output contracts.
Read and write files and JDBC sources
Work through the decision
The daily ticket extract is CSV and the station catalog is a JDBC table. The first prototype uses schema inference for both and overwrites the entire output on each run. How should the job define its contracts?
Check your reasoning
Supply a validated CSV schema and the actual header, delimiter and null conventions. Select required columns early. For JDBC, configure a supported driver, connection and table or query, and limit parallel reads to a level the source can sustain. Numeric partitioning bounds control how rows are divided among JDBC partitions. Use an explicit predicate to filter the result set.
Compare the choices
- Explicit input schema
- Declare expected names and types when parsing text or creating a DataFrame. Validate rejected or null fields and handle schema drift intentionally.
- JDBC partition settings
- Choose supported partition column and bounds, partition count and source query carefully. Coordinate connection concurrency with the source database.
- Append mode
- Add output rows to an existing destination when its provider supports the operation. Prevent duplicate writes if the same input batch can be replayed.
- Overwrite mode
- Replace data in the defined write scope. Confirm table format, partition overwrite behavior and target path before a destructive write.
- Direct file query
- Use a supported SQL data source and path for a one-off query without creating a catalog table. Confirm the provider and file access are available.
Test the decision
File sources handle different formats differently. A SQL query can address a supported provider and escaped path, such as parquet followed by a backtick-quoted path. Direct CSV queries without options may expose generated column names. To control parsing and names, read with options and expose the resulting DataFrame as a view. Delta access requires a runtime with the Delta provider configured. Verify append, overwrite, ignore and error behavior on disposable data before selecting a save mode.
Choose how SQL reads a path
Spark SQL accepts a provider and a backtick-quoted path in the FROM clause. Parquet and ORC carry a schema. JSON can infer one. CSV commonly needs reader options for field types and headers, and text supplies a value column with one line per row. For a Delta table, use the Delta provider on a runtime with Delta support. Write modes control what happens at an existing target. For a plain file export, overwrite alone does not provide transactional replacement.
Apache Spark · SQL data sources and save modes
Query a disposable ORC export by path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as tmp:
path = tmp + "/stops.orc"
spark.createDataFrame([("A",), ("B",)], "station string").write.orc(path)
print(spark.sql(f"SELECT count(*) AS n FROM orc.`{path}`").first().n)
Check the result
The query returns 2. Spark gets the field types from the ORC data. Repeating the write to the same existing path with the default save mode raises an error. Appending the same records would make the count 4.
Practice the guide skills
- Plan a schema for ticket files and a partitioned JDBC read, then choose a bounded write parallelism and safe destination mode. Watch 43:00 · Continue at 45:33 · Continue at 46:31
- Write a SQL query against an ORC or JSON path. Explain the default fields for CSV and text and the provider requirement for a Delta path. Compare the four write modes on a disposable output. Watch 49:30 · Continue at 45:23
Official objectives for this lesson
Related lesson · Data sources
Read and write files and JDBC sources quiz
Answer 3 questions. Check each answer for an explanation.