1 - 簡介
elasticsearch-sql以插件的形式運行在ES中,攔截_sql開頭的請求,將請求中的sql語句解釋成es的DSL查詢語句,在ES內(nèi)部調(diào)用執(zhí)行后,將結(jié)果返回給用戶。
部署后的效果:
curl -XGET -u test_user:test_user "192.168.1.1:9299/_sql" -H 'Content-Type: application/json' -d'select * from agile_subscribe_2_20200526_120400 limit 2'
2 - 安裝
下載地址:https://github.com/NLPchina/elasticsearch-sql
版本要與es版本對應(yīng)
下載好之后,上傳到ES服務(wù)器,安裝
./bin/elasticsearch-plugin install file:/opt/soft/elasticsearch-sql-6.8.0.0.zip
修改ES配置文件,添加http相關(guān)參數(shù),用來支持可視化頁面。
http.cors.enabled: true
http.cors.allow-credentials: true
http.cors.allow-origin: "/.*/"
http.cors.allow-headers: WWW-Authenticate,X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization
修改好配置文件后,重啟ES集群。
3 - 命令行訪問
重啟ES集群后,命令行即可使用。
curl -XGET -u test_user:test_user "192.168.1.1:9299/_sql" -H 'Content-Type: application/json' -d'select * from indexname limit 2'
4 - 可視化頁面
ES 5.x之后,需要安裝 node.js 和下載及解壓site,然后像這樣啟動web前端:
cd site-server
npm install express --save
node node-server.js
然后在Chrome中a
此時,訪問[http://site-server:8080/](http://site-server:8080/),即可看到可視化頁面。

可視化頁面
如果ES啟用了xpack認證,那么訪問的地址需要更改:
http://site-server:8080/?username=test_user&password=test_user&base_uri=http://192.168.1.1:9299
base_uri是上圖中的ES集群地址,username和password是相應(yīng)的賬號、密碼。配置好之后,享受在ES上運行SQL的暢快吧。