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 a2c8e0d [SPARK-52073] Add `pi` example
a2c8e0d is described below
commit a2c8e0d236b71cd899a55c0c4b0c926ee8e61286
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Sun May 11 17:04:54 2025 -0700
[SPARK-52073] Add `pi` example
### What changes were proposed in this pull request?
This PR aims to add `pi` example.
### Why are the changes needed?
This is a Swift Pi example corresponding to `SparkDataFramePi.scala`.
-
https://github.com/apache/spark/blob/master/examples/src/main/scala/org/apache/spark/examples/sql/SparkDataFramePi.scala
```
$ swift run SparkConnectSwiftPi 1000000
...
Connected to Apache Spark 4.0.0 Server
Pi is roughly 3.1423711423711422
```
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
Manual review.
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #130 from dongjoon-hyun/SPARK-52073.
Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
---
Examples/pi/Package.swift | 37 +++++++++++++++++++++++++++++++++++++
Examples/pi/README.md | 14 ++++++++++++++
Examples/pi/Sources/main.swift | 35 +++++++++++++++++++++++++++++++++++
3 files changed, 86 insertions(+)
diff --git a/Examples/pi/Package.swift b/Examples/pi/Package.swift
new file mode 100644
index 0000000..7b2400e
--- /dev/null
+++ b/Examples/pi/Package.swift
@@ -0,0 +1,37 @@
+// swift-tools-version: 6.0
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+import PackageDescription
+
+let package = Package(
+ name: "SparkConnectSwiftPi",
+ platforms: [
+ .macOS(.v15)
+ ],
+ dependencies: [
+ .package(url: "https://github.com/apache/spark-connect-swift.git", branch:
"main")
+ ],
+ targets: [
+ .executableTarget(
+ name: "SparkConnectSwiftPi",
+ dependencies: [.product(name: "SparkConnect", package:
"spark-connect-swift")]
+ )
+ ]
+)
diff --git a/Examples/pi/README.md b/Examples/pi/README.md
new file mode 100644
index 0000000..3ae9d6b
--- /dev/null
+++ b/Examples/pi/README.md
@@ -0,0 +1,14 @@
+# A Swift Application computing an approximation to pi with Apache Spark
Connect Swift Client
+
+This is an example Swift application to show how to use Apache Spark Connect
Swift Client library.
+
+## How to run
+
+Run this Swift application.
+
+```
+$ swift run SparkConnectSwiftPi 1000000
+...
+Connected to Apache Spark 4.0.0 Server
+Pi is roughly 3.1423711423711422
+```
diff --git a/Examples/pi/Sources/main.swift b/Examples/pi/Sources/main.swift
new file mode 100644
index 0000000..f894a54
--- /dev/null
+++ b/Examples/pi/Sources/main.swift
@@ -0,0 +1,35 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+import SparkConnect
+
+let spark = try await SparkSession.builder.getOrCreate()
+
+let n: Int64 = CommandLine.arguments.count > 1 ?
Int64(CommandLine.arguments[1])! : 1_000_000
+
+let count =
+ try await spark
+ .range(n)
+ .selectExpr("(pow(rand() * 2 - 1, 2) + pow(rand() * 2 - 1, 2)) as v")
+ .where("v <= 1")
+ .count()
+
+print("Pi is roughly \(4.0 * Double(count) / (Double(n) - 1))")
+
+await spark.stop()
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]