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
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.
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
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.
持续更新中…欢迎关注