SparkSQL is a Spark module for Structured data processing.
One use of SparkSQL is to execute SQL queries using a basic SQL syntax.
There are several ways to interact with Spark SQL
including SQL, the dataframes API,dataset API.
The backbone for all these operation is Dataframes and SchemaRDD.
A dataFrame is a distributed collection of data organised into named columns. It is conceptually
equivalent to a table in a relational database.
SchemaRDDs are made of row objects along with the metadata information.
Spark SQL needs SQLcontext object,which is created from existing SparkContext.
Steps for creating Dataframes,SchemaRDD and performing some operations using the sql methods provided by
start the spark shell by using the following command.
import the following packages:
SQLContext entry point for working with Structured data:
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
Below is used to implicitly convert an RDD to a
Import SparkSQL data types and Row:
load the data into a new RDD
val data = sc.textFile(“Youf file name”)
Define the schema according to your dataset using a case
For example your dataset contains product id,product name and product rating then your schema should be
case class Product(productid:Int, product_name: String, product_rating: Int);
If you want to check your schema you can use printSchem(), it prints the schema to the console in a tree format.
Create an RDD of product objects
val productdata = data.map(x=>x.split(“,”)).
change productData RDD of Product objects to a
A DataFrame is a distributed collection of data
organised into named columns. SparkSQL support
automatically converting an RDD containing case classes
to a DataFrame with the method toDF():
val product = productData.toDF()
Explore and query the product data with Spark DataFrames
DataFrames provide a domain-specific language for
structed data manipulation in Scala,Java,Python.
Check the basic practical of sparkSQL for examples.
Register the dataFrame as a temp table
Now execute the sql statement
val results = sqlContext.sql(“select * from Product”);