用6.2.3.0版本的elasticsearch-sql怎么写
时间: 2024-03-05 18:48:08 浏览: 154
在Elasticsearch 6.2.3.0版本中,使用elasticsearch-sql插件执行SQL查询需要使用类似以下的语法:
```
POST _xpack/sql?format=txt
{
"query": "SELECT DATE_FORMAT(t1.createTime, '%Y%m%d%H') AS monTime, IFNULL(t1.monValue,0) AS monValue FROM device_monitor_attr_his t1 INNER JOIN (SELECT MAX(id) id FROM device_monitor_attr_his t where deviceID=27143 AND monKey=107 AND createTime > '2023-03-03' AND createTime < '2023-06-02 23:59:59' GROUP BY DATE_FORMAT(createTime,'%Y%m%d') ) t2 on t2.id = t1.id"
}
```
其中,`_xpack/sql`是执行SQL查询的API,`format=txt`用于指定返回结果的格式为文本。在请求体中,`query`参数指定要执行的SQL查询语句。需要注意的是,在Elasticsearch 6.2.3.0版本中,elasticsearch-sql插件需要安装和启用_xpack插件。
相关问题
SELECT domain_name,http_port,DATE_FORMAT(happenTime, '%Y-%m-%d'),SUM(ll) / (1024 * 1024) AS ll, SUM(llsd) / (1024 * 1024) AS llsd, SUM(cksj) / (1024 * 1024) AS cksj FROM video_traffic_record t group by domain_name,http_port,DATE_FORMAT(happenTime, '%Y-%m-%d')用elasticsearch-sql6.2.3.0怎么写?
在 Elasticsearch 6.2.3 版本中,你可以使用以下查询语句来实现上述 SQL 查询:
POST _xpack/sql?format=txt
{
"query": "SELECT domain_name, http_port, DATE_FORMAT(happenTime, 'yyyy-MM-dd'), SUM(ll)/(1024*1024) AS ll, SUM(llsd)/(1024*1024) AS llsd, SUM(cksj)/(1024*1024) AS cksj FROM \"video_traffic_record\" GROUP BY domain_name, http_port, DATE_FORMAT(happenTime, 'yyyy-MM-dd')"
}
注意,这里使用的是 Elasticsearch 内置的 SQL 查询语言,需要在请求 URL 中添加 `_xpack/sql`。查询结果将会以文本格式返回。
SELECT domain_name, http_port, DATE_FORMAT(FROM_UNIXTIME(TIME), '%Y-%m-%d') AS day, COUNT(*) AS count, SUM(ll) / (1024 * 1024) AS ll, SUM(llsd) / (1024 * 1024) AS llsd, SUM(cksj) / (1024 * 1024) AS cksj FROM video_traffic_record GROUP BY domain_name, http_port, day ORDER BY day DESC LIMIT 10 OFFSET 2如何转换成elasticsearch-sql6.2.3.0?
如果您使用的是 Elasticsearch-sql6.2.3.0 插件,可以使用类似 SQL 的查询语句来查询 Elasticsearch 数据。
以下是与原始查询语句相对应的 Elasticsearch-sql6.2.3.0 查询语句:
```
POST /_sql?format=txt
{
"query": "SELECT domain_name, http_port, DATE_FORMAT(FROM_UNIXTIME(TIME), '%Y-%m-%d') AS day, COUNT(*) AS count, SUM(ll) / (1024 * 1024) AS ll, SUM(llsd) / (1024 * 1024) AS llsd, SUM(cksj) / (1024 * 1024) AS cksj FROM video_traffic_record GROUP BY domain_name, http_port, day ORDER BY day DESC LIMIT 2,10"
}
```
注意,Elasticsearch-sql6.2.3.0 支持类似 SQL 的查询语句,但是并不支持所有的 SQL 语法,因此有些查询语句可能无法转换为 Elasticsearch-sql6.2.3.0 查询语句。另外,Elasticsearch-sql6.2.3.0 插件已经停止维护,建议使用更新的 Elasticsearch 版本自带的 SQL 查询语法。
阅读全文