[Elasticsearch]怎麼使用Elasticsearch SQL

1.概述

Overviewhtml

Elasticsearch SQL提供了功能強大又輕量的可使用SQL與Elasticsearch交互的方式.node

Elasticsearch SQL aims to provide a powerful yet lightweight SQL interface to Elasticsearch.sql

簡介

Introductionapp

Elasticsearch SQL是屬於X-Pack提供的功能組件,可讓咱們近實時的對Elasticsearch執行相似SQL的查詢.不管是使用REST API仍是使用的命令行、JDBC或者其它客戶端連接的Elasticsearch,均可以使用SQL執行搜索統計操做,就像Elasticsearch原生就支持SQL同樣.能夠把Elasticsearch SQL理解爲一個翻譯器,就是它能夠把你寫的SQL翻譯爲Elasticsearch的查詢語言去執行.這樣不管你是熟悉SQL仍是熟悉Elasticsearch的查詢語言均可以使用Elasticsearch近實時的讀取和處理數據.elasticsearch

Elasticsearch SQL is an X-Pack component that allows SQL-like queries to be executed in real-time against Elasticsearch. Whether using the REST interface, command-line or JDBC, any client can use SQL to search and aggregate data natively inside Elasticsearch. One can think of Elasticsearch SQL as a translator, one that understands both SQL and Elasticsearch and makes it easy to read and process data in real-time, at scale by leveraging Elasticsearch capabilities.ide

爲何要使用Elasticsearch SQL?

Why Elasticsearch SQL?工具

原生集成

Native integration性能

Elasticsearch SQL是爲Elasticsearch特別定製的,每一個查詢均可以根據底層數據存儲方式位置作出相應優化從而提升查詢性能.優化

Elasticsearch SQL is built from the ground up for Elasticsearch. Each and every query is efficiently executed against the relevant nodes according to the underlying storage.ui

不須要額外投入

No external parts

使用Elasticsearch SQL不須要額外添加硬件、軟件、特別處理,由於Ealsticsearch SQL是運行在集羣內部的.

No need for additional hardware, processes, runtimes or libraries to query Elasticsearch; Elasticsearch SQL eliminates extra moving parts by running inside the Elasticsearch cluster.

輕量高性能

Lightweight and efficient

Elasticsearch SQL並非對Elasticsearch和它查詢功能的二次封裝抽象,相反Elasticsearch也是很熱情的去支持使用SQL以一樣聲明性、簡潔的方式進行近實時的全文檢索.

Elasticsearch SQL does not abstract Elasticsearch and its search capabilities - on the contrary, it embraces and exposes SQL to allow proper full-text search, in real-time, in the same declarative, succinct fashion.

2.開始使用SQL

Getting Started with SQL

爲了嘗試使用SQL,新建一個索引並添加點數據用於演示:

To start using Elasticsearch SQL, create an index with some data to experiment with:

PUT /library/book/_bulk?refresh{"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}1234567

建立好演示數據就可使用REST API進行SQL查詢了, 就像這樣:

And now you can execute SQL using the SQL REST API right away:

POST /_sql?format=txt{
    "query": "SELECT * FROM library WHERE release_date < '2000-01-01'"}1234

查詢返回的結果大概是這個樣子地:

Which should return something along the lines of:

 author     |     name      |  page_count   | release_date
---------------+---------------+---------------+------------------------
Dan Simmons    |Hyperion       |482            |1989-05-26T00:00:00.000Z
Frank Herbert  |Dune           |604            |1965-06-01T00:00:00.000Z1234

也可使用SQL命令行工具進行SQL查詢,能夠在x-pack的bin目錄中找到並啓動它:

You can also use the SQL CLI. There is a script to start it shipped in x-pack’s bin directory:

$ ./bin/elasticsearch-sql-cli1

啓動後,能夠像這樣執行跟上面同樣的SQL:

From there you can run the same query:

sql> SELECT * FROM library WHERE release_date < '2000-01-01';
    author     |     name      |  page_count   | release_date
---------------+---------------+---------------+------------------------
Dan Simmons    |Hyperion       |482            |1989-05-26T00:00:00.000Z
Frank Herbert  |Dune           |604            |1965-06-01T00:00:00.000Z12345

3.約定和術語

Conventions and Terminology

理解某些詞真正表達的意思有助於更好的理解文檔內容,對有些詞你們理解的意思可能不同,這主要取決於咱們的知識背景不一樣對SQL、Elasticsearch的熟悉程度不一樣.

For clarity, it is important to establish the meaning behind certain words as, the same wording might convey different meanings to different readers depending on one’s familiarity with SQL versus Elasticsearch.

通常來講,Elasticsearch SQL就像它的名字同樣是爲Elasticsearch提供SQL交互支持,因此會優先使用SQL的術語和約定,但畢竟底層的支持引擎是Elasticsearch不免有些地方不能很好的兼容,可能會出現一些特定的術語。這也沒辦法不是,畢竟須要有所取捨,但咱們會盡可能少的引入新的術語和約定.

As a general rule, Elasticsearch SQL as the name indicates provides a SQL interface to Elasticsearch. As such, it follows the SQL terminology and conventions first, whenever possible. However the backing engine itself is Elasticsearch for which Elasticsearch SQL was purposely created hence why features or concepts that are not available, or cannot be mapped correctly, in SQL appear in Elasticsearch SQL. Last but not least, Elasticsearch SQL tries to obey the principle of least surprise, though as all things in the world, everything is relative.

持續更新中…歡迎關注

相關文章
相關標籤/搜索