Elasticsearch的查詢語言(DSL)真是很差寫,恰恰查詢的功能千奇百怪,filter/query/match/agg/geo各類各樣,無論你是經過封裝JSON仍是經過python/java的api進行封裝,都很是不方便。java
最近發現了一個插件,Elasticsearch-SQL能夠用sql查詢Elasticsearch,感受這個輪子造的真是好。python
Elasticsearch-sql的項目地址:https://github.com/NLPchina/elasticsearch-sqlgit
一、簡介github
Elasticsearch-sql實現的功能:web
1)插件式的安裝sql
2)SQL查詢編程
3)超越SQL以外的查詢json
4)對JDBC方式的支持api
二、插件式的安裝curl
安裝方法和elasticsearch-head的安裝方法相似:
咱們使用的es版本是2.3.3,若是你用的是不一樣的版本,能夠在https://github.com/NLPchina/elasticsearch-sql找到支持。
$ cd ~/elasticsearch-2.3.3 $./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.3.3.0/elasticsearch-sql-2.3.3.0.zip
若是成功,命令行打印以下東東: 複製代碼
[bigdata-dw@bigdata-arch-client10 es2.1.1]$ ./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.3.3.0/elasticsearch-sql-2.3.3.0.zip -> Installing from https://github.com/NLPchina/elasticsearch-sql/releases/download/2.3.3.0/elasticsearch-sql-2.3.3.0.zip... Trying https://github.com/NLPchina/elasticsearch-sql/releases/download/2.3.3.0/elasticsearch-sql-2.3.3.0.zip ... Downloading .................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE Verifying https://github.com/NLPchina/elasticsearch-sql/releases/download/2.3.3.0/elasticsearch-sql-2.3.3.0.zip checksums if available ... NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify) Installed sql into /home/bigdata-dw/es2.1.1/plugins/sql
複製代碼
三、SQL查詢
安裝成功之後咱們就能夠經過sql查詢ES了。
es-sql還提供了web頁面,訪問方式是http://10.93.18.34:9200/_plugin/sql/(若是你使用head,那麼你的head訪問應該是http://10.93.18.34:9200/_plugin/head/)
這裏的ip和port是你安裝es的主機和http端口。
訪問到的頁面是這樣的
那麼你如今有兩種方式能夠執行你的SQL:
1)在搜索框裏直接輸入你的sql了。(個人版本行尾不要寫「;」不然會解析不了SQL)
2)經過http請求如
curl -XPOST http://10.93.18.34:8049/_sql -d 'SELECT * FROM audit where dDelay=-2053867461'
你會收到一個json格式的返回
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":12.549262,"hits":[{"_index":"audit","_type":"kafka","_id":"AVzzK-h_V9seINxbZ2Ox","_score":12.549262,"_source":{"timestamp":"1498726500000","dCount":680008,"dDelay":-2053867461,"cDelay":0,"clanName":"DJ_elk_common","checkTime":1498728360063,"cCount":0,"pCount":680008,"topicName":"DJ_elk_common_clean","pDelay":370356423}}]}}
下面咱們簡單說4種類型的sql的書寫方式:
1)query
SELECT * FROM bank WHERE age >30 AND gender = 'm'
2)aggregation
select COUNT(*),SUM(age),MIN(age) as m, MAX(age),AVG(age) FROM bank GROUP BY gender ORDER BY SUM(age), m DESC
3)delete
DELETE FROM bank WHERE age >30 AND gender = 'm'
4)geo
SELECT * FROM locations WHERE GEO_BOUNDING_BOX(fieldname,100.0,1.0,101,0.0)
5)須要指定index+type
SELECT * FROM indexName/type
6)如何指定路由
select /*! ROUTINGS(salary) */ sum(count) from index where type="salary"
四、對JDBC的支持
上述查詢方式無論是直接在web上輸入sql仍是經過http請求。elasticsearch-sql還支持經過jdbc進行編程。
這個尚未研究,抽空研究一下再回來。