From e80c75430157d347be11d08964ea7bbec4b6513f Mon Sep 17 00:00:00 2001 From: Dongjoon Hyun Date: Wed, 12 Mar 2025 08:36:06 -0700 Subject: [PATCH] [SPARK-51485] Add `How to use in your apps` section to `README.md` --- README.md | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/README.md b/README.md index f7cfb41..18bd5e2 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,82 @@ So far, this library project is tracking the upstream changes like the [Apache S - [gRPC Swift Protobuf 1.0 (March 2025)](https://github.com/grpc/grpc-swift-protobuf/releases/tag/1.1.0) - [gRPC Swift NIO Transport 1.0 (March 2025)](https://github.com/grpc/grpc-swift-nio-transport/releases/tag/1.0.1) - [Apache Arrow Swift](https://github.com/apache/arrow/tree/main/swift) + +## How to use in your apps + +Create a Swift project. +``` +$ mkdir SparkConnectSwiftApp +$ cd SparkConnectSwiftApp +$ swift package init --name SparkConnectSwiftApp --type executable +``` + +Add `SparkConnect` package to the dependency like the following +``` +$ cat Package.swift +import PackageDescription + +let package = Package( + name: "SparkConnectSwiftApp", + platforms: [ + .macOS(.v15) + ], + dependencies: [ + .package(url: "https://github.com/apache/spark-connect-swift.git", branch: "main") + ], + targets: [ + .executableTarget( + name: "SparkConnectSwiftApp", + dependencies: [.product(name: "SparkConnect", package: "spark-connect-swift")] + ) + ] +) +``` + +Use `SparkSession` of `SparkConnect` module in Swift. + +``` +$ cat Sources/main.swift + +import SparkConnect + +let spark = try await SparkSession.builder.getOrCreate() +print("Connected to Apache Spark \(await spark.version) Server") + +let statements = [ + "DROP TABLE IF EXISTS t", + "CREATE TABLE IF NOT EXISTS t(a INT)", + "INSERT INTO t VALUES (1), (2), (3)", +] + +for s in statements { + print("EXECUTE: \(s)") + _ = try await spark.sql(s).count() +} +print("SELECT * FROM t") +try await spark.sql("SELECT * FROM t").show() + +await spark.stop() +``` + +Run your Swift application. + +``` +$ swift run +... +Connected to Apache Spark 4.0.0 Server +EXECUTE: DROP TABLE IF EXISTS t +EXECUTE: CREATE TABLE IF NOT EXISTS t(a INT) +EXECUTE: INSERT INTO t VALUES (1), (2), (3) +SELECT * FROM t ++---+ +| a | ++---+ +| 2 | +| 1 | +| 3 | ++---+ +``` + +You can find this example in the following repository. +- https://github.com/dongjoon-hyun/spark-connect-swift-app