Over a million developers have joined DZone.

What Is Spark SQL?

Spark SQL allows you to use data frames in Python, Java, and Scala; read and write data in a variety of structured formats; and query Big Data with SQL. Find out more about Spark SQL here.

· Database Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

Spark SQL is Spark’s interface for working with structured and semistructured data. Structured data is considered any data that has a schema such as JSON, Hive Tables, Parquet.  Schema means having a known set of fields for each record.  Semistructured data is when there is no separation between the schema and the data

Spark SQL provides three main capabilities for using structured and semistructured data:

  1. It provides a DataFrame abstraction in Python, Java, and Scala to simplify working with structured datasets. DataFrames are similar to tables in a relational database.

  2. It can read and write data in a variety of structured formats (e.g., JSON, Hive Tables, and Parquet).

  3. It lets you query the data using SQL, both inside a Spark program and from external tools that connect to Spark SQL through standard database connectors (JDBC/ ODBC), such as business intelligence tools like Tableau.


Including Spark SQL in our application requires some additional library dependencies.  Spark SQL may be built with or without support for Apache Hive.  When you download Spark in binary form, it should already be built with Hive support.

Using Spark SQL in Applications

Spark SQL is best approached by using inside a Spark application. This presents the ability to combine easily loading and querying data while simultaneously combining it Python, Java, or Scala.

Basic Query Example

To query a table, we call the sql() method on either the HiveContext or SQLContext.

Scala code example: Load customer data from JSON 

val customers = sqlContext.jsonFile("customers.json")
val firstCityState = sqlContext.sql("SELECT first_name, address.city, address.state FROM customers LIMIT 10")


DataFrames are similar to tables in a relational database.  A DataFrame is an RDD of Row objects. A DataFrame also knows the schema of each of its rows. DataFrames store data in a more efficient manner than native RDDs by taking advantage of knowing their schema.


Caching in Spark SQL is more efficient because the DataFrame knows the types of each column.

Loading and Saving Data

Spark SQL supports a number of structured data sources natively. These sources include Hive tables, JSON, and Parquet files.

In addition, Spark SQL also has a DataSource API which allows integration. Notable implementations of the DataSource API include Avro, Apache HBase, Elasticsearch, Cassandra, and more. Many of these and more can be found on the community index of packages at http://spark-packages.org.

JDBC Connectivity

Spark SQL provides JDBC connectivity, which can be useful when connecting to business intelligence (BI) tools such as Tableau.

For a tutorial on Spark SQL JDBC in Scala, see Spark SQL, Scala, JDBC, and mySQL example or for Python see Spark SQL, Python, and mySQL.

User-Defined Functions (UDFs)

Spark SQL supports registration of user-defined functions in Python, Java, and Scala to call from within SQL. They are a very popular way to expose advanced functionality to SQL, so users can use them without writing code.

Spark SQL Performance

Spark SQL’s additional type information allows Spark SQL to be more efficient and provides more than just SQL with relational databases. It also makes it easy to perform conditional aggregate operations such as counting the sum of multiple columns.

Performance Tuning Options

There are a number of different performance tuning options with Spark SQL such as codegen, memory settings, batch sizes and compression codes.


In many data processing pipelines, it is convenient and powerful to combine Spark SQL with Python, Scala or Java code. In addition, when using Spark SQL optimizations may be gained from the engine’s ability to leverage schemas.

Related Refcard:

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

spark,spark sql,scala,json,csv,python,apache spark,hive,parquet,sql

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}