Elasticsearch SQL查询 --- 2022-04-03
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch SQL查询 --- 2022-04-03相关的知识,希望对你有一定的参考价值。
参考技术A ES SQL查询的用法跟mysql基本一样,熟悉MYSQL的同学,可以直接通过SQL查询ES数据。SQL语法结构跟MYSQL类似都是SQL标准语法结构,区别就是SQL标准的支持程度。
SQL例子
都是标准的SQL写法。
查询ES索引的字段和类型
语法:
例子:
输出:
将ES中所有的索引都列出来
例子:
输出:
展示ES支持的SQL函数有哪些
例子:
输出:
Elasticsearch之使用RestClient实现_sql查询
版本:elasticsearch 7.13.4
1. 声明
当前内容主要为使用RestClient以及使用sql方式进行查询操作,主要参考官方文档
主要使用_sql方式进行查询
2. 主要demo
public static void main(String[] args) throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
// Sniffer 默认为5分钟一次更新节点信息(主要为控制节点,判断节点是否可以访问)
// sniffer的主要作用就是按照指定的时间间隔方式修改restClient的setNodes方法
Sniffer sniffer = Sniffer.builder(restClient).build();
// 手动设置为1分钟更新一次节点
// Sniffer.builder(restClient).setSniffIntervalMillis(60*1000);
try {
// 使用sql方式查询
selectDataUsingNativeSql(restClient);
} finally {
// 注意关闭顺序
sniffer.close();
restClient.close();
}
}
/**
*
* @author hy
* @createTime 2021-08-01 12:38:57
* @description 使用原生的sql查询操作
* @param restClient
* @throws IOException
*
*/
private static void selectDataUsingNativeSql(RestClient restClient) throws IOException {
// Request request = new Request("GET", "/_sql?format=txt"); // 默认按照视图的方式显示
/*
bookName | count | id | price | publishDate
---------------+---------------+---------------+---------------+---------------
null |null |2 |66.6 |null
*/
Request request = new Request("GET", "/_sql");// 默认format就是json
// {"columns":[{"name":"bookName","type":"text"},{"name":"count","type":"long"},{"name":"id","type":"long"},{"name":"price","type":"float"},{"name":"publishDate","type":"text"}],"rows":[[null,null,2,66.6,null]]}
// request.setJsonEntity("{\\"query\\": \\"SELECT * FROM book WHERE bookName is null and price>5\\"}");
// 但是不可以在query中使用limit方式实现分页操作入:limit 0,5
// 不支持分页查询操作,只能控制当前查询的数量,具有缺点(官方显示有很多缺陷)
request.setJsonEntity("{\\"query\\": \\"SELECT * FROM book WHERE bookName is null and price>5 LIMIT 5\\"}");
Response response = restClient.performRequest(request);
System.out.println(response);
String result = getResponseContent(response);
System.out.println(result);
}
/**
*
* @author hy
* @createTime 2021-07-31 13:51:11
* @description 获取带有返回值的响应数据,例如select查询操作
* @param response
* @return
* @throws UnsupportedOperationException
* @throws IOException
*
*/
private static String getResponseContent(Response response) throws UnsupportedOperationException, IOException {
if (response == null) {
return null;
}
HttpEntity entity = response.getEntity();
StringBuilder builder = new StringBuilder();
if (entity != null) {
InputStream content = entity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
builder.append("\\n");
}
}
return builder.toString();
}
3.查询结果
format表示返回的格式,默认就是json
八月 01, 2021 1:18:43 下午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/_nodes/http?timeout=1000ms] returned 1 warnings: [299 Elasticsearch-7.13.4-c5f60e894ca0c61cdbae4f5a686d9f08bcefc942 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html to enable security."]
八月 01, 2021 1:18:43 下午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/_sql] returned 1 warnings: [299 Elasticsearch-7.13.4-c5f60e894ca0c61cdbae4f5a686d9f08bcefc942 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html to enable security."]
Response{requestLine=GET /_sql HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}
{"columns":[{"name":"bookName","type":"text"},{"name":"count","type":"long"},{"name":"id","type":"long"},{"name":"price","type":"float"},{"name":"publishDate","type":"text"}],"rows":[[null,null,2,66.6,null]]}
查询成功
3. 缺点
1.不能使用分页查询,这个导致有点尴尬
,而且具有多个限制官方描述限制
2.而且还可以支持其他的query?,感觉有点跑偏了,果然还是原生的查询好
以上是关于Elasticsearch SQL查询 --- 2022-04-03的主要内容,如果未能解决你的问题,请参考以下文章
2021年大数据ELK(十六):Elasticsearch SQL(职位查询案例)