Elasticsearch系列(七)----JAVA客户端之RestClient操作详解
2017-09-05 17:08
537 查看
elasticsearch 5.0引入了一个新的客户端 RestClient ,使用HTTP API elasticsearch代替内部协议, RestClient 初始化方法是线程安全的,最理想的客户端生命周期是与应用相同,在应用停止服务之前应该关闭客户端链接,释放资源。
RestClient完整示例如下:
完整示例: http://download.csdn.net/download/u011781521/9965339
初始化客户端 RestClient restClient = RestClient.builder( new HttpHost("xxxx", 9800, "http"), new HttpHost("xxxx", 9800, "http")).build(); 关闭客户端连接 restClient.close();
RestClient完整示例如下:
package com.fendo.RestClient; import java.io.IOException; import java.util.Collections; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.junit.Before; import org.junit.Test; /** * Elasticserach RestClient示例 * @author fendo * */ public class Rest { private static RestClient restClient; public void getRestClient(){ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme")); restClient = RestClient.builder(new HttpHost("localhost",9200,"http")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }).build(); } @Before public void getRest(){ restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build(); } /** * 查看api信息 * @throws Exception */ @Test public void CatApi() throws Exception{ String method = "GET"; String endpoint = "/_cat"; Response response = restClient.performRequest(method,endpoint); System.out.println(EntityUtils.toString(response.getEntity())); } /** * 创建索引 * @throws Exception */ @Test public void CreateIndex() throws Exception{ String method = "PUT"; String endpoint = "/test-index"; Response response = restClient.performRequest(method,endpoint); System.out.println(EntityUtils.toString(response.getEntity())); } /** * 创建文档 * @throws Exception */ @Test public void CreateDocument()throws Exception{ String method = "PUT"; String endpoint = "/test-index/test/1"; HttpEntity entity = new NStringEntity( "{\n" + " \"user\" : \"kimchy\",\n" + " \"post_date\" : \"2009-11-15T14:12:12\",\n" + " \"message\" : \"trying out Elasticsearch\"\n" + "}", ContentType.APPLICATION_JSON); Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity); System.out.println(EntityUtils.toString(response.getEntity())); } /** * 获取文档 * @throws Exception */ @Test public void getDocument()throws Exception{ String method = "GET"; String endpoint = "/test-index/test/1"; Response response = restClient.performRequest(method,endpoint); System.out.println(EntityUtils.toString(response.getEntity())); } /** * 查询所有数据 * @throws Exception */ @Test public void QueryAll() throws Exception { String method = "POST"; String endpoint = "/test-index/test/_search"; HttpEntity entity = new NStringEntity("{\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " }\n" + "}", ContentType.APPLICATION_JSON); Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity); System.out.println(EntityUtils.toString(response.getEntity())); } /** * 根据ID获取 * @throws Exception */ @Test public void QueryByField() throws Exception { String method = "POST"; String endpoint = "/test-index/test/_search"; HttpEntity entity = new NStringEntity("{\n" + " \"query\": {\n" + " \"match\": {\n" + " \"user\": \"kimchy\"\n" + " }\n" + " }\n" + "}", ContentType.APPLICATION_JSON); Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity); System.out.println(EntityUtils.toString(response.getEntity())); } /** * 更新数据 * @throws Exception */ @Test public void UpdateByScript() throws Exception { String method = "POST"; String endpoint = "/test-index/test/1/_update"; HttpEntity entity = new NStringEntity("{\n" + " \"doc\": {\n" + " \"user\":\"大美女\"\n" + " }\n" + "}", ContentType.APPLICATION_JSON); Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity); System.out.println(EntityUtils.toString(response.getEntity())); } @Test public void GeoBoundingBox() throws IOException { String method = "POST"; String endpoint = "/attractions/restaurant/_search"; HttpEntity entity = new NStringEntity("{\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " },\n" + " \"post_filter\": {\n" + " \"geo_bounding_box\": {\n" + " \"location\": {\n" + " \"top_left\": {\n" + " \"lat\": 39.990481,\n" + " \"lon\": 116.277144\n" + " },\n" + " \"bottom_right\": {\n" + " \"lat\": 39.927323,\n" + " \"lon\": 116.405638\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}", ContentType.APPLICATION_JSON); Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity); System.out.println(EntityUtils.toString(response.getEntity())); } }
完整示例: http://download.csdn.net/download/u011781521/9965339
相关文章推荐
- Elasticsearch系列(五)----JAVA客户端之TransportClient操作详解
- Elasticsearch系列(六)----JAVA客户端之Jest操作详解
- Elasticsearch【JAVA REST Client】客户端操作
- elasticsearch 5.0 获取 TransportClient 操作客户端java API
- elasticsearch 5.0 获取 TransportClient 操作客户端java API
- 使用Java Rest Client操作Elasticsearch
- 使用Java Rest Client操作Elasticsearch
- ElasticSearch 2.4.0系列之二 java 创建client客户端
- Elasticsearch简单使用系列--使用Rest API进行CRUD操作
- 使用java客户端操作elasticsearch
- elasticsearch java客户端 - 原生esClient
- elasticsearch JAVA客户端操作---搜索的过滤、分组高亮
- JavaWEB--POI之EXCEL操作、优化、封装详解系列(三)--万能POI之EXCEL导出工具--PoiExportUtil入门篇
- Elasticsearch简单使用系列--使用Rest API进行CRUD操作
- SSO单点登录系列1:cas客户端源码分析cas-client-java-2.1.1.jar
- JavaWEB--POI之EXCEL操作、优化、封装详解系列(四)--PoiExportUtil架构设计篇与功能说明
- SSO单点登录系列1:cas客户端源码分析cas-client-java-2.1.1.jar
- elasticsearch 口水篇(4)java客户端 - 原生esClient
- JavaWEB--POI之EXCEL操作、优化、封装详解系列(一)--概述与原理
- elasticsearch JAVA客户端操作---索引的CURD