123

Elasticsearch SQL

 5 years ago
source link: http://www.10tiao.com/html/496/201806/2652402834/1.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1. 简介

Elasticsearch 6.3.0 正式发布了,其中有一个重要特性:原生支持 SQL 了,不需要任何外部其他的库、处理方式等等,开箱即用。

REST 接口、命令行、JDBC 各种方式都支持使用 SQL 进行查询和聚合操作,Elasticsearch SQL 就像一个翻译器,它即懂得 SQL 也懂 Elasticsearch,帮助我们方便的使用 SQL 来实时处理 ES 的数据,可读性非常好,又可以很好的发挥 ES 的能力。

2. 示例

(1)插入测试数据

  1. curl -X PUT "localhost:9200/library/book/_bulk?refresh" \

  2.     -H 'Content-Type: application/json'-d'

  3. {"index":{"_id": "Leviathan Wakes"}}

  4. {"name": "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561}

  5. {"index":{"_id": "Hyperion"}}

  6. {"name": "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482}

  7. {"index":{"_id": "Dune"}}

  8. {"name": "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604}

  9. '

(2)使用 SQL 查询所有数据



(3)order by 和 limit



(4)SQL 和 ES filter 联合使用


(5)把 SQL 转成 ES 查询方式


结果

(6)使用 SQL 客户端

登录 sql-cli

bin/elasticsearch-sql-cli

执行 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


点击“阅读原文”查看文章列表


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK