Skip to content

Commit a2c8e0d

Browse files
committed
[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 apache#130 from dongjoon-hyun/SPARK-52073. Authored-by: Dongjoon Hyun <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent 93060c1 commit a2c8e0d

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

Examples/pi/Package.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// swift-tools-version: 6.0
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
//
20+
21+
import PackageDescription
22+
23+
let package = Package(
24+
name: "SparkConnectSwiftPi",
25+
platforms: [
26+
.macOS(.v15)
27+
],
28+
dependencies: [
29+
.package(url: "https://github.com/apache/spark-connect-swift.git", branch: "main")
30+
],
31+
targets: [
32+
.executableTarget(
33+
name: "SparkConnectSwiftPi",
34+
dependencies: [.product(name: "SparkConnect", package: "spark-connect-swift")]
35+
)
36+
]
37+
)

Examples/pi/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# A Swift Application computing an approximation to pi with Apache Spark Connect Swift Client
2+
3+
This is an example Swift application to show how to use Apache Spark Connect Swift Client library.
4+
5+
## How to run
6+
7+
Run this Swift application.
8+
9+
```
10+
$ swift run SparkConnectSwiftPi 1000000
11+
...
12+
Connected to Apache Spark 4.0.0 Server
13+
Pi is roughly 3.1423711423711422
14+
```

Examples/pi/Sources/main.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
import SparkConnect
21+
22+
let spark = try await SparkSession.builder.getOrCreate()
23+
24+
let n: Int64 = CommandLine.arguments.count > 1 ? Int64(CommandLine.arguments[1])! : 1_000_000
25+
26+
let count =
27+
try await spark
28+
.range(n)
29+
.selectExpr("(pow(rand() * 2 - 1, 2) + pow(rand() * 2 - 1, 2)) as v")
30+
.where("v <= 1")
31+
.count()
32+
33+
print("Pi is roughly \(4.0 * Double(count) / (Double(n) - 1))")
34+
35+
await spark.stop()

0 commit comments

Comments
 (0)