Elasticsearch 6.3 釋出,你們要的 SQL 功能來了
本文原文:https://www.iteblog.com/archives/2378.html(點選下面 閱讀原文 即可進入)
Elasticsearch 6.3 於前天正式釋出,其中帶來了很多新特性,詳情請參見:https://www.elastic.co/blog/elasticsearch-6-3-0-released。這個版本最大的亮點莫過於內建支援 SQL 模組!我在早些時間就說過 Elasticsearch 將會內建支援 SQL,參見:ElasticSearch內建也將支援SQL特性。我們可以像操作 MySQL一樣使用 Elasticsearch,這樣我們就可以減少 DSL 的學習成本,這個 SQL 模組是屬於 X-Pack 的一部分。Elasticsearch SQL 主要有以下幾個特點:
允許我們在 Elasticsearch 使用 SQL 查詢其中的資料;
支援 REST 、 JDBC 以及命令列來來下資料,任何客戶端都可以使用 SQL 在 Elasticsearch 中本地搜尋和聚合資料;
內部應該是將 SQL 翻譯成 DSL 來查詢資料的
本文將簡單介紹如何在 Elasticsearch 中使用 SQL。
安裝
在使用之前,我們需要先安裝 Elasticsearch 6.3,因為我這只是測試,所以安裝過程非常簡單。步驟如下:
iteblog$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.0 .zip
iteblog$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.0.zip.sha512
iteblog$ shasum -a 512 -c elasticsearch-6.3.0.zip.sha512
iteblog$ unzip elasticsearch-6.3.0.zip
iteblog$ cd elasticsearch-6.3.0/
iteblog$ ./bin/elasticsearch
經過上面幾步,我們就在伺服器上簡單地部署好了 Elasticsearch 6.3。我們可以訪問 ip:9200 頁面來確定我們的 Elasticsearch 6.3 是否正常執行:
如果想及時瞭解Spark、Hadoop或者Hbase相關的文章,歡迎關注微信公共帳號:iteblog_hadoop
在使用 Elasticsearch SQL 之前,我們先通過下面命令往 Elasticsearch 導一些資料:
curl -X PUT "www.iteblog.com: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}
> '
[返回結果]
{"took":719,"errors":false,"items":[{"index":{"_index":"library","_type":"book","_id":"Leviathan Wakes","_version":1,"result":"created","forced_refresh":true,"_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}},{"index":{"_index":"library","_type":"book","_id":"Hyperion","_version":1,"result":"created","forced_refresh":true,"_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}},{"index":{"_index":"library","_type":"book","_id":"Dune","_version":1,"result":"created","forced_refresh":true,"_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1,"status":201}}]}
SQL REST API
curl -X POST "www.iteblog.com:9200/_xpack/sql?format=txt" -H 'Content-Type: application/json' -d'
{
"query": "SELECT * FROM library ORDER BY page_count DESC LIMIT 5"
}
'
返回結果
author | name | page_count | release_date
----------------+---------------+---------------+------------------------
Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
James S.A. Corey|Leviathan Wakes|561 |2011-06-02T00:00:00.000Z
Dan Simmons |Hyperion |482 |1989-05-26T00:00:00.000Z
上面通過 format=txt 指定以文字的形式返回結果,這種形式對我們人來說看起來很舒服,但是對計算機來說很不友好,所以我們可以指定返回資料的格式:
curl -X POST "l-qdws2.tc.cn8:9200/_xpack/sql?format=json" -H 'Content-Type: application/json' -d'
{
"query": "SELECT * FROM library ORDER BY page_count DESC LIMIT 5"
}
'
返回結果
{
"columns": [
{
"name": "author",
"type": "text"
},
{
"name": "name",
"type": "text"
},
{
"name": "page_count",
"type": "long"
},
{
"name": "release_date",
"type": "date"
}
],
"rows": [
[
"Frank Herbert",
"Dune",
604,
"1965-06-01T00:00:00.000Z"
],
[
"James S.A. Corey",
"Leviathan Wakes",
561,
"2011-06-02T00:00:00.000Z"
],
[
"Dan Simmons",
"Hyperion",
482,
"1989-05-26T00:00:00.000Z"
]
]
}
其他的格式支援包括:yaml、smile、cbor 、txt、csv、tsv等等,我們可以通過 format 引數指定。
SQL Translate API
ElasticSearch 提供了 SQL Translate API 介面,我們可以通過這個介面檢視 ElasticSearch 如何將我們的 SQL 翻譯成 DSL:
curl -X POST "l-qdws2.tc.cn8:9200/_xpack/sql/translate" -H 'Content-Type: application/json' -d'
{
"query": "SELECT * FROM library ORDER BY page_count DESC",
"fetch_size": 10
}
'
返回結果
{
"size": 10,
"_source": {
"includes": [
"author",
"name"
],
"excludes": [ ]
},
"docvalue_fields": [
"page_count",
"release_date"
],
"sort": [
{
"page_count": {
"order": "desc"
}
}
]
}
SQL CLI
ElasticSearch 還為我們提供了一個 CLI,我們可以通過下面命令啟動並查詢資料:
./bin/elasticsearch-sql-cli l-qdws2.tc.cn8:9200
如果想及時瞭解Spark、Hadoop或者Hbase相關的文章,歡迎關注微信公共帳號:iteblog_hadoop
SQL JDBC
當然,我們還可以在程式裡面通過 JDBC 連線 ElasticSearch 來查詢裡面的資料:
String address = "jdbc:es://" + elasticsearchAddress;
Properties connectionProperties = connectionProperties();
Connection connection = DriverManager.getConnection(address, connectionProperties);
try (Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery(
"SELECT name, page_count FROM library ORDER BY page_count DESC LIMIT 1")) {
assertTrue(results.next());
assertEquals("Don Quixote", results.getString(1));
assertEquals(1072, results.getInt(2));
SQLException e = expectThrows(SQLException.class, () -> results.getInt(1));
assertTrue(e.getMessage(), e.getMessage().contains("unable to convert column 1 to an int"));
assertFalse(results.next());
}
關於 ElasticSearch SQL 的更多資訊,請參見官方文件:https://www.elastic.co/guide/en/elasticsearch/reference/current/xpack-sql.html
猜你喜歡歡迎關注本公眾號:iteblog_hadoop:
0、回覆 電子書獲取 本站所有可下載的電子書
11、更多大資料文章歡迎訪問https://www.iteblog.com及本公眾號(iteblog_hadoop)12、Flink中文文件:http://flink.iteblog.com13、Carbondata 中文文件:http://carbondata.iteblog.com