JDBC To Other Databases

Spark SQL also includes a data source that can read data from other databases using JDBC. This functionality should be preferred over using JdbcRDD. This is because the results are returned as a DataFrame and they can easily be processed in Spark SQL or joined with other data sources. The JDBC data source is also easier to use from Java or Python as it does not require the user to provide a ClassTag. (Note that this is different than the Spark SQL JDBC server, which allows other applications to run queries using Spark SQL).

To get started you will need to include the JDBC driver for your particular database on the spark classpath. For example, to connect to postgres from the Spark Shell you would run the following command:

  1. ./bin/spark-shell --driver-class-path postgresql-9.4.1207.jar --jars postgresql-9.4.1207.jar

Data Source Option

Spark supports the following case-insensitive options for JDBC. The Data source options of JDBC can be set via:

For connection properties, users can specify the JDBC connection properties in the data source options. user and password are normally provided as connection properties for logging into the data sources.

Property NameDefaultMeaningScope
url(none)The JDBC URL of the form jdbc:subprotocol:subname to connect to. The source-specific connection properties may be specified in the URL. e.g., jdbc:postgresql://localhost/test?user=fred&password=secretread/write
dbtable(none)The JDBC table that should be read from or written into. Note that when using it in the read path anything that is valid in a FROM clause of a SQL query can be used. For example, instead of a full table you could also use a subquery in parentheses. It is not allowed to specify dbtable and query options at the same time.read/write
query(none)A query that will be used to read data into Spark. The specified query will be parenthesized and used as a subquery in the FROM clause. Spark will also assign an alias to the subquery clause. As an example, spark will issue a query of the following form to the JDBC Source.

SELECT <columns> FROM (<user_specified_query>) spark_gen_alias

Below are a couple of restrictions while using this option.
  1. It is not allowed to specify dbtable and query options at the same time.
  2. It is not allowed to specify query and partitionColumn options at the same time. When specifying partitionColumn option is required, the subquery can be specified using dbtable option instead and partition columns can be qualified using the subquery alias provided as part of dbtable.
    Example:
    spark.read.format(“jdbc”)
    .option(“url”, jdbcUrl)
    .option(“query”, “select c1, c2 from t1”)
    .load()
read/write
prepareQuery(none)A prefix that will form the final query together with query. As the specified query will be parenthesized as a subquery in the FROM clause and some databases do not support all clauses in subqueries, the prepareQuery property offers a way to run such complex queries. As an example, spark will issue a query of the following form to the JDBC Source.

<prepareQuery> SELECT <columns> FROM (<user_specified_query>) spark_gen_alias

