数据库
首页 > 数据库> > Elasticsearch之使用RestClient实现_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?,感觉有点跑偏了,果然还是原生的查询好

标签:RestClient,restClient,查询,Elasticsearch,sql,null,type,name
来源: https://blog.csdn.net/weixin_45492007/article/details/119297148