Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ let package = Package(
.product(name: "FlatBuffers", package: "flatbuffers"),
]
),
.executableTarget(
name: "SparkSQLRepl",
dependencies: ["SparkConnect"]
),
.testTarget(
name: "SparkConnectTests",
dependencies: ["SparkConnect"],
Expand Down
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,73 @@ SELECT * FROM t

You can find this example in the following repository.
- https://github.com/dongjoon-hyun/spark-connect-swift-app

## How to use `Spark SQL REPL` via `Spark Connect for Swift`

This project also provides `Spark SQL REPL`. You can run it directly from this repository.

```
$ swift run
...
Build of product 'SQLRepl' complete! (2.33s)
Connected to Apache Spark 4.0.0 Server
spark-sql (default)> SHOW DATABASES;
+---------+
|namespace|
+---------+
| default|
+---------+

Time taken: 30 ms
spark-sql (default)> CREATE DATABASE db1;
++
||
++
++

Time taken: 31 ms
spark-sql (default)> USE db1;
++
||
++
++

Time taken: 27 ms
spark-sql (db1)> CREATE TABLE t1 AS SELECT * FROM RANGE(10);
++
||
++
++

Time taken: 99 ms
spark-sql (db1)> SELECT * FROM t1;
+---+
| id|
+---+
| 1|
| 5|
| 3|
| 0|
| 6|
| 9|
| 4|
| 8|
| 7|
| 2|
+---+

Time taken: 80 ms
spark-sql (db1)> USE default;
++
||
++
++

Time taken: 26 ms
spark-sql (default)> DROP DATABASE db1 CASCADE;
++
||
++
++
spark-sql (default)> exit;
```
52 changes: 52 additions & 0 deletions Sources/SparkSQLRepl/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// 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 Foundation
import SparkConnect

let statement = /([^;]*);/

let spark = try await SparkSession.builder.getOrCreate()
print("Connected to Apache Spark \(await spark.version) Server")

var isRunning = true
while isRunning {
print("spark-sql (\(try await spark.catalog.currentDatabase()))> ", terminator: "")
guard let input = readLine() else {
isRunning = false
break
}

let matches = input.matches(of: statement)
for match in matches {
switch match.1 {
case "exit":
isRunning = false
break
default:
do {
try await spark.time(spark.sql(String(match.1)).show)
} catch {
print("Error: \(error)")
}
}
}
}

await spark.stop()
Loading