You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by do...@apache.org on 2025/05/13 04:46:04 UTC
(spark-connect-swift) branch main updated: [SPARK-52098] Support JDBC in DataFrameReader and Writer
This is an automated email from the ASF dual-hosted git repository.
dongjoon pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/spark-connect-swift.git
The following commit(s) were added to refs/heads/main by this push:
new 0183dfb [SPARK-52098] Support JDBC in DataFrameReader and Writer
0183dfb is described below
commit 0183dfb4c0cfaf66469b21d35863e5a835feb636
Author: William Hyun <wi...@apache.org>
AuthorDate: Mon May 12 21:45:57 2025 -0700
[SPARK-52098] Support JDBC in DataFrameReader and Writer
### What changes were proposed in this pull request?
This PR aims to Support JDBC in `DataFrameReader` and `DataFrameWriter`.
### Why are the changes needed?
To support various databases via JDBC protocol.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
Manually.
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #141 from williamhyun/jdbc.
Authored-by: William Hyun <wi...@apache.org>
Signed-off-by: Dongjoon Hyun <do...@apache.org>
---
Sources/SparkConnect/DataFrameReader.swift | 17 +++++++++++++++++
Sources/SparkConnect/DataFrameWriter.swift | 17 +++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/Sources/SparkConnect/DataFrameReader.swift b/Sources/SparkConnect/DataFrameReader.swift
index 7230ee1..58567db 100644
--- a/Sources/SparkConnect/DataFrameReader.swift
+++ b/Sources/SparkConnect/DataFrameReader.swift
@@ -258,4 +258,21 @@ public actor DataFrameReader: Sendable {
self.source = "parquet"
return load(paths)
}
+
+ /// Construct a `DataFrame` representing the database table accessible via JDBC URL url named
+ /// table and connection properties.
+ /// - Parameters:
+ /// - url: The JDBC URL of the form `jdbc:subprotocol:subname` to connect to.
+ /// - table: The JDBC table that should be read from or written into.
+ /// - properties: A string-string dictionary for connection properties.
+ /// - Returns: A `DataFrame`.
+ public func jdbc(_ url: String, _ table: String, _ properties: [String: String] = [:]) -> DataFrame {
+ for (key, value) in properties {
+ self.extraOptions[key] = value
+ }
+ self.extraOptions["url"] = url
+ self.extraOptions["dbtable"] = table
+ self.source = "jdbc"
+ return load()
+ }
}
diff --git a/Sources/SparkConnect/DataFrameWriter.swift b/Sources/SparkConnect/DataFrameWriter.swift
index c9d8425..11a5fa8 100644
--- a/Sources/SparkConnect/DataFrameWriter.swift
+++ b/Sources/SparkConnect/DataFrameWriter.swift
@@ -228,4 +228,21 @@ public actor DataFrameWriter: Sendable {
self.source = "text"
return try await save(path)
}
+
+ /// Saves the content of the `DataFrame` to an external database table via JDBC. In the case the
+ /// table already exists in the external database, behavior of this function depends on the save
+ /// mode, specified by the `mode` function (default to throwing an exception).
+ /// - Parameters:
+ /// - url: The JDBC URL of the form `jdbc:subprotocol:subname` to connect to.
+ /// - table: Name of the table in the external database.
+ /// - properties:JDBC database connection arguments, a list of arbitrary string tag/value.
+ public func jdbc(_ url: String, _ table: String, _ properties: [String: String] = [:]) async throws {
+ for (key, value) in properties {
+ self.extraOptions[key] = value
+ }
+ self.extraOptions["url"] = url
+ self.extraOptions["dbtable"] = table
+ self.source = "jdbc"
+ return try await save()
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org