Ways to create DataFrame in Apache Spark [Examples with Code]


Ways to create DataFrame in Apache Spark –

DATAFRAME is the representation of a matrix but we can have columns of different datatypes or similar table with different rows and having different types of columns (values of each column will be same data type).

When working with Spark most of the times you are required to create Dataframe and play around with it. DATAFRAME is nothing but a data structure which is stored in memory and can be created by following ways –

1)Using Case Class
2)Using createDataFrame method
3)Using SQL method
4)Using read..load methods
i) From flat files(JSON, CSV)
ii) From RDBMS Databases

1)Using Case Class

val sqlContext = new org.apache.spark.sql.SQLContext(sc)
import sqlContext.implicits._
case class Employee(name: String, sal: Int)
Below is the sample data available in emp.txt:
Sai, 3000
Hari, 5000
Mahesh, 6000

val emp = sc.textFile(“/Users/sai/Desktop/Files/emp.txt”).
map(_.split(“,”)).map(p => Employee(p(0), p(1).trim.toInt)).toDF()

val empDF = sqlContext.sql(“SELECT name, sal FROM employees”) => “Name: ” + t(0)).collect().foreach(println) => “Name: ” + t.getAs[String](“name”)).collect().foreach(println)[Any](List(“name”, “sal”))).collect().foreach(println)

2)Using createDataFrame method(Specifying the Schema)

val people = sc.textFile(“/Users/sai/Desktop/Files/emp.txt”)
val schemaString = “name,sal”
import org.apache.spark.sql.Row;
import org.apache.spark.sql.types.{StructType,StructField,StringType}

val schema = StructType(schemaString.split(“,”).map(fieldName => StructField(fieldName, StringType, true)))

val rowRDD =“,”)).map(p => Row(p(0), p(1).trim))
val empDF = sqlContext.createDataFrame(rowRDD, schema)
val results = sqlContext.sql(“SELECT name FROM emp”)

3)Reading from Hive

val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)
val hiveDF = hiveContext.sql(“select * from emp”)

4)Using read..load methods

Reading the JSON file and creating DataFrame:

val df =“/Users/sai/Desktop/Files/empjson.txt”)
df.printSchema()“name”).show()“name”), df(“sal”) + 1).show()
df.filter(df(“sal”) > 3000).show()

Reading CSV files with header and creating DataFrame:

./bin/spark-shell –packages com.databricks:spark-csv_2.11:1.1.0

val df =“com.databricks.spark.csv”).option(“header”, “true”).load(“/Users/sai/Desktop/Files/emp.txt”)

Note: From Spark 2.0 or later versions you no need to use databricks jar for reading csv files. API is already available for the same as below –

val df =“header”, “true”).csv(“/Users/sai/Desktop/Files/emp.txt”)

Creating DataFrame from RBMS Databases

(Required JAR to connnect to mysql : mysql-connector-java.jar)
val df_mysql =“jdbc”)
.option(“url”, “jdbc:mysql://localhost/db”)
.option(“driver”, “com.mysql.jdbc.Driver”)
.option(“dbtable”, “person”) //replace with own
.option(“user”, “root”) //replace with own
.option(“password”, “pwd”) // replace with own

(Required JAR to connnect to DB2 : db2jcc4.jar)
val df_db2 =“jdbc”)
.option(“url”, “jdbc:db2://localhost:50000/dbname”)
.option(“driver”, “”)
.option(“dbtable”, “person”) //replace with own
.option(“user”, “root”) //replace with own
.option(“password”, “pwd”) // replace with own

Share This Post

An Ambivert, music lover, enthusiast, artist, designer, coder, gamer, content writer. He is Professional Software Developer with hands-on experience in Spark, Kafka, Scala, Python, Hadoop, Hive, Sqoop, Pig, php, html,css. Know more about him at

Lost Password


24 Tutorials