概要
Elasticsearechに対してSQLで結果を取得できるSQL Accessが6.3から提供されています。
SQL AccessのためのCLIは、Elasticsearch本体に含まれていますが、
SQLでSELECTして結果をCSVで保存するということを、外からCIサーバやバッチでやりたいと思うのは、人情です。
それをやってみましょう、という内容です。
まず読むもの
The jar containing the SQL CLI is a stand alone Java application and the scripts just launch it. You can move it around to other machines without having to install Elasticsearch on them.
You can move it around to other machines without having to install Elasticsearch on them.
と書いてあるので、jarファイルだけ他所に持って行って動かしてしまいしょう。
そのjarファイルは、 /usr/share/elasticsearch/bin/elasticsearch-sql-cli-6.3.0.jar にあります。
(Elastic社が公開しているDockerイメージだと)
方法
SQLCLIコマンドの第1引数に接続先のElasticsearchサーバのURLを書けば良い、となっています。
You can pass the URL of the Elasticsearch instance to connect to as the first parameter:
$ ./bin/elasticsearch-sql-cli https://some.server:9200
jarファイルだけを持って行った例だと、このように書けます。
java -jar /usr/share/elasticsearch/bin/elasticsearch-sql-cli-6.3.2.jar
通常起動すると、このように対話型モードとなっています。
対話型モードでないオプションはないかな?と ヘルプを確認すると、こうなっています。
Elasticsearch SQL CLI
Non-option arguments:
uri
Option Description
------ -----------
-c, --check <Boolean> Enable initial connection check on startup (default:
true)
-d, --debug Enable debug logging
-h, --help show help
-k, --keystore_location Location of a keystore to use when setting up SSL. If
specified then the CLI will prompt for a keystore
password. If specified when the uri isn't https then
an error is thrown.
-s, --silent show minimal output
-v, --verbose show verbose output
コマンドを受け付けるオプションは特に用意されてないようです。(MySQLのeオプションのような)
それでも外からSQL渡したい
定番のパイプやリダイレクトでSQLを渡すことができます。
echo "show tables;" | \
java -jar /usr/share/elasticsearch/bin/elasticsearch-sql-cli-6.3.2.jar http:/xxx.xxx.xxx.xxx:9200
WARNINGも消したいなら
この起動方法だと、WARNINGログが出てしまうので、標準出力のみを残すようにする場合だとこんな感じに。
echo "show tables;" |\
java -jar /usr/share/elasticsearch/bin/elasticsearch-sql-cli-6.3.2.jar \
http:/xxx.xxx.xxx.xxx:9200 2>/dev/null
X-Packのロゴが・・・
そんなあなたはsedコマンドを。
echo "show tables;" |\
java -jar /usr/share/elasticsearch/bin/elasticsearch-sql-cli-6.3.2.jar \
http:/xxx.xxx.xxx.xxx:9200 2>/dev/null | \
sed -e '1,26d'
まとめ
jarファイル1つで、sqlcliが動くのは心強い。
SQLCLIの使いどころは対話型で何かやるところ。無理やり単体で使おうとするより、決まったSQLを投げて結果を取得する、という目的であればcurlで_xpack/sqlにリクエストを投げる方が何倍も楽。
(あたりまえの結論だけども)
過去記事
Elasticsearch 6.3.0 リリース記念 X-Pack SQL Accessをやってみよう
[番外編] Dockerにするなら
FROM openjdk:10-slim
ENV ELASTICSEARCH_URL= \
SQL_VERSION=6.3.2
COPY run.sh /
RUN chmod +x /run.sh
ENTRYPOINT ["/run.sh"]
COPY elasticsearch-sql-cli-${SQL_VERSION}.jar /usr/share/elasticsearch/bin/
で、entrypointに指定しているrun.shはこんな感じに。
#!/bin/bash
SQL="-h"
if [ $# -eq 1 ]; then
SQL=$1
fi
if [ -z "$ELASTICSEARCH_URL" ] && [ "${ELASTICSEARCH_URL:-A}" = "${ELASTICSEARCH_URL-A}" ] ; then
ELASTICSEARCH_URL=http://localhost:9200
fi
echo $SQL | java -jar /usr/share/elasticsearch/bin/elasticsearch-sql-cli-${SQL_VERSION}.jar \
${ELASTICSEARCH_URL} 2>/dev/null \
| sed -e '1,26d'
こうすると、docker runでSQL文を渡すと結果が返ってくる、ということができるようになります。
(-eオプションでElasticsearchの場所を、CMDでSQL文を渡している)
docker run --rm -it -e ELASTICSEARCH_URL=http://xxx.xxx.xxx.xxx:9200 es:latest "SELECT \* FROM test;"
[番外編] 違うバージョンのESに接続しようとしたら
SQLCLIのjarは、6.3.0として、試しに2つの環境に接続をしてみました。
- ES6.3.2への接続は出来る
- ES5.6.0への接続は不可
ERROR: Cannot communicate with the server http://xxx.xxx.xxx.xxx:9200. This version of CLI only works with Elasticsearch version v6.3.0 [424e937]
特段の事情がない限り、バージョンはあわせておくのが吉ですな。