Spark Scala Exercise 2: Load a CSV and Count Rows
Briefly

This exercise details how to load a CSV file into a Spark DataFrame using Scala, demonstrating fundamental Spark concepts including schema inference and header handling. The tutorial covers how to read data from CSV files, explore data with display methods, and count rows efficiently. It highlights Spark's lazy evaluation approach, which means that counting rows only triggers the actual data processing when done explicitly. The hands-on nature of this exercise equips learners with practical skills for real-world data engineering tasks.
By completing this exercise, we learned how to load structured data (CSV) into Spark using Scala, applying simple read and action commands, making it a critical skill for aspiring data engineers.
Spark uses the first line as column names through the header option, while inferSchema option attempts to determine the correct data types automatically.
The count action triggers a full scan of the data, indicating how Spark executes lazy transformations at this point.
df.show(5) allows users to preview the initial rows of the DataFrame, providing an immediate look at the structure and contents of the loaded data.
Read at Medium
[
|
]