-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-37558][DOC] Improve spark sql cli document #34821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
01848dc
[SPARK-37558][DOC] Improve spark sql cli document
AngersZhuuuu 7a9ecbf
update
AngersZhuuuu a0218f8
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 1c59308
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu cea5be4
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 8eb04bf
Merge branch 'master' into SPARK-37558
AngersZhuuuu 36ce29f
follow comment
AngersZhuuuu 21fb349
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 0c77217
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu e274700
Update sql-distributed-sql-engine-spark-sql-cli.md
cloud-fan 6d6b0cf
Update docs/sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu a06b166
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 8d2fa9a
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 4a7ca98
Update docs/sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 45ec6a0
Update docs/sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 403d966
Merge branch 'master' into SPARK-37558
AngersZhuuuu d9f150c
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 453f260
Update sql-distributed-sql-engine-spark-sql-cli.md
cloud-fan d4f6778
Update docs/sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 29f3b45
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 2b6f130
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu 147d8b0
Update sql-distributed-sql-engine-spark-sql-cli.md
AngersZhuuuu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| --- | ||
| layout: global | ||
| title: Spark SQL CLI | ||
| displayTitle: Spark SQL CLI | ||
| license: | | ||
| 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. | ||
| --- | ||
|
|
||
| * Table of contents | ||
| {:toc} | ||
|
|
||
|
|
||
| The Spark SQL CLI is a convenient interactive command tool to run the Hive metastore service and execute SQL | ||
| queries input from the command line. Note that the Spark SQL CLI cannot talk to the Thrift JDBC server. | ||
|
|
||
| To start the Spark SQL CLI, run the following in the Spark directory: | ||
|
|
||
| ./bin/spark-sql | ||
|
|
||
| Configuration of Hive is done by placing your `hive-site.xml`, `core-site.xml` and `hdfs-site.xml` files in `conf/`. | ||
|
|
||
| ## Spark SQL Command Line Options | ||
|
|
||
| You may run `./bin/spark-sql --help` for a complete list of all available options. | ||
|
|
||
| CLI options: | ||
| -d,--define <key=value> Variable substitution to apply to Hive | ||
| commands. e.g. -d A=B or --define A=B | ||
| --database <databasename> Specify the database to use | ||
| -e <quoted-query-string> SQL from command line | ||
| -f <filename> SQL from files | ||
| -H,--help Print help information | ||
| --hiveconf <property=value> Use value for given property | ||
| --hivevar <key=value> Variable substitution to apply to Hive | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| commands. e.g. --hivevar A=B | ||
| -i <filename> Initialization SQL file | ||
| -S,--silent Silent mode in interactive shell | ||
| -v,--verbose Verbose mode (echo executed SQL to the | ||
| console) | ||
|
|
||
| ## The hiverc File | ||
|
|
||
| When invoked without the `-i`, the Spark SQL CLI will attempt to load `$HIVE_HOME/bin/.hiverc` and `$HOME/.hiverc` as initialization files. | ||
|
|
||
| ## Path interpretation | ||
|
|
||
| Spark SQL CLI supports running SQL from initialization script file(`-i`) or normal SQL file(`-f`), If path url don't have a scheme component, the path will be handled as local file. | ||
| For example: `/path/to/spark-sql-cli.sql` equals to `file:///path/to/spark-sql-cli.sql`. User also can use Hadoop supported filesystems such as `s3://<mys3bucket>/path/to/spark-sql-cli.sql` or `hdfs://<namenode>:<port>/path/to/spark-sql-cli.sql`. | ||
|
|
||
| ## Supported comment types | ||
|
|
||
| <table class="table"> | ||
| <tr><th>Comment</th><th>Example</th></tr> | ||
| <tr> | ||
| <td>simple comment</td> | ||
| <td> | ||
| <code> | ||
| -- This is a simple comment. | ||
| <br> | ||
| SELECT 1; | ||
| </code> | ||
| </td> | ||
| </tr> | ||
| <tr> | ||
| <td>bracketed comment</td> | ||
| <td> | ||
| <code> | ||
| /* This is a bracketed comment. */ | ||
| <br> | ||
| SELECT 1; | ||
| </code> | ||
| </td> | ||
| </tr> | ||
| <tr> | ||
| <td>nested bracketed comment</td> | ||
| <td> | ||
| <code> | ||
| /* This is a /* nested bracketed comment*/ .*/ | ||
| <br> | ||
| SELECT 1; | ||
| </code> | ||
| </td> | ||
| </tr> | ||
| </table> | ||
|
|
||
| ## Spark SQL CLI Interactive Shell Commands | ||
|
|
||
| When `./bin/spark-sql` is run without either the `-e` or `-f` option, it enters interactive shell mode. | ||
| Use `;` (semicolon) to terminate commands. Notice: | ||
| 1. The CLI use `;` to terminate commands only when it's at the end of line, and it's not escaped by `\\;`. | ||
| 2. `;` is the only way to terminate commands. If the user types `SELECT 1` and presses enter, the console will just wait for input. | ||
| 3. If the user types multiple commands in one line like `SELECT 1; SELECT 2;`, the commands `SELECT 1` and `SELECT 2` will be executed separatly. | ||
| 4. If `;` appears within a SQL statement (not the end of the line), then it has no special meanings: | ||
| ```sql | ||
| -- This is a ; comment | ||
| SELECT ';' as a; | ||
| ``` | ||
| This is just a comment line followed by a SQL query which returns a string literal. | ||
| ```sql | ||
| /* This is a comment contains ; | ||
| */ SELECT 1; | ||
| ``` | ||
| However, if ';' is the end of the line, it terminates the SQL statement. The example above will be terminated into `/* This is a comment contains ` and `*/ SELECT 1`, Spark will submit these two commands separated and throw parser error (`unclosed bracketed comment` and `extraneous input '*/'`). | ||
|
|
||
| <table class="table"> | ||
| <tr><th>Command</th><th>Description</th></tr> | ||
| <tr> | ||
| <td><code>quit</code> or <code>exit</code></td> | ||
| <td>Exits the interactive shell.</td> | ||
| </tr> | ||
| <tr> | ||
| <td><code>!<command></code></td> | ||
| <td>Executes a shell command from the Spark SQL CLI shell.</td> | ||
| </tr> | ||
| <tr> | ||
| <td><code>dfs <HDFS dfs command></code></td> | ||
| <td>Executes a HDFS <a href="https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/HDFSCommands.html#dfs">dfs command</a> from the Spark SQL CLI shell.</td> | ||
| </tr> | ||
| <tr> | ||
| <td><code><query string></code></td> | ||
| <td>Executes a Spark SQL query and prints results to standard output.</td> | ||
| </tr> | ||
| <tr> | ||
| <td><code>source <filepath></code></td> | ||
| <td>Executes a script file inside the CLI.</td> | ||
| </tr> | ||
| </table> | ||
|
|
||
| ## Examples | ||
|
|
||
| Example of running a query from the command line: | ||
|
|
||
| ./bin/spark-sql -e 'SELECT COL FROM TBL' | ||
|
|
||
| Example of setting Hive configuration variables: | ||
|
|
||
| ./bin/spark-sql -e 'SELECT COL FROM TBL' --hiveconf hive.exec.scratchdir=/home/my/hive_scratch | ||
|
|
||
| Example of setting Hive configuration variables and using it in the SQL query: | ||
|
|
||
| ./bin/spark-sql -e 'SELECT ${hiveconf:aaa}' --hiveconf aaa=bbb --hiveconf hive.exec.scratchdir=/home/my/hive_scratch | ||
| spark-sql> SELECT ${aaa}; | ||
| bbb | ||
|
|
||
| Example of setting Hive variables substitution: | ||
|
|
||
| ./bin/spark-sql --hivevar aaa=bbb --define ccc=ddd | ||
| spark-sql> SELECT ${aaa}, ${ccc}; | ||
| bbb ddd | ||
|
|
||
| Example of dumping data out from a query into a file using silent mode: | ||
|
|
||
| ./bin/spark-sql -S -e 'SELECT COL FROM TBL' > result.txt | ||
|
|
||
| Example of running a script non-interactively: | ||
|
|
||
| ./bin/spark-sql -f /path/to/spark-sql-script.sql | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Example of running an initialization script before entering interactive mode: | ||
|
|
||
| ./bin/spark-sql -i /path/to/spark-sql-init.sql | ||
|
|
||
| Example of entering interactive mode: | ||
|
|
||
| ./bin/spark-sql | ||
| spark-sql> SELECT 1; | ||
| 1 | ||
| spark-sql> -- This is a simple comment. | ||
| spark-sql> SELECT 1; | ||
| 1 | ||
|
|
||
| Example of entering interactive mode with escape `;` in comment: | ||
|
|
||
| ./bin/spark-sql | ||
| spark-sql>/* This is a comment contains \\; | ||
| > It won't be terminaled by \\; */ | ||
| > SELECT 1; | ||
| 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.