aboutsummaryrefslogtreecommitdiff
path: root/examples/src/main/scala
diff options
context:
space:
mode:
authorJustin Pihony <justin.pihony@gmail.com>2016-09-26 09:54:22 +0100
committerSean Owen <sowen@cloudera.com>2016-09-26 09:54:22 +0100
commit50b89d05b7bffc212cc9b9ae6e0bca7cb90b9c77 (patch)
tree004018c95e9fedc204d683c210af79ac43bd4212 /examples/src/main/scala
parentac65139be96dbf87402b9a85729a93afd3c6ff17 (diff)
downloadspark-50b89d05b7bffc212cc9b9ae6e0bca7cb90b9c77.tar.gz
spark-50b89d05b7bffc212cc9b9ae6e0bca7cb90b9c77.tar.bz2
spark-50b89d05b7bffc212cc9b9ae6e0bca7cb90b9c77.zip
[SPARK-14525][SQL] Make DataFrameWrite.save work for jdbc
## What changes were proposed in this pull request? This change modifies the implementation of DataFrameWriter.save such that it works with jdbc, and the call to jdbc merely delegates to save. ## How was this patch tested? This was tested via unit tests in the JDBCWriteSuite, of which I added one new test to cover this scenario. ## Additional details rxin This seems to have been most recently touched by you and was also commented on in the JIRA. This contribution is my original work and I license the work to the project under the project's open source license. Author: Justin Pihony <justin.pihony@gmail.com> Author: Justin Pihony <justin.pihony@typesafe.com> Closes #12601 from JustinPihony/jdbc_reconciliation.
Diffstat (limited to 'examples/src/main/scala')
-rw-r--r--examples/src/main/scala/org/apache/spark/examples/sql/SQLDataSourceExample.scala22
1 files changed, 22 insertions, 0 deletions
diff --git a/examples/src/main/scala/org/apache/spark/examples/sql/SQLDataSourceExample.scala b/examples/src/main/scala/org/apache/spark/examples/sql/SQLDataSourceExample.scala
index dc3915a488..66f7cb1b53 100644
--- a/examples/src/main/scala/org/apache/spark/examples/sql/SQLDataSourceExample.scala
+++ b/examples/src/main/scala/org/apache/spark/examples/sql/SQLDataSourceExample.scala
@@ -16,6 +16,8 @@
*/
package org.apache.spark.examples.sql
+import java.util.Properties
+
import org.apache.spark.sql.SparkSession
object SQLDataSourceExample {
@@ -148,6 +150,8 @@ object SQLDataSourceExample {
private def runJdbcDatasetExample(spark: SparkSession): Unit = {
// $example on:jdbc_dataset$
+ // Note: JDBC loading and saving can be achieved via either the load/save or jdbc methods
+ // Loading data from a JDBC source
val jdbcDF = spark.read
.format("jdbc")
.option("url", "jdbc:postgresql:dbserver")
@@ -155,6 +159,24 @@ object SQLDataSourceExample {
.option("user", "username")
.option("password", "password")
.load()
+
+ val connectionProperties = new Properties()
+ connectionProperties.put("user", "username")
+ connectionProperties.put("password", "password")
+ val jdbcDF2 = spark.read
+ .jdbc("jdbc:postgresql:dbserver", "schema.tablename", connectionProperties)
+
+ // Saving data to a JDBC source
+ jdbcDF.write
+ .format("jdbc")
+ .option("url", "jdbc:postgresql:dbserver")
+ .option("dbtable", "schema.tablename")
+ .option("user", "username")
+ .option("password", "password")
+ .save()
+
+ jdbcDF2.write
+ .jdbc("jdbc:postgresql:dbserver", "schema.tablename", connectionProperties)
// $example off:jdbc_dataset$
}
}