Elasticsearch 6.3 X-PACK SQL Overview

Elasticsearch 6.3 X-PACK SQL Overview

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

写在前面

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 suprise, though as all things in the world, everything is relative.

Elasticsearch终究是Elasticsearch,不能够完全兼容SQL。开发者们尽量准守principle of least suprise(最小惊吓原则)来开发Elasticsearch SQL。

Overview

X-PACK SQL支持以REST API、SQL CLI客户端以及JDBC的形式连接Elasticsearch。

Mapping concepts across SQL and Elasticsearch

SQLElasticsearch
columnfield
rowdocument
tableindex
schemaimplicit
databasecluster

SQL REST API

支持txt,json,yaml,smile,cbor,csv

  1. Format is txt

    POST /_xpack/sql?format=txt
    {
      "query": "SELECT date, domain FROM sinadpool_nginx* LIMIT 5"
    }
              date          |           domain            
    ------------------------+-----------------------------
    2018-08-02T00:00:00.000Z|wgw.city.sina.com.cn
    2018-08-02T00:00:00.000Z|php.weather.sina.com.cn
    2018-08-02T00:00:00.000Z|vip.stock.finance.sina.com.cn
    2018-08-02T00:00:00.000Z|guba.sina.com.cn
    2018-08-02T00:00:00.000Z|i.search.sina.com.cn
  2. Format is json

    POST /_xpack/sql
    {
      "query": "SELECT * FROM sinadpool_nginx* LIMIT 1"
    }
    {
      "columns": [
        {
          "name": "date",
          "type": "date"
        },
        {
          "name": "domain",
          "type": "keyword"
        }
      ],
      "rows": [
        [
          "2018-08-02T00:00:00.000Z",
          "stock2.finance.sina.com.cn"
        ]
      ]
    }

SQL Translate API

将SQL语句转换为Elasticsearch查询语法

POST /_xpack/sql/translate
{
    "query": "SELECT * FROM library ORDER BY page_count DESC",
    "fetch_size": 10
}

SQL CLI

$ ./bin/elasticsearch-sql-cli https://some.server:9200

SQL JDBC

这部分未进一步探索,因为这个功能是收费的

<dependency>
  <groupId>org.elasticsearch.plugin</groupId>
  <artifactId>jdbc</artifactId>
  <version>6.3.2</version>
</dependency>

存在的问题

  1. SQL不支持’-‘字符,需要转义

    POST /_xpack/sql?format=txt
    {
    "query": "SELECT date, domain FROM \"sinadpool_nginx-2018.08.02\" LIMIT 5"
    }
  2. SQL Translate API 不支持索引不存在的查询条件

    这里写图片描述

  3. 暂不支持JOIN

  4. 对较为复杂的SQL解析有问题
    以下结果一致,且与预期查询结果不一致

    POST /_xpack/sql/translate
    {
      "query": "select * from (select domain, count(1) as C from sinadpool_nginx* where date='2018-08-01' group by domain) a order by C LIMIT 10"
    }
    POST /_xpack/sql/translate
    {
      "query": "select domain, count(1) as C from sinadpool_nginx* where date='2018-08-01' group by domain LIMIT 10"
    }

Reference

SQL Access

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值