File tree Expand file tree Collapse file tree 3 files changed +86
-0
lines changed
Expand file tree Collapse file tree 3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 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+ )
Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change 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 ( )
You can’t perform that action at this time.
0 commit comments