Is Spark read csv a lazy operation or eager?

Viewed 1540

I've read some resources claiming that Spark read operations are generally lazy. But I've run some jobs that took a long time on the csv read step. Then I read this article saying csv read is an eager operation[1]. Do you have a more definitive answer with reference? Thank you!

1.https://towardsdatascience.com/a-brief-introduction-to-pyspark-ff4284701873

Try minimizing eager operations: In order for your pipeline to be as scalable as possible, it’s good to avoid eager operations that pull full dataframes into memory. I’ve noticed that reading in CSVs is an eager operation, and my work around is to save the dataframe as parquet and then reload it from parquet to build more scalable pipelines.

1 Answers

After reading the source code it appears read CSV is lazy if inferSchema option is disabled:

If the schema is not specified using schema function and inferSchema option is enabled, this function goes through the input once to determine the input schema.

If the schema is not specified using schema function and inferSchema option is disabled, it determines the columns as string types and it reads only the first line to determine the names and the number of fields.

If the enforceSchema is set to false, only the CSV header in the first line is checked to conform specified or inferred schema.

2021 July edit: Source: https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/DataFrameReader.scala#L560

Related