Reading: csv, json & parquet
spark.read returns a DataFrameReader. Pick a format (or use the shortcut methods), set options, then point it at a path. The same fluent shape works for every source.
spark.read.format("csv").option("header", "true").load("path")
spark.read.csv("path") spark.read.json("path") spark.read.parquet("path")
spark.read.option("header", true).option("inferSchema", true).csv("orders.csv")val orders = spark.read
.option("header", "true")
.option("inferSchema", "true")
.csv("s3://bucket/orders.csv")
orders.printSchema()
orders.show(3, false)| id | product | amount | qty |
|---|---|---|---|
| 1 | Wireless Mouse | 25.0 | 2 |
| 2 | Notebook | 6.5 | 3 |
| 3 | Mechanical Keyboard | 80.0 | 1 |
header/inferSchema needed, and they carry types and column stats, so prefer them over CSV/JSON for anything downstream. See Parquet vs ORC vs Avro for choosing a columnar format.