|
| 1 | +# A Swift Application with Apache Spark Connect Swift Client |
| 2 | + |
| 3 | +This project is designed to illustrate a Swift-based HTTP WebServer with Apache Spark Connect. |
| 4 | + |
| 5 | +- https://swiftpackageindex.com/apache/spark-connect-swift |
| 6 | +- https://swiftpackageindex.com/vapor/vapor |
| 7 | + |
| 8 | +## Create a Swift project |
| 9 | + |
| 10 | +``` |
| 11 | +brew install vapor |
| 12 | +vapor new spark-connect-swift-web -n |
| 13 | +``` |
| 14 | + |
| 15 | +## Use `Apache Spark Connect Swift Client` package. |
| 16 | + |
| 17 | +``` |
| 18 | +$ git diff HEAD |
| 19 | +diff --git a/Package.swift b/Package.swift |
| 20 | +index 477bcbd..3e7bb06 100644 |
| 21 | +--- a/Package.swift |
| 22 | ++++ b/Package.swift |
| 23 | +@@ -4,13 +4,14 @@ import PackageDescription |
| 24 | + let package = Package( |
| 25 | + name: "SparkConnectSwiftWebapp", |
| 26 | + platforms: [ |
| 27 | +- .macOS(.v13) |
| 28 | ++ .macOS(.v15) |
| 29 | + ], |
| 30 | + dependencies: [ |
| 31 | + // 💧 A server-side Swift web framework. |
| 32 | + .package(url: "https://github.com/vapor/vapor.git", from: "4.110.1"), |
| 33 | + // 🔵 Non-blocking, event-driven networking for Swift. Used for custom executors |
| 34 | + .package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"), |
| 35 | ++ .package(url: "https://github.com/apache/spark-connect-swift.git", branch: "main"), |
| 36 | + ], |
| 37 | + targets: [ |
| 38 | + .executableTarget( |
| 39 | +@@ -19,6 +20,7 @@ let package = Package( |
| 40 | + .product(name: "Vapor", package: "vapor"), |
| 41 | + .product(name: "NIOCore", package: "swift-nio"), |
| 42 | + .product(name: "NIOPosix", package: "swift-nio"), |
| 43 | ++ .product(name: "SparkConnect", package: "spark-connect-swift"), |
| 44 | + ], |
| 45 | + swiftSettings: swiftSettings |
| 46 | + ), |
| 47 | +diff --git a/Sources/SparkConnectSwiftWebapp/routes.swift b/Sources/SparkConnectSwiftWebapp/routes.swift |
| 48 | +index 2edcc8f..22313c8 100644 |
| 49 | +--- a/Sources/SparkConnectSwiftWebapp/routes.swift |
| 50 | ++++ b/Sources/SparkConnectSwiftWebapp/routes.swift |
| 51 | +@@ -1,4 +1,5 @@ |
| 52 | + import Vapor |
| 53 | ++import SparkConnect |
| 54 | +
|
| 55 | + func routes(_ app: Application) throws { |
| 56 | + app.get { req async in |
| 57 | +@@ -6,6 +7,15 @@ func routes(_ app: Application) throws { |
| 58 | + } |
| 59 | +
|
| 60 | + app.get("hello") { req async -> String in |
| 61 | +- "Hello, world!" |
| 62 | ++ return await Task { |
| 63 | ++ do { |
| 64 | ++ let spark = try await SparkSession.builder.getOrCreate() |
| 65 | ++ let response = "Hi, this is powered by the Apache Spark \(await spark.version)." |
| 66 | ++ await spark.stop() |
| 67 | ++ return response |
| 68 | ++ } catch { |
| 69 | ++ return "Fail to connect: \(error)" |
| 70 | ++ } |
| 71 | ++ }.value |
| 72 | + } |
| 73 | + } |
| 74 | +``` |
| 75 | + |
| 76 | +## Run this Swift application. |
| 77 | + |
| 78 | +``` |
| 79 | +$ swift run |
| 80 | +``` |
| 81 | + |
| 82 | +## Connect to the Swift Web Server to talk with `Apache Spark`. |
| 83 | + |
| 84 | +``` |
| 85 | +$ curl http://127.0.0.1:8080/ |
| 86 | +Welcome to the Swift world. Say hello!% |
| 87 | +$ curl http://127.0.0.1:8080/hello |
| 88 | +Hi, this is powered by the Apache Spark 4.0.0.% |
| 89 | +``` |
0 commit comments