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. Find out more about Spark SQL here.
Join the DZone community and get the full member experience.Join For Free
Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper
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:
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.
It can read and write data in a variety of structured formats (e.g., JSON, Hive Tables, and Parquet).
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") customers.registerTempTable("customers") 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.
Spark SQL provides JDBC connectivity, which can be useful when connecting to business intelligence (BI) tools such as Tableau.
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.
Opinions expressed by DZone contributors are their own.