Updated March 22, 2023
Introduction to Spark SQL Dataframe
Spark SQL Dataframe is the distributed dataset that stores as a tabular structured format. Dataframe is similar to RDD or resilient distributed dataset for data abstractions. The Spark data frame is optimized and supported through the R language, Python, Scala, and Java data frame APIs. The Spark SQL data frames are sourced from existing RDD, log table, Hive tables, and Structured data files and databases. Spark uses select and filters query functionalities for data analysis. Spark SQL Dataframe supports fault tolerance, in-memory processing as an advanced feature. Spark SQL Dataframes are highly scalable that can process very high volumes of data.
The different sources which generate a dataframe are-
- Existing RDD
- Structured data files and databases
- Hive Tables
Need of Dataframe
The spark community has always tried to bring structure to the data, where spark SQL- dataframes are the steps taken in that direction. The initial API of spark, RDD is for unstructured data where the computations and data are both opaque. Thus there was a requirement to create an API that is able to provide additional benefits of optimization. Below are the few requirements which formed the basis of dataframe-
- Process structured and semi- data
- Multiple data sources
- Integration with multiple programming languages
- The number of operations that can be performed on the data such as select & filter.
How to Create Spark SQL Dataframe?
Before understanding ways of creating a dataframe it is important to understand another concept by which spark applications create dataframe from different sources. This concept is known as sparksession and is the entry point for all the spark functionality. Earlier we had to create sparkConf, sparkContext or sqlContext individually but with sparksession, all are encapsulated under one session where spark acts as a sparksession object.
val spark = SparkSession
Ways of creating a Spark SQL Dataframe
Let’s discuss the two ways of creating a dataframe.
1. From Existing RDD
There are two ways in which a Dataframe can be created through RDD. One way is using reflection which automatically infers the schema of the data and the other approach is to create a schema programmatically and then apply to the RDD.
- By Inferring the Schema
An easy way of converting an RDD to Dataframe is when it contains case classes due to the Spark’s SQL interface. The arguments passed to the case classes are fetched using reflection and it becomes the name of the columns of the table. Sequences and Arrays can also be defined in case classes. The RDD which will be created using the case class can be implicitly converted to Dataframe using the toDF() method.
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
case class Transport(AutoName: String, year: Int)
val Vehicle = sc.textFile("//path//").map(_.split(",")).map(p => Transport(p(0), p(1)).toDF()
A dataframe Vehicle is created and can be registered as a table against which sql statements can be executed.
- By programmatically specifying the Schema
There may be cases where we are not aware of the schema beforehand or scenarios where case classes cannot take more than 22 fields. In such conditions, we use the approach of programmatically creating the schema. Firstly an RDD of rows is created from the original RDD, i.e converting the rdd object from rdd[t] to rdd[row]. Then create a schema using StructType (Table) and StructField (Field) objects. This schema is applied to the RDD of Rows using the createDataFrame method as which resembles the structure of rdd[row] created earlier.
val Vehicle = sc.textFile("//path")
val schema = StructType(Array(StructField("AutoName",StringType,true),StructField("Year",IntegerType,true)))
scala> val rowRDD = vehicle.map(_.split(",")).map(p => org.apache.spark.sql.Row(p(0),p(1).toInt))
val vehicleSchemaRDD = sqlContext.applySchema(rowRDD, schema)
2. Through Data Sources
Spark allows the creation of dataframes through multiple sources such as hive, json, parquet, csv and text files that can also be used to create dataframes.
Val file=sqlContext.read.json(“path to the json file”)
Val file=sqlContext.read.csv(“path to the json file”)
Val file=sqlContext.read.text(“path to the json file”)
val hiveData = new org.apache.spark.sql.hive.HiveContext(sc)
val hiveDF = hiveData.sql(“select * from tablename”)
As the data is stored in a tabular format along with the schema, there are a number of operations that can be performed on the dataframes. It allows multiple operations that can be performed on data in dataframes.
Consider file is a dataframe which has been created from a csv file with two columns – FullName and AgePerPA
1. printSchema()- To view the schema structure
// |-- AgePerPA: long (nullable = true)
// |-- FullName: string (nullable = true)
2. select- Similar to select statement in SQL, showcases the data as mentioned in the select statement.
// | name|
// | Bala|
3. Filter- To view the filtered data from the dataframe. The condition mentioned in the command
file.filter($"AgePerPA" > 18).show()
4. GroupBy- To groupby the values
5. show()- to display the contents of dataframe
Though with dataframes you can catch SQL syntax error at compile time itself, it is not capable of handling any analysis related error until runtime. For example, if a non-existing column name is being refered in the code it won’t be noticed until runtime. This would lead to wasting the developer’s time and project cost.
This article gives an overall picture(need, creation, limitations) about the dataframe API of Spark SQL. Due to the popularity of dataframe APIs Spark SQL remains one of the widely used libraries. Just like an RDD, it provides features like fault tolerance, lazy evaluation, in-memory processing along with some additional benefits. It can be defined as data distributed across the cluster in a tabular form. Thus a data frame will have a schema associated with it and can be created through multiple sources via spark session object.
This is a guide to Spark SQL Dataframe. Here we discuss the basic concept, need, and 2 ways of creating a dataframe with limitations. You may also look at the following article to learn more –