1. 簡介:
es-sql 以插件的方式運行在es中,攔截_sql開頭的請求,將請求中的sql語句解釋成es的DSL查詢語句,在es內部調用執行后,將結果返回給用戶。
部署后的效果:
rest API 調用 http://xxxx:9200/_sql?sql=select * from ds_alarm
2. 安裝
下載插件
https://github.com/NLPchina/elasticsearch-sql
版本要與es版本對應
將插件壓縮包里的文件解壓到/opt/elasticsearch-5.6.16/plugins/sql目錄下
將sql文件夾的權限賦權給es用戶
chown -R es:elasticsearch /opt/elasticsearch-5.6.16/plugins/sql
然后重啟es 即可。
3. 訪問
此時即可通過sql 查詢es:
http://10.168.4.60:9200/_sql?sql=SELECT x,y,sfdz,sfsj,zjhm from ds_alarm ORDER BY sfsj desc
4. es-sq 可視化頁面(方便測試sql)
es-sql-site-standalone.zip
下載地址
(需要 先安裝node.js 和npm 環境)
解壓es-sql-site-standalone.zip后
> cd site-server
> npm install express –save
# /opt/site/site-server/site_configuration.json中修改端口號為9000
> node node-server.js
5. 支持的sql
大部分簡單的sql都能支持
用法見官方文檔
https://github.com/NLPchina/elasticsearch-sql/wiki
http://10.168.4.60:9200/_sql?sql=SELECT x,y,sfdz,sfsj,zjhm from ds_alarm ORDER BY sfsj desc
支持多表關聯查詢
不支持
SELECT a.command_id,a.bjrxm,c.alarm_id FROM ds_alarm a,ds_command c where a.command_id = c.command_id
支持
SELECT a.command_id,a.bjrxm,c.alarm_id FROM ds_alarm a Join ds_command c on a.command_id=c.command_id
支持 inner join 和 left join
join 只支持兩張表
join中, on 后面的條件只支持and;
join 查詢結果不支持 order by 或 group by , 且 limit 不支持加 offset
支持between and,
支持給字段取別名,
支持在join 之后 添加條件
支持對單表查詢結果order by
不支持 '常量 field' 來在輸出結果中增加列 如'1 jllx'
iner join 示例:
SELECT x,y,sfdz,sfsj,zjhm from ds_alarm ORDER BY sfsj desc
SELECT a.x,a.y,a.sfdz,a.sfsj,a.zjhm,a.bjr_lxdh as bjrLxdh,a.command_id as commandId,a.aymc,a.swzk,a.ssxqmc
from ds_alarm a JOIN ds_command b on a.command_id = b.command_id
where a.sfsj BETWEEN '2019/11/15 18:11:10' and '2019/11/16 11:11:10'
left join 示例
支持
SELECT a.aymc,a.ssxqmc,a.sfsj,a.sfdz,COALESCE(b.handle_status,'1') handleStatus,a.command_id commandId,a.x,a.y
FROM ds_alarm a LEFT JOIN handle_cases b
ON a.command_id = b.command_id
WHERE a.sfsj BETWEEN '2019/11/15 18:11:10' and '2019/11/16 11:11:10'
支持的語法:
SQL Statements
SQL Select
SQL Delete
SQL Where
SQL Order By
SQL Group By
SQL Limit (default is 200)
Conditions:
SQL Like
SQL AND & OR
SQL COUNT distinct
SQL In
SQL Between
SQL Aliases
SQL(ES) Date
SQL now()
SQL NOT
Basic aggregations:
SQL avg()
SQL count()
SQL last()
SQL max()
SQL min()
SQL sum()
SQL Fields
Fields can be listed out by exact field name or used with the include/exclude syntax for use with wildcards.
include('d*') - include all fields starting with "d"
exclude('age') - include all fields except "age"
include('*Name'), exclude('lastName') - include all fields that end with "Name" except "lastName"
支持es的一些特性
Some more features using ElasticSearch capabilities
ES TopHits
ES MISSING
ES STATS
ES EXTENDED_STATS
ES PERCENTILES
ES TERMS/TERM
ES IDS syntax: IDS_QUERY(type, ids..)
ES SCRIPTED_METRIC (read about it on Aggregations page)
ES QUERY_STRING
支持地理信息的計算查詢,詳見
https://github.com/NLPchina/elasticsearch-sql/wiki/Geographic-Queries
SELECT * FROM 表名 WHERE GEO_INTERSECTS(location,'POLYGON ((102 2, 103 2, 103 3, 102 3, 102 2))
SELECT * FROM 表名 WHERE GEO_DISTANCE(location,'1km',100.5,0.5)
SELECT * FROM ds_alarm WHERE GEO_DISTANCE(center,'1m','1km',100.5,0.50001)