Schemas, Dates, and Results on the Driver
Databricks Spark Developer Associate · Lesson 10 of 18 · 1:14:47
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: explicit schemas (StructType or a DDL string), date and time functions and patterns, sorting, printSchema, and collect, take, toLocalIterator, and toPandas. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Seconds, guesses, and a top ten
Coralpeak's events carry their time as a Unix epoch, a number of seconds, and the nightly job lets Spark guess the types of millions of JSON files. The team needs a fixed schema, readable dates, the top players sorted, and a short list of results back in Python. This lesson covers reading and writing with a schema, working with dates, sorting and printing the schema, and bringing results to the driver.
Reading and writing with a schema
A schema lists the columns of a DataFrame, with their names, their types, and whether they accept nulls. You build it as a StructType, holding one StructField for each column, or as a DDL string, which lists each name followed by its type. Passing it to the reader with the schema method skips inference, which costs an extra pass over CSV or JSON data, and keeps the types the same from one run to the next. With a schema, a value that does not fit its type becomes a null in the default permissive mode, instead of changing the type of the whole column. On the way out, the overwrite mode replaces the existing data at the target, and a format like Parquet stores the schema with the files, so the next read gets the same types back.
Working with dates
A Unix epoch counts the seconds since the first of January, nineteen seventy, in UTC. from_unixtime turns those seconds into a string, by default with the year, month, day, hours, minutes, and seconds, in the time zone of the session, while unix_timestamp goes the other way. to_date turns a string or a timestamp into a date, with an optional pattern, to_timestamp does the same for a full timestamp, and date_format writes a date as a string, with any pattern. In these patterns, a capital M is the month, and a small m is the minute, a mistake that is easy to make. To extract one component, call functions such as year, month, dayofmonth, dayofweek, or hour, on a date or a timestamp column.
Sorting and printing the schema
orderBy, or sort, which is the same method, sorts the rows by one or more columns, in ascending order by default. For descending order, call desc on the column, and asc_nulls_last or desc_nulls_first choose where the nulls go. Sorting the whole DataFrame is a wide transformation, with a shuffle, while sortWithinPartitions only sorts the rows inside each partition. printSchema prints the schema as a tree, with each column, its type, and whether it can be null, while the schema attribute returns it as a StructType, and columns returns the list of names.
Bringing results to the driver
collect is an action that returns every row to the driver, as a list of Row objects, so it only suits a small result, such as the top ten players. take and head return the first rows, first returns a single row, and show only prints rows, without returning them. toLocalIterator also returns every row, but one partition at a time, so the driver only needs the memory of the largest partition. toPandas loads the whole result into the driver as well, as a pandas DataFrame, and asDict turns each Row into a Python dictionary. The other way, createDataFrame builds a DataFrame from a Python list of tuples, rows, or dictionaries, with an optional schema.
Recap
Let's recap. A schema, as a StructType or a DDL string, skips inference and keeps the types stable, and overwrite replaces the data at the target. from_unixtime turns epoch seconds into a string, to_date turns it into a date, and functions like year or month return one component. In date patterns, a capital M is the month, and a small m is the minute. orderBy sorts with a shuffle, and printSchema shows the schema as a tree. And collect returns a list of rows to the driver, while toLocalIterator brings one partition at a time.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
The nightly job reads millions of JSON files. The developer wants to stop spending time on guessing the types, and keep them the same every night. What should they do?
- Set inferSchema to true
- Set multiLine to true
- Pass a StructType to the reader with .schema()
- Call printSchema() after the read
Show the answer
Pass a StructType to the reader with .schema()
A schema given to the reader skips inference, and fixes the types. “Set inferSchema to true” is a CSV option, and it asks for inference, the opposite of the goal. “Set multiLine to true” is for documents spread over several lines, and changes nothing for the types. And “Call printSchema() after the read” only prints the schema, after Spark has already inferred it.
The ts column holds a Unix epoch, in seconds. Which expression gives the date of each event, as a date?
- to_date(from_unixtime("ts"))
- from_unixtime("ts", "yyyy-mm-dd")
- unix_timestamp("ts")
- year(from_unixtime("ts"))
Show the answer
to_date(from_unixtime("ts"))
From_unixtime turns the seconds into a string, and to_date turns that string into a date. “from_unixtime("ts", "yyyy-mm-dd")” is wrong, since a small m means minutes, and the result is still a string. “unix_timestamp("ts")” would go the other way, from a time to a number of seconds. And “year(from_unixtime("ts"))” would return only the year, as a number, not a date.
A job must send every purchase of the day, about 50 million rows, to an outside service from the driver, and the driver has little memory. Which approach fits?
- collect(), then loop over the list
- toPandas(), then loop over the rows
- show() with a large number of rows
- toLocalIterator(), then loop over the rows
Show the answer
toLocalIterator(), then loop over the rows
ToLocalIterator brings one partition at a time, so the driver only needs the memory of the largest partition. “collect(), then loop over the list” would bring all fifty million rows to the driver at once. “toPandas(), then loop over the rows” is the same problem, with a pandas DataFrame. And “show() with a large number of rows” only prints rows, and returns nothing to send.