|
| 1 | +# A `Spark SQL REPL` Application with Apache Spark Connect Swift Client |
| 2 | + |
| 3 | +This is an example Swift application to show how to develop a Spark SQL REPL(Read-eval-print Loop) with Apache Spark Connect Swift Client library. |
| 4 | + |
| 5 | +## How to run |
| 6 | + |
| 7 | +Prepare `Spark Connect Server` via running Docker image. |
| 8 | + |
| 9 | +``` |
| 10 | +docker run -it --rm -p 15002:15002 apache/spark:4.0.0-preview2 bash -c "/opt/spark/sbin/start-connect-server.sh --wait" |
| 11 | +``` |
| 12 | + |
| 13 | +Build an application Docker image. |
| 14 | + |
| 15 | +``` |
| 16 | +$ docker build -t apache/spark-connect-swift:spark-sql . |
| 17 | +$ docker images apache/spark-connect-swift:spark-sql |
| 18 | +REPOSITORY TAG IMAGE ID CREATED SIZE |
| 19 | +apache/spark-connect-swift spark-sql 265ddfec650d 7 seconds ago 390MB |
| 20 | +``` |
| 21 | + |
| 22 | +Run `spark-sql` docker image. |
| 23 | + |
| 24 | +``` |
| 25 | +$ docker run -it --rm -e SPARK_REMOTE=sc://host.docker.internal:15002 apache/spark-connect-swift:spark-sql |
| 26 | +Connected to Apache Spark 4.0.0-preview2 Server |
| 27 | +spark-sql (default)> SHOW DATABASES; |
| 28 | ++---------+ |
| 29 | +|namespace| |
| 30 | ++---------+ |
| 31 | +|default | |
| 32 | ++---------+ |
| 33 | +
|
| 34 | +Time taken: 30 ms |
| 35 | +spark-sql (default)> CREATE DATABASE db1; |
| 36 | +++ |
| 37 | +|| |
| 38 | +++ |
| 39 | +++ |
| 40 | +
|
| 41 | +Time taken: 31 ms |
| 42 | +spark-sql (default)> USE db1; |
| 43 | +++ |
| 44 | +|| |
| 45 | +++ |
| 46 | +++ |
| 47 | +
|
| 48 | +Time taken: 27 ms |
| 49 | +spark-sql (db1)> CREATE TABLE t1 AS SELECT * FROM RANGE(10); |
| 50 | +++ |
| 51 | +|| |
| 52 | +++ |
| 53 | +++ |
| 54 | +
|
| 55 | +Time taken: 99 ms |
| 56 | +spark-sql (db1)> SELECT * FROM t1; |
| 57 | ++---+ |
| 58 | +| id| |
| 59 | ++---+ |
| 60 | +| 1| |
| 61 | +| 5| |
| 62 | +| 3| |
| 63 | +| 0| |
| 64 | +| 6| |
| 65 | +| 9| |
| 66 | +| 4| |
| 67 | +| 8| |
| 68 | +| 7| |
| 69 | +| 2| |
| 70 | ++---+ |
| 71 | +
|
| 72 | +Time taken: 80 ms |
| 73 | +spark-sql (db1)> USE default; |
| 74 | +++ |
| 75 | +|| |
| 76 | +++ |
| 77 | +++ |
| 78 | +
|
| 79 | +Time taken: 26 ms |
| 80 | +spark-sql (default)> DROP DATABASE db1 CASCADE; |
| 81 | +++ |
| 82 | +|| |
| 83 | +++ |
| 84 | +++ |
| 85 | +spark-sql (default)> exit; |
| 86 | +``` |
| 87 | + |
| 88 | +Apache Spark 4 supports [SQL Pipe Syntax](https://dist.apache.org/repos/dist/dev/spark/v4.0.0-rc6-docs/_site/sql-pipe-syntax.html). |
| 89 | + |
| 90 | +``` |
| 91 | +$ swift run |
| 92 | +... |
| 93 | +Build of product 'SparkSQLRepl' complete! (2.33s) |
| 94 | +Connected to Apache Spark 4.0.0 Server |
| 95 | +spark-sql (default)> |
| 96 | +FROM ORC.`/opt/spark/examples/src/main/resources/users.orc` |
| 97 | +|> AGGREGATE COUNT(*) cnt |
| 98 | + GROUP BY name |
| 99 | +|> ORDER BY cnt DESC, name ASC |
| 100 | +; |
| 101 | ++------+---+ |
| 102 | +| name|cnt| |
| 103 | ++------+---+ |
| 104 | +|Alyssa| 1| |
| 105 | +| Ben| 1| |
| 106 | ++------+---+ |
| 107 | +
|
| 108 | +Time taken: 159 ms |
| 109 | +``` |
| 110 | + |
| 111 | +Run from source code. |
| 112 | + |
| 113 | +``` |
| 114 | +$ swift run |
| 115 | +... |
| 116 | +Connected to Apache Spark 4.0.0.9-apple-SNAPSHOT Server |
| 117 | +spark-sql (default)> |
| 118 | +``` |
0 commit comments