Below are a couple of examples.
  1. MSSQL Server does not accept WITH clauses in subqueries but it is possible to split such a query to prepareQuery and query:
    spark.read.format(“jdbc”)
    .option(“url”, jdbcUrl)
    .option(“prepareQuery”, “WITH t AS (SELECT x, y FROM tbl)”)
    .option(“query”, “SELECT FROM t WHERE x > 10”)
    .load()
  2. MSSQL Server does not accept temp table clauses in subqueries but it is possible to split such a query to prepareQuery and query:
    spark.read.format(“jdbc”)
    .option(“url”, jdbcUrl)
    .option(“prepareQuery”, “(SELECT
    INTO #TempTable FROM (SELECT FROM tbl) t)”)
    .option(“query”, “SELECT
    FROM #TempTable”)
    .load()
read/write
driver(none)The class name of the JDBC driver to use to connect to this URL.read/write
partitionColumn, lowerBound, upperBound(none)These options must all be specified if any of them is specified. In addition, numPartitions must be specified. They describe how to partition the table when reading in parallel from multiple workers. partitionColumn must be a numeric, date, or timestamp column from the table in question. Notice that lowerBound and upperBound are just used to decide the partition stride, not for filtering the rows in table. So all rows in the table will be partitioned and returned. This option applies only to reading.
Example:
spark.read.format(“jdbc”)
.option(“url”, jdbcUrl)
.option(“dbtable”, “(select c1, c2 from t1) as subq”)
.option(“partitionColumn”, “c1”)
.option(“lowerBound”, “1”)
.option(“upperBound”, “100”)
.option(“numPartitions”, “3”)
.load()
read
numPartitions(none)The maximum number of partitions that can be used for parallelism in table reading and writing. This also determines the maximum number of concurrent JDBC connections. If the number of partitions to write exceeds this limit, we decrease it to this limit by calling coalesce(numPartitions) before writing.read/write
queryTimeout0The number of seconds the driver will wait for a Statement object to execute to the given number of seconds. Zero means there is no limit. In the write path, this option depends on how JDBC drivers implement the API setQueryTimeout, e.g., the h2 JDBC driver checks the timeout of each query instead of an entire JDBC batch.read/write
fetchsize0The JDBC fetch size, which determines how many rows to fetch per round trip. This can help performance on JDBC drivers which default to low fetch size (e.g. Oracle with 10 rows).read
batchsize1000The JDBC batch size, which determines how many rows to insert per round trip. This can help performance on JDBC drivers. This option applies only to writing.write
isolationLevelREAD_UNCOMMITTEDThe transaction isolation level, which applies to current connection. It can be one of NONE, READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, or SERIALIZABLE, corresponding to standard transaction isolation levels defined by JDBC’s Connection object, with default of READ_UNCOMMITTED. Please refer the documentation in java.sql.Connection.write
sessionInitStatement(none)After each database session is opened to the remote DB and before starting to read data, this option executes a custom SQL statement (or a PL/SQL block). Use this to implement session initialization code. Example: option(“sessionInitStatement”, “””BEGIN execute immediate ‘alter session set “_serial_direct_read”=true’; END;”””)read
truncatefalseThis is a JDBC writer related option. When SaveMode.Overwrite is enabled, this option causes Spark to truncate an existing table instead of dropping and recreating it. This can be more efficient, and prevents the table metadata (e.g., indices) from being removed. However, it will not work in some cases, such as when the new data has a different schema. In case of failures, users should turn off truncate option to use DROP TABLE again. Also, due to the different behavior of TRUNCATE TABLE among DBMS, it’s not always safe to use this. MySQLDialect, DB2Dialect, MsSqlServerDialect, DerbyDialect, and OracleDialect supports this while PostgresDialect and default JDBCDirect doesn’t. For unknown and unsupported JDBCDirect, the user option truncate is ignored.write
cascadeTruncatethe default cascading truncate behaviour of the JDBC database in question, specified in the isCascadeTruncate in each JDBCDialectThis is a JDBC writer related option. If enabled and supported by the JDBC database (PostgreSQL and Oracle at the moment), this options allows execution of a TRUNCATE TABLE t CASCADE (in the case of PostgreSQL a TRUNCATE TABLE ONLY t CASCADE is executed to prevent inadvertently truncating descendant tables). This will affect other tables, and thus should be used with care.write
createTableOptionsThis is a JDBC writer related option. If specified, this option allows setting of database-specific table and partition options when creating a table (e.g., CREATE TABLE t (name string) ENGINE=InnoDB.).write
createTableColumnTypes(none)The database column data types to use instead of the defaults, when creating the table. Data type information should be specified in the same format as CREATE TABLE columns syntax (e.g: “name CHAR(64), comments VARCHAR(1024)”). The specified types should be valid spark sql data types.write
customSchema(none)The custom schema to use for reading data from JDBC connectors. For example, “id DECIMAL(38, 0), name STRING”. You can also specify partial fields, and the others use the default type mapping. For example, “id DECIMAL(38, 0)”. The column names should be identical to the corresponding column names of JDBC table. Users can specify the corresponding data types of Spark SQL instead of using the defaults.read
pushDownPredicatetrueThe option to enable or disable predicate push-down into the JDBC data source. The default value is true, in which case Spark will push down filters to the JDBC data source as much as possible. Otherwise, if set to false, no filter will be pushed down to the JDBC data source and thus all filters will be handled by Spark. Predicate push-down is usually turned off when the predicate filtering is performed faster by Spark than by the JDBC data source.read
pushDownAggregatefalseThe option to enable or disable aggregate push-down in V2 JDBC data source. The default value is false, in which case Spark will not push down aggregates to the JDBC data source. Otherwise, if sets to true, aggregates will be pushed down to the JDBC data source. Aggregate push-down is usually turned off when the aggregate is performed faster by Spark than by the JDBC data source. Please note that aggregates can be pushed down if and only if all the aggregate functions and the related filters can be pushed down. If numPartitions equals to 1 or the group by key is the same as partitionColumn, Spark will push down aggregate to data source completely and not apply a final aggregate over the data source output. Otherwise, Spark will apply a final aggregate over the data source output.read
pushDownLimitfalseThe option to enable or disable LIMIT push-down into V2 JDBC data source. The LIMIT push-down also includes LIMIT + SORT , a.k.a. the Top N operator. The default value is false, in which case Spark does not push down LIMIT or LIMIT with SORT to the JDBC data source. Otherwise, if sets to true, LIMIT or LIMIT with SORT is pushed down to the JDBC data source. If numPartitions is greater than 1, Spark still applies LIMIT or LIMIT with SORT on the result from data source even if LIMIT or LIMIT with SORT is pushed down. Otherwise, if LIMIT or LIMIT with SORT is pushed down and numPartitions equals to 1, Spark will not apply LIMIT or LIMIT with SORT on the result from data source.read
pushDownOffsetfalseThe option to enable or disable OFFSET push-down into V2 JDBC data source. The default value is false, in which case Spark will not push down OFFSET to the JDBC data source. Otherwise, if sets to true, Spark will try to push down OFFSET to the JDBC data source. If pushDownOffset is true and numPartitions is equal to 1, OFFSET will be pushed down to the JDBC data source. Otherwise, OFFSET will not be pushed down and Spark still applies OFFSET on the result from data source.read
pushDownTableSamplefalseThe option to enable or disable TABLESAMPLE push-down into V2 JDBC data source. The default value is false, in which case Spark does not push down TABLESAMPLE to the JDBC data source. Otherwise, if value sets to true, TABLESAMPLE is pushed down to the JDBC data source.read
keytab(none)Location of the kerberos keytab file (which must be pre-uploaded to all nodes either by —files option of spark-submit or manually) for the JDBC client. When path information found then Spark considers the keytab distributed manually, otherwise —files assumed. If both keytab and principal are defined then Spark tries to do kerberos authentication.read/write
principal(none)Specifies kerberos principal name for the JDBC client. If both keytab and principal are defined then Spark tries to do kerberos authentication.read/write
refreshKrb5ConfigfalseThis option controls whether the kerberos configuration is to be refreshed or not for the JDBC client before establishing a new connection. Set to true if you want to refresh the configuration, otherwise set to false. The default value is false. Note that if you set this option to true and try to establish multiple connections, a race condition can occur. One possble situation would be like as follows.
  1. refreshKrb5Config flag is set with security context 1
  2. A JDBC connection provider is used for the corresponding DBMS
  3. The krb5.conf is modified but the JVM not yet realized that it must be reloaded
  4. Spark authenticates successfully for security context 1
  5. The JVM loads security context 2 from the modified krb5.conf
  6. Spark restores the previously saved security context 1
  7. The modified krb5.conf content just gone
read/write
connectionProvider(none)The name of the JDBC connection provider to use to connect to this URL, e.g. db2, mssql. Must be one of the providers loaded with the JDBC data source. Used to disambiguate when more than one provider can handle the specified driver and options. The selected provider must not be disabled by spark.sql.sources.disabledJdbcConnProviderList.read/write
preferTimestampNTZfalseWhen the option is set to true, all timestamps are inferred as TIMESTAMP WITHOUT TIME ZONE. Otherwise, timestamps are read as TIMESTAMP with local time zone.read

Note that kerberos authentication with keytab is not always supported by the JDBC driver.
Before using keytab and principal configuration options, please make sure the following requirements are met:

  • The included JDBC driver version supports kerberos authentication with keytab.
  • There is a built-in connection provider which supports the used database.

There is a built-in connection providers for the following databases:

  • DB2
  • MariaDB
  • MS Sql
  • Oracle
  • PostgreSQL

If the requirements are not met, please consider using the JdbcConnectionProvider developer API to handle custom authentication.

  1. // Note: JDBC loading and saving can be achieved via either the load/save or jdbc methods
  2. // Loading data from a JDBC source
  3. val jdbcDF = spark.read
  4. .format("jdbc")
  5. .option("url", "jdbc:postgresql:dbserver")
  6. .option("dbtable", "schema.tablename")
  7. .option("user", "username")
  8. .option("password", "password")
  9. .load()
  10. val connectionProperties = new Properties()
  11. connectionProperties.put("user", "username")
  12. connectionProperties.put("password", "password")
  13. val jdbcDF2 = spark.read
  14. .jdbc("jdbc:postgresql:dbserver", "schema.tablename", connectionProperties)
  15. // Specifying the custom data types of the read schema
  16. connectionProperties.put("customSchema", "id DECIMAL(38, 0), name STRING")
  17. val jdbcDF3 = spark.read
  18. .jdbc("jdbc:postgresql:dbserver", "schema.tablename", connectionProperties)
  19. // Saving data to a JDBC source
  20. jdbcDF.write
  21. .format("jdbc")
  22. .option("url", "jdbc:postgresql:dbserver")
  23. .option("dbtable", "schema.tablename")
  24. .option("user", "username")
  25. .option("password", "password")
  26. .save()
  27. jdbcDF2.write
  28. .jdbc("jdbc:postgresql:dbserver", "schema.tablename", connectionProperties)
  29. // Specifying create table column data types on write
  30. jdbcDF.write
  31. .option("createTableColumnTypes", "name CHAR(64), comments VARCHAR(1024)")
  32. .jdbc("jdbc:postgresql:dbserver", "schema.tablename", connectionProperties)

Find full example code at “examples/src/main/scala/org/apache/spark/examples/sql/SQLDataSourceExample.scala” in the Spark repo.

  1. // Note: JDBC loading and saving can be achieved via either the load/save or jdbc methods
  2. // Loading data from a JDBC source
  3. Dataset<Row> jdbcDF = spark.read()
  4. .format("jdbc")
  5. .option("url", "jdbc:postgresql:dbserver")
  6. .option("dbtable", "schema.tablename")
  7. .option("user", "username")
  8. .option("password", "password")
  9. .load();
  10. Properties connectionProperties = new Properties();
  11. connectionProperties.put("user", "username");
  12. connectionProperties.put("password", "password");
  13. Dataset<Row> jdbcDF2 = spark.read()
  14. .jdbc("jdbc:postgresql:dbserver", "schema.tablename", connectionProperties);
  15. // Saving data to a JDBC source
  16. jdbcDF.write()
  17. .format("jdbc")
  18. .option("url", "jdbc:postgresql:dbserver")
  19. .option("dbtable", "schema.tablename")
  20. .option("user", "username")
  21. .option("password", "password")
  22. .save();
  23. jdbcDF2.write()
  24. .jdbc("jdbc:postgresql:dbserver", "schema.tablename", connectionProperties);
  25. // Specifying create table column data types on write
  26. jdbcDF.write()
  27. .option("createTableColumnTypes", "name CHAR(64), comments VARCHAR(1024)")
  28. .jdbc("jdbc:postgresql:dbserver", "schema.tablename", connectionProperties);

Find full example code at “examples/src/main/java/org/apache/spark/examples/sql/JavaSQLDataSourceExample.java” in the Spark repo.

  1. # Note: JDBC loading and saving can be achieved via either the load/save or jdbc methods
  2. # Loading data from a JDBC source
  3. jdbcDF = spark.read \
  4. .format("jdbc") \
  5. .option("url", "jdbc:postgresql:dbserver") \
  6. .option("dbtable", "schema.tablename") \
  7. .option("user", "username") \
  8. .option("password", "password") \
  9. .load()
  10. jdbcDF2 = spark.read \
  11. .jdbc("jdbc:postgresql:dbserver", "schema.tablename",
  12. properties={"user": "username", "password": "password"})
  13. # Specifying dataframe column data types on read
  14. jdbcDF3 = spark.read \
  15. .format("jdbc") \
  16. .option("url", "jdbc:postgresql:dbserver") \
  17. .option("dbtable", "schema.tablename") \
  18. .option("user", "username") \
  19. .option("password", "password") \
  20. .option("customSchema", "id DECIMAL(38, 0), name STRING") \
  21. .load()
  22. # Saving data to a JDBC source
  23. jdbcDF.write \
  24. .format("jdbc") \
  25. .option("url", "jdbc:postgresql:dbserver") \
  26. .option("dbtable", "schema.tablename") \
  27. .option("user", "username") \
  28. .option("password", "password") \
  29. .save()
  30. jdbcDF2.write \
  31. .jdbc("jdbc:postgresql:dbserver", "schema.tablename",
  32. properties={"user": "username", "password": "password"})
  33. # Specifying create table column data types on write
  34. jdbcDF.write \
  35. .option("createTableColumnTypes", "name CHAR(64), comments VARCHAR(1024)") \
  36. .jdbc("jdbc:postgresql:dbserver", "schema.tablename",
  37. properties={"user": "username", "password": "password"})

Find full example code at “examples/src/main/python/sql/datasource.py” in the Spark repo.

  1. # Loading data from a JDBC source
  2. df <- read.jdbc("jdbc:postgresql:dbserver", "schema.tablename", user = "username", password = "password")
  3. # Saving data to a JDBC source
  4. write.jdbc(df, "jdbc:postgresql:dbserver", "schema.tablename", user = "username", password = "password")

Find full example code at “examples/src/main/r/RSparkSQLExample.R” in the Spark repo.

  1. CREATE TEMPORARY VIEW jdbcTable
  2. USING org.apache.spark.sql.jdbc
  3. OPTIONS (
  4. url "jdbc:postgresql:dbserver",
  5. dbtable "schema.tablename",
  6. user 'username',
  7. password 'password'
  8. )
  9. INSERT INTO TABLE jdbcTable
  10. SELECT * FROM resultTable