本文詳細介紹了不一樣版本中Elasticsearch SQL的使用方法,總結了實際中經常使用的方法和操做,並給出了幾個具體例子。html
1、5.x中ES-SQL用法Elasticsearch 5.x版本中,SQL功能尚未集成到Elasticsearch源碼中,須要下載第三方插件後才能使用,配置過程以下: node
1.安裝ES-SQL依賴node npm git
ES-SQL 5.x版本之後,安裝須要依賴node和npm,先安裝node和npm,安裝後在檢查node及npm的安裝,命令以下: github
yum -y install nodejs npm
node -v
npm -v
2.下載ES-SQL並安裝 sql
而後切換到ES的根目錄下,執行以下命令,下載並安ES-SQL插件: express
./bin/elasticsearch-plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/5.6.3.0/elasticsearch-sql-5.6.3.0.zip
離線包安裝能夠執行: npm
./bin/elasticsearch-plugin install file:/elasticsearch-sql-5.6.3.0.zip
3.重啓ES服務 api
執行完上述三步,你就可使用SQL探索數據了,以kibana中的使用爲例:app
2、6.4 Elasticsearch SQL用法 1Elasticsearch SQL支持的數據類型
首先咱們看下Elasticsearch SQL和標準SQL中數據類型的對應關係:dom
2Elasticsearch SQL的使用方式
Elasticsearch SQL支持三種client:REST Interface, command-line,JDBC
2.1 REST Interface
建議先在kibana中測試(能夠一次執行多個SQL),查詢經過以後把查詢copy到項目中進行測試。
6.3+ Elasticsearch SQL有個很是實用的功能,就是能夠用translate api把SQL語句翻譯成ES DSL語句,對於學習DSL感到頭痛的同窗有福啦。
2.2 command-line
命令行界面的進入方式:
./elasticsearch-sql-cli IP:PORT(本機ip和es的端口)
進入後的界面以下:
命令行通常做爲SQL測試時使用。
2.3 JDBC
該組件爲X-Pack中的收費組件,感興趣的同窗能夠參考官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-jdbc.html
3經常使用SQL語句
注意:查詢單個索引名必定要用""引上,不然會報錯
*查看當前用戶全部的索引:「SHOW TABLES;」
精準查詢某個索引:「SHOW TABLES LIKE ‘indexname’;」
通配符查詢某些索引:「SHOW TABLES LIKE ‘ ’;」
*查看某個索引結構:「DESCRIBE table;」 或者 「DESC table;」
上面兩個命令都是「SHOW COLUMNS [ FROM | IN ] ? table」命令的別名
*查看函數:「SHOW FUNCTIONS [ LIKE? pattern? ]?」
精準查詢某個函數:
通配符查詢某些函數:
查看全部函數:
經常使用的聚合函數:
SELECT MIN(value_1) min, MAX(value_1) max, AVG(value_1) avg,SUM(value_1) sum,COUNT(*) count,COUNT(DISTINCT value_1) dictinct_count FROM "micloud_es_sink_zhouyongbo_test-2018.10.19」;
SELECT語句的語法排序以下:
SELECT select_expr [, ...]
[ FROM table_name ]
[ WHERE condition ]
[ GROUP BY grouping_element [, ...] ]
[ HAVING condition]
[ ORDER BY expression [ ASC | DESC ] [, ...] ]
[ LIMIT [ count ] ]
*限定返回數據的條數:「limit」
SELECT * FROM "micloud_es_sink_zhouyongbo_test-2018.10.19」 limit 10 ;
注意SQL中的limit比fetch_size中的優先級高,例以下面的例子返回的是5條 :
{
"query": "SELECT * FROM "micloud_es_sink_zhouyongbo_test-2018.10.19」 limit 5",
"fetch_size":10
}
*排序:「order by + 字段名字 + asc/desc」
SELECT * FROM "micloud_es_sink_zhouyongbo_test-2018.10.19」 ORDER BY value_1 asc/desc;
根據多個字段排序:
select city c,value_1 + 1 vp from "micloud_es_sink_zhouyongbo_test-2018.10.19" group by c,vp order by c desc,vp asc;
*WHERE根據條件查詢:
WHERE後面跟ES複雜數據類型:
SELECT first_name FROM index WHERE first_name.raw = ‘John’ ;
WHERE後面跟多個查詢條件:
SELECT * FROM micloud_es_sink_zhouyongbo_test* where city=‘北京' and value_1=8 ORDER BY value_1 desc ;
*group by分組查詢:
根據單個字段分組查詢:
select city,count(city) as count_city,sum(value_1) as count_value_1 from "micloud_es_sink_zhouyongbo_test-2018.10.19" group by city;
根據多個字段分組查詢:
select city,count(city) count_city,sum(value_1) count_value_1 from "micloud_es_sink_zhouyongbo_test-2018.10.19" group by city,value_1;
對於比較長的字段,也能夠對該字段聲明別名,並對別名進行分組查詢,聲明字段別名的「as」可省略:
select city c,count(city) count_city,sum(value_1) count_value_1 from "micloud_es_sink_zhouyongbo_test-2018.10.19" group by c,value_1;
還能夠對某字段進行計算,而後按照計算結果分組查詢:
select city c,value_1 + 1 vp from "micloud_es_sink_zhouyongbo_test-2018.10.19" group by c,vp;
*HAVING過濾分組結果(ES-SQL引擎一樣會在分組以後計算HAVING語句):
Select city c,count(*) count from "micloud_es_sink_zhouyongbo_test-2018.10.19" group by c having count > 53834;
*查詢嵌套類型:
select * from zhouyongbo_test04 where love.kaishu=‘魯公’;
*用通配符查詢多個索引:
注意被查詢索引必須有相同的mapping,不然會有以下報錯:
經常使用的方法和操做彙總:
*比較操做:
Equality (=)
select * from "micloud_es_sink_zhouyongbo_test-2018.10.19" where value_1 = 6 limit 5;
Inequality (<> or != or <=>)
select * from "micloud_es_sink_zhouyongbo_test-2018.10.19" where value_1 <> 6 limit 5;
Comparison (<, <=, >, >=)
select * from "micloud_es_sink_zhouyongbo_test-2018.10.19" where value_1 >= 6 limit 5;
BETWEEN
select * from "micloud_es_sink_zhouyongbo_test-2018.10.19" where value_1 between 6 and 8 limit 5;
IS NULL/IS NOT NULL
select * from "micloud_es_sink_zhouyongbo_test-2018.10.19" where value_1 is not NULL limit 5;
*邏輯操做:
AND
select * from "micloud_es_sink_zhouyongbo_test-2018.10.19" where value_1 > 5 and value_1 < 7 limit 5;
OR
select * from "micloud_es_sink_zhouyongbo_test-2018.10.19" where value_1 = 5 or value_1 = 7 limit 5;
NOT
select * from "micloud_es_sink_zhouyongbo_test-2018.10.19" where not value_1 > 5 limit 5;
*數學運算操做:
Add (+)
select 1 + 1 as x;
Subtract (infix -)
select 1 - 1 as x;
Negate (unary -)
select - 1 as x;
Multiply (*)
select 6 * 6 as x;
Divide (/)
select 30 / 5 as x;
Modulo or Reminder(%)
select 30 % 7 as x;
*數學函數:(分爲通用函數和三角函數兩部分 ):
通用函數:
ABS:求數字的絕對值
select ABS(value_1) from "micloud_es_sink_zhouyongbo_test-2018.10.19" limit 5;
CBRT:求數字的立方根,返回double
select value_1 v,CBRT(value_1) cbrt from "micloud_es_sink_zhouyongbo_test-2018.10.19" limit 5;
CEIL:返回大於或者等於指定表達式最小整數(double)
select value_1 v,CEIL(value_1) from "micloud_es_sink_zhouyongbo_test-2018.10.19" limit 5;
CEILING:等同於CEIL
select value_1 v,CEILING(value_1) from "micloud_es_sink_zhouyongbo_test-2018.10.19" limit 5;
E:返回天然常數e(2.718281828459045)
select value_1,E(value_1) from "micloud_es_sink_zhouyongbo_test-2018.10.19" limit 5;
ROUND:四捨五入精確到個位
select ROUND(-3.14);
FLOOR:向下取整
select FLOOR(3.14);
LOG:計算以2爲底的天然對數
select LOG(4);
LOG10:計算以10爲底的天然對數
select LOG10(100);
SQRT:求一個非負實數的平方根
select SQRT(9);
EXP:此函數返回e(天然對數的底)的X次方的值
select EXP(3);
EXPM1:返回e x -1
select EXPM1(3);
三角函數:
DEGREES:返回X從弧度轉換爲度值
select DEGREES(x);
RADIANS:返回X從度轉換成弧度的值
select RADIANS(x);
SIN:返回X的正弦
select SIN(x);
COS:返回X,X值是以弧度給出的餘弦值
select COS(角度);
TAN:返回參數X,表示以弧度的切線值
select TAN(角度);
ASIN:返回X的反正弦,X的值必須在-1至1範圍內,返回NULL
select ASIN(x);
ACOS:返回X的反正弦,X值必須-1到1之間範圍不然將返回NULL
select ACOS(x);
ATAN:返回X的反正切
select ATAN(x);
SINH:返回X的雙曲正弦值
select SINH(x);
COSH:返回X的雙曲餘弦值
select COSH(x);
*日期和時間處理相關方法:
YEAR:
SELECT YEAR(CAST('2018-10-23T16:59:27Z' AS TIMESTAMP)) AS year;
MONTH_OF_YEAR() or MONTH():
SELECT MONTH(CAST('2018-10-23T16:59:27Z' AS TIMESTAMP)) AS month;
WEEK_OF_YEAR() or WEEK():
SELECT WEEK(CAST('2018-10-23T16:59:27Z' AS TIMESTAMP)) AS week;
DAY_OF_YEAR() or DOY(),效果等同於EXTRACT(<datetime_function> FROM <expression>):
SELECT DOY(CAST('2018-10-23T16:59:27Z' AS TIMESTAMP)) AS day;
DAY_OF_MONTH(), DOM(), or DAY():
SELECT DAY(CAST('2018-10-23T16:59:27Z' AS TIMESTAMP)) AS day;
DAY_OF_WEEK() or DOW():
SELECT DOW(CAST('2018-10-23T16:59:27Z' AS TIMESTAMP)) AS day;
HOUR_OF_DAY() or HOUR():
SELECT HOUR(CAST('2018-10-23T16:59:27Z' AS TIMESTAMP)) AS hour;
MINUTE_OF_DAY():
SELECT MINUTE_OF_DAY(CAST('2018-10-23T16:59:27Z' AS TIMESTAMP)) AS minute;
MINUTE_OF_HOUR() or MINUTE():
SELECT MINUTE(CAST('2018-10-23T16:59:27Z' AS TIMESTAMP)) AS minute;
SECOND_OF_MINUTE() or SECOND():
SELECT SECOND(CAST('2018-10-23T16:59:27Z' AS TIMESTAMP)) AS second;
如上就是6.4 Elasticsearch SQL支持的主要用法了,若是在優化SQL語句以後還不知足查詢需求,能夠拿SQL和DSL混用,ES會先根據SQL進行查詢,而後根據DSL語句對SQL的執行結果進行二次查詢,下面是個小例子:
POST /_xpack/sql?format=txt
{
"query": "SELECT * FROM library ORDER BY page_count DESC",
"filter": {
"range": {
"page_count": {
"gte" : 100,
"lte" : 200
}
}
},
"fetch_size": 5
}
這個查詢就會先根據「query」後面的SQL進行查詢,而後用執行「filter」和「fetch_size」 DSL語法對查詢結果進行過濾,進而返回最終結果。
參考文檔:
6.4.0 Elasticsearch SQL新特性簡介:
https://www.elastic.co/cn/products/stack/elasticsearch-sql
6.4.0 Elasticsearch SQL使用文檔:
https://www.elastic.co/guide/en/elasticsearch/reference/current/xpack-sql.htm