Elasticsearch SQL

1. 簡介

Elasticsearch 6.3.0 正式發佈了,其中有一個重要特性:原生支持 SQL 了,不需要任何外部其他的庫、處理方式等等,開箱即用。

REST 接口、命令行、JDBC 各種方式都支持使用 SQL 進行查詢和聚合操作,Elasticsearch SQL 就像一個翻譯器,它即懂得 SQL 也懂 Elasticsearch,幫助我們方便的使用 SQL 來實時處理 ES 的數據,可讀性非常好,又可以很好的發揮 ES 的能力。

2. 示例

(1)插入測試數據

curl -X PUT "localhost:9200/library/book/_bulk?refresh" -H 'Content-Type: application/json' -d'
{"index":{"_id": "Leviathan Wakes"}}
{"name": "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561}
{"index":{"_id": "Hyperion"}}
{"name": "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482}
{"index":{"_id": "Dune"}}
{"name": "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604}
'

(2)使用 SQL 查詢所有數據

Elasticsearch SQL

Elasticsearch SQL

(3)order by 和 limit

Elasticsearch SQL

Elasticsearch SQL

(4)SQL 和 ES filter 聯合使用

Elasticsearch SQL

(5)把 SQL 轉成 ES 查詢方式

Elasticsearch SQL

結果

Elasticsearch SQL

(6)使用 SQL 客戶端

登錄 sql-cli

bin/elasticsearch-sql-cli

執行 sql

Elasticsearch SQL

Elasticsearch SQL

3. 小結

SQL 是開發人員非常熟悉的語言,新版 ES 原生支持 SQL 後會給我們帶來了極大的便利,有興趣的同學可以體驗一下。

安裝文檔:

https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html

Elasticsearch SQL 文檔:

https://www.elastic.co/guide/en/elasticsearch/reference/current/xpack-sql.html


分享到:


相關文章: