SQL CLI
editSQL CLI
editElasticsearch 自带一个脚本,用于在其 bin
目录中运行 SQL CLI:
$ ./bin/elasticsearch-sql-cli
您可以将要连接的 Elasticsearch 实例的 URL 作为第一个参数传递:
$ ./bin/elasticsearch-sql-cli https://some.server:9200
如果您的集群启用了安全性,您可以在SQL CLI中以username:password@host_name:port
的形式传递用户名和密码:
$ ./bin/elasticsearch-sql-cli https://sql_user:strongpassword@some.server:9200
一旦CLI运行,您可以使用Elasticsearch支持的任何查询:
sql> SELECT * FROM library WHERE page_count > 500 ORDER BY page_count DESC; author | name | page_count | release_date -----------------+--------------------+---------------+--------------- Peter F. Hamilton|Pandora's Star |768 |1078185600000 Vernor Vinge |A Fire Upon the Deep|613 |707356800000 Frank Herbert |Dune |604 |-144720000000 Alastair Reynolds|Revelation Space |585 |953078400000 James S.A. Corey |Leviathan Wakes |561 |1306972800000
包含 SQL CLI 的 jar 文件是一个独立的 Java 应用程序,脚本只是启动它。您可以将其移动到其他机器上,而无需在这些机器上安装 Elasticsearch。如果没有提供脚本文件,您可以使用类似于以下的命令来启动 SQL CLI:
$ ./java -jar [PATH_TO_CLI_JAR]/elasticsearch-sql-cli-[VERSION].jar https://some.server:9200
或
$ ./java -cp [PATH_TO_CLI_JAR]/elasticsearch-sql-cli-[VERSION].jar org.elasticsearch.xpack.sql.cli.Cli https://some.server:9200
每个 Elasticsearch 版本的 jar 名称都会不同(例如 elasticsearch-sql-cli-7.3.2.jar
),
因此在上面的示例中指定了通用的 VERSION
。此外,
如果不在 SQL CLI jar 所在的文件夹中运行命令,
您还需要提供完整路径。
CLI 命令
edit除了SQL查询外,CLI还可以执行一些特定命令:
-
allow_partial_search_results = <boolean>
(defaultfalse
) -
如果
true
,则在存在分片请求超时或 分片失败时返回部分结果。如果false
,则返回错误且不包含部分结果。
sql> allow_partial_search_results = true; allow_partial_search_results set to true
-
fetch_size = <number>
(default1000
) - 允许更改查询执行的获取大小。 每次获取由获取分隔符(如果明确设置)分隔。
sql> fetch_size = 2000; fetch size set to 2000
-
fetch_separator = <string>
(empty string by default) - 允许更改每次获取之间的分隔符字符串。
sql> fetch_separator = "---------------------"; fetch separator set to "---------------------"
-
lenient = <boolean>
(defaultfalse
) -
如果
false
,Elasticsearch SQL 会对包含数组值的字段返回错误。 如果true
,Elasticsearch SQL 会返回数组中的第一个值,但不保证结果的一致性。
sql> lenient = true; lenient set to true
-
info
- 返回服务器信息。
sql> info; Node:mynode Cluster:elasticsearch Version:8.3
-
exit
- 关闭CLI。
sql> exit; Bye!
-
cls
- 清屏。
sql> cls;
-
logo
- 打印 Elastic 标志。
sql> logo; asticElasticE ElasticE sticEla sticEl ticEl Elast lasti Elasti tic cEl ast icE icE as cEl icE as cEl icEla las El sticElasticElast icElas las last ticElast El asti asti stic El asticEla Elas icE El Elas cElasticE ticEl cE Ela ticEl ticElasti cE las astic last icE sticElas asti stic icEl sticElasticElast icE sticE ticEla icE sti cEla icEl sti Ela cEl sti cEl Ela astic ticE asti ElasticElasti ticElasti lasticElas ElasticElast SQL 8.3.0