Adding Custom Schema to Spark Dataframe

Updated On February 12, 2021 | By Mahesh Mogal

In the last blog, we have loaded our data to Spark Dataframe. We have also used "inferschema" option to let spark figure out the schema of the Dataframe on its own. But in many cases, you would like to specify a schema for Dataframe. This will give you much better control over column names and especially data types. Let us see how we can add our custom schema while reading data in Spark.

Adding Custom Schema

In spark, schema is array StructField of type StructType. Each StructType has 4 parameters.

  • Column Name
  • Data type of that column
  • Boolean value indication if values in this column can be null or not
  • Metadata column - this is optional column which can be used to add additional information about column

Let us write our custom schema for our flights data.

Now our data frame has column names and data types which we have specified. If you want to print schema for any dataframe you can use below function.

Using Metadata With Custom Schema

We can add extra information about columns using the metadata filed. This filed takes key-value pairs and we can choose any number of keys and values depending on our needs.

We can check our data frame and its schema now.

custom schema with metadata
Custom schema with Metadata

If you want to check schema with its metadata then we need to use following code. We can read all of schema with this function or also read schema for one column as well.

This is how we can add a custom schema to our dataframes. I hope this helps. See you in the next blog.

Mahesh Mogal

I am passionate about Cloud, Data Analytics, Machine Learning, and Artificial Intelligence. I like to learn and try out new things. I have started blogging about my experience while learning these exciting technologies.

Stay Updated with Latest Blogs

Get latest blogs delivered to your mail directly.

Recent Posts

Spark Join Types With Examples

In this blog, we are going to learn different spark join types. We will also write code and validate data output for each join type to better understand them.

Read More
Integrate Spark with Jupyter Notebook and Visual Studio Code

In this blog, we are going to integrate spark with jupyter notebook and visual studio code to create easy-to-use development environment.

Read More
Reading Data From SQL Tables in Spark

In this blog, we are going to learn about reading data from SQL tables in Spark. We will create Spark data frames from tables and query results as well.

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

2 comments on “Adding Custom Schema to Spark Dataframe”

  1. Hi mahesh

    I have txt file say temp.txt with following data
    a,(1,2)
    b,(1,2)

    readFile=spark.read.format("csv") \
    .option("header",False) \
    .option("inferSchema",True)\
    .load("/home/data/temp.txt")
    Now my question is how to define schema for such type of dataset.

    1. Hello Vicky,
      Thanks for reaching out.
      To solve this issue you need to such data enclosed in quotes
      "a","(1,2)"
      "b","(1,2)"

      Then you can use option("escapeQuotes", "true") while reading data.

      If this is not possible, then it is slightly difficult to get it working.
      You can read entire row as single column. And then format data according to your needs.

      df = spark.read\
      .option("header", "true")\
      .option("inferSchema", "true")\
      .option("sep", "\n")\
      .csv("csv/sample_data1.csv")
      df.show()

      +---------+
      |col1,col2|
      +---------+
      | a,(1,2)|
      | b,(1,2)|
      +---------+

      You will need to run some transformations to get that data in required format.
      Please let me know if you need my help in this.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram