Got it

What Is Spark SQL?

Latest reply: Apr 9, 2022 10:37:41 441 1 1 0 0

Original link:


hello, everyone!

This post is a about “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.

Spark SQL is Spark’s interface for working with structured and semi-structured 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.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

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.

  • x
  • convention:

Created Apr 9, 2022 10:37:41

Good share
View more
  • x
  • convention:


You need to log in to comment to the post Login | Register

Notice: To protect the legitimate rights and interests of you, the community, and third parties, do not release content that may bring legal risks to all parties, including but are not limited to the following:
  • Politically sensitive content
  • Content concerning pornography, gambling, and drug abuse
  • Content that may disclose or infringe upon others ' commercial secrets, intellectual properties, including trade marks, copyrights, and patents, and personal privacy
Do not share your account and password with others. All operations performed using your account will be regarded as your own actions and all consequences arising therefrom will be borne by you. For details, see " User Agreement."

My Followers

Login and enjoy all the member benefits


Are you sure to block this user?
Users on your blacklist cannot comment on your post,cannot mention you, cannot send you private messages.
Please bind your phone number to obtain invitation bonus.
Information Protection Guide
Thanks for using Huawei Enterprise Support Community! We will help you learn how we collect, use, store and share your personal information and the rights you have in accordance with Privacy Policy and User Agreement.