Adding StructType columns to Spark DataFrames

Nachiket Rajput
2 min readJan 17, 2018

--

StructType objects define the schema of Spark DataFrames. StructType objects contain a list of StructField objects that define the name, type, and nullable flag for each column in a DataFrame.

Let’s start with an overview of StructType objects and then demonstrate how StructType columns can be added to DataFrame schemas (essentially creating a nested schema).

StructType columns are a great way to eliminate order dependencies from Spark code.

StructType overview

The StructType case class can be used to define a DataFrame schema as follows.

The DataFrame schema method returns a StructType object.

StructType(print(df.schema) 
StructField(num, IntegerType, true),
StructField(letter, StringType, true)
)

Let’s lookat another example to see how StructType columns can be appended to DataFrames.

Appending StructType columns

Let’s use the function to append a StructType column to a DataFrame.

Let’s take a look at the schema.

StructType(print(actualDF.schema) 
StructField(weight,DoubleType,true),
StructField(animal_type,StringType,true),
StructField(animal_interpretation, StructType(
StructField(is_large_animal,BooleanType,true),
StructField(is_mammal,BooleanType,true)
), false)
)

The animal_interpretation column has a StructType type - this DataFrame has a nested schema.

It’s easier to view the schema with the printSchema method.

actualDF.printSchema() root
|-- weight: double (nullable = true)
|-- animal_type: string (nullable = true)
|-- animal_interpretation: struct (nullable = false)
| |-- is_large_animal: boolean (nullable = true)
| |-- is_mammal: boolean (nullable = true)

We can flatten the DataFrame as follows.

Using StructTypes to eliminate order dependencies

Let’s demonstrate some order dependent code and then use a StructType column to eliminate the order dependencies.

Let’s consider three custom transformations that add is_teenager, has_positive_mood, and what_to_do columns to a DataFrame.

Notice that both the withIsTeenager and withHasPositiveMood transformations must be run before the withWhatToDo transformation can be run. The functions have an order dependency because they must be run in a certain order for the code to work.

Let’s build a DataFrame and execute the functions in the right order so the code will run.

Let’s use the struct function to append a StructType column to the DataFrame and remove the order depenencies from this code.

Order dependencies can be a big problem in large Spark codebases

If you’re code is organized as DataFrame transformations, order dependencies can become a big problem.

You might need to figure out how to call 20 functions in exactly the right order to get the desired result.

StructType columns are one way to eliminate order dependencies from your code. I’ll discuss other strategies in more detail in a future blog post!

Originally published at https://medium.com on January 17, 2018.

--

--

Nachiket Rajput
Nachiket Rajput

Written by Nachiket Rajput

Developer | Writer | Career Generalist | Blogger | Community Person

No responses yet