DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK-3-0 practice question 14 of 25
The code block shown below should return the number of columns in the CSV file stored at location filePath. From the CSV file, only lines should be read that do not start with a # character. Choose the answer that correctly fills the blanks in the code block to accomplish this. Code block: __1__(__2__.__3__.csv(filePath, __4__).__5__)
Full set: 180 questionsCorrect code block: len(spark.read.csv(filePath, comment='#').columns) This is a challenging Question: with difficulties in an unusual context: The boundary between DataFrame and the DataFrameReader. It is unlikely that a Question: of this difficulty level appears in the exam. However, solving it helps you get more comfortable with the DataFrameReader, a subject you will likely have to deal with in the exam. Before dealing with the inner parentheses, it is easier to figure out the outer parentheses, gaps 1 and
5. Given the code block, the object in gap 5 would have to be evaluated by the object in gap 1, returning the number of columns in the read-in CSV. One answer option includes DataFrame in gap 1 and shape[0] in gap 2. Since DataFrame cannot be used to evaluate shape[0], we can discard this answer option. Other answer options include size in gap 1. size() is not a built-in Python command, so if we use it, it would have to come from somewhere else. pyspark.sql.functions includes a size() method, but this method only returns the length of an array or map stored within a column (documentation linked below). So, using a size() method is not an option here. This leaves us with two potentially valid answers. We have to pick between gaps 2 and 3 being spark.read or pyspark.DataFrameReader. Looking at the documentation (linked below), the DataFrameReader is actually a child class of pyspark.sql, which means that we cannot import it using pyspark.DataFrameReader. Moreover, spark.read makes sense because on Databricks, spark
current Spark session
(pyspark.sql.SparkSession) and spark.read therefore
returns a DataFrameReader (also see
documentation below). Finally, there is only one correct answer option remaining.
More info:
- pyspark.sql.functions.size — PySpark 3.1.2 documentation
- pyspark.sql.DataFrameReader.csv — PySpark 3.1.2 documentation
- pyspark.sql.SparkSession.read — PySpark 3.1.2 documentation
Static notebook | Dynamic notebook: See test 3,
Keep practicing
Take the free DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK-3-0 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK-3-0 practice questions
- Question 1The code block displayed below contains an error. The code block is intended to perform a…
- Question 2Which of the following code blocks shows the structure of a DataFrame in a tree-like way,…
- Question 3Which of the following code blocks reads the parquet file stored at filePath into DataFra…
- Question 4Which of the following code blocks returns a 2-column DataFrame that shows the distinct v…
- Question 5The code block shown below should return an exact copy of DataFrame transactionsDf that d…
- Question 6The code block displayed below contains an error. The code block should configure Spark t…
