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()