DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. What Is Spark SQL?

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.

Todd McGrath user avatar by
Todd McGrath
·
Apr. 17, 19 · Opinion
Like (6)
Save
Tweet
Share
36.58K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Linking

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")
customers.registerTempTable("customers")
val firstCityState = sqlContext.sql("SELECT first_name, address.city, address.state FROM customers LIMIT 10")

DataFrames

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

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.

Conclusion

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:

Apache Spark

sql Database MySQL Relational database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is a Kubernetes CI/CD Pipeline?
  • Deploying Java Serverless Functions as AWS Lambda
  • Bye Bye, Regular Dev [Comic]
  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: