Loading database data using Spark 2.0 Data Sources API

Sujee
Spark Experts
Published in
2 min readJul 30, 2017

Previously I blogged about loading database data using Spark 1.3. With multiple releases afterwards, spark and its data source API evolved a lot. With the new 2.0 major release, I thought about revisiting it again.

In this post, I’m using Spark 2.0.0 to rewrite the example of reading employees table from MySQL database to generate full name from first name and last name columns.

The new DataFrameReader class (introduced in version 1.4) defines methods used for loading external storages such as files, databases, …etc. DataFrameReader can be accessed by calling read() method in SparkSession (a new entry point introduced in 2.0 that subsumes SQLContext and HiveContext). Once we have a reference to DataFrameReader, database can be accessed using one of following approaches.

  • Using format() and options() methods
sparkSession.read().format("jdbc").options(options).load();

This is the example shown in official Spark SQL documentation. Here format() method is used to indicate the type of datasource. Options() method is used to pass JDBC connection properties as a Map. Alternatively option() method can be used which allows to define single property at a time. By chaining multiple option() methods, multiple properties can be passed. Option() method also has multiple variations to be used with different data types such as Double, String, …etc.
The list of supported properties can be viewed here. http://spark.apache.org/docs/latest/sql-programming-guide.html#jdbc-to-other-databases

  • Using convenient jdbc() method

In this blog post, I’m focusing on this approach as it is more convenient and easy to read.

Steps

  • MySQL connection parameters are defined as constants
private static final String MYSQL_CONNECTION_URL = "jdbc:mysql://localhost:3306/employees";
private static final String MYSQL_USERNAME = "expertuser";
private static final String MYSQL_PWD = "expertuser123";
  • Initializing SparkSession
private static final SparkSession sparkSession =
SparkSession.builder().master("local[*]").appName("Spark2JdbcDs").getOrCreate();
  • JDBC connection properties
final Properties connectionProperties = new Properties();
connectionProperties.put("user", MYSQL_USERNAME);
connectionProperties.put("password", MYSQL_PWD);

“user” and “password” are mandatory connection properties need to be added here. All other mandatory properties are defined as method parameters of jdbc() method.
If you’ve used old versions of spark before, another thing you’ll notice is user and password are no longer needed to be defined as part of connection URL.

  • Loading MySQL query result as DataFrame
final String dbTable =
"(select emp_no, concat_ws(' ', first_name, last_name) as full_name from employees) as employees_name";
Dataset<Row> jdbcDF =
sparkSession.read().jdbc(MYSQL_CONNECTION_URL, dbTable, "emp_no", 10001, 499999, 10, connectionProperties);

Here we invoke jdbc() method with connection URL, db query, partitionColumn, lowerBound, upperBound, numPartitions and connection properties.

Note: There is another variation of jdbc() method with parameters — connection URL, db query and connection properties. In that case, partitionColumn, lowerBound, upperBound, numPartitions have to be defined as connection properties along with user and password.

  • Executing DataFrame by invoking an action
List<Row> employeeFullNameRows = jdbcDF.collectAsList();for (Row employeeFullNameRow : employeeFullNameRows) {
LOGGER.info(employeeFullNameRow);
}

Complete Java application is available at my GitHub repo https://github.com/sujee81/SparkApps.

--

--