介绍
Java高级别Rest客户端(Java High Level REST Client)简称高级客户端,其内部仍然是基于低级客户端。提供了更多的API,接受请求对象作为参数并返回响应对象,由客户端自己处理编码和解码。API支持同步或异步调用。
兼容性
高级客户端需要Java 1.8并依赖于Elasticsearch core项目。客户端版本需要与Elasticsearch版本相同。
若要与Elasticsearch进行通信,主版本号必须一致,次版本号不必相同,因为是向前兼容的。次版本号小于等于Elasticsearch的都可以,意味着它支持与更高版本的Elasticsearch通信。例如:6.1客户端肯定能够与6.1,6.2和任何后来的6.x版本进行通信,但与旧版本的Elasticsearch节点通信时可能会存在不兼容的问题。
高级客户端与Elasticsearch的发布周期相同(优点)
pom.xml
导入ES、Java High Level Rest Client依赖
<!--elasticsearch-rest-high-level-client-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
Java High Level REST Client的配置文件
ESConfig.java
package com.test.testmanagement.config;//package com.test.config;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestClientBuilder.RequestConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
@Configuration
public class ESConfig {
private static String hosts = "10.237.79.147"; //集群地址,多个用,分隔
private static int port = 9200; //使用的端口号
private static String schema = "http"; //使用的协议
private static ArrayList hostList = null;
private static int connectTimeOut = 1000;//连接超时时间
private static int socketTimeOut = 30000;//??
private static int connectionRequestTimeOut = 500;//获取连接的超时时间
private static int maxConnectNum = 100;//最大连接数
private static int maxConnectPerRoute = 100;//最大路由连接数
static {
hostList = new ArrayList<>();
String[] hostStrs = hosts.split(",");
for (String host:hostStrs){
hostList.add(new HttpHost(host,port,schema));
}
}
@Bean
public RestHighLevelClient client(){
RestClientBuilder builder = RestClient.builder((HttpHost[])hostList.toArray(new HttpHost[0]));
//异步httpclient连接延时配置
builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public Builder customizeRequestConfig(Builder requestConfigBuilder) {
requestConfigBuilder.setConnectTimeout(connectTimeOut);
requestConfigBuilder.setSocketTimeout(socketTimeOut);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
return requestConfigBuilder;
}
});
//异步httpclient连接数设置
builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
return httpClientBuilder;
}
});
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
相关接口举例
public SearchHit[] esHighLevelClientSearchApiDevops(String searchTaskName, String env, String rpid, String result) {
System.out.println("任务名111:" + searchTaskName);
//第一部分
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.fetchSource(new String[]{"log_case", "log_task_name", "log_module", "log_rpid","log_rname","log_env","log_result"}, new String[]{});
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("log_task_name", searchTaskName);
// 下面多条件查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (env != "" && env != null) {
MatchQueryBuilder matchQueryBuilder0 = QueryBuilders.matchQuery("log_env", env);
boolQueryBuilder.must(matchQueryBuilder0);
}
if (rpid != "" && rpid != null) {
MatchQueryBuilder matchQueryBuilder1 = QueryBuilders.matchQuery("log_rpid", rpid);
boolQueryBuilder.must(matchQueryBuilder1);
}
if (result != "" && result != null) {
MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery("log_result", result);
boolQueryBuilder.must(matchQueryBuilder2);
}
boolQueryBuilder.must(matchQueryBuilder);
sourceBuilder.query(boolQueryBuilder);
sourceBuilder.size(10000); //设置确定搜素命中返回数的size选项,默认为10
//第二部分
//log-2019.11.24 数据库,
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(sourceBuilder);
try {
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
SearchHit[] searchHits = hits.getHits();
System.out.println("searchHits数组长度:" + searchHits.length);
return searchHits;
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("这句话不应该显示!");
return null;
}
///
/**
* devops查日志用这个 2020-03-12
* @param searchTaskName
* @param env
* @param interfaceType
* @param result
* @return
*/
@RequestMapping(value = "/es/devopsSearch", method = RequestMethod.GET)
public Map<String, Object> devopsSearch(@RequestParam(value = "searchTaskName") String searchTaskName,
@RequestParam(value = "env", required = false) String env,
@RequestParam(value = "interfaceType", required = false) String interfaceType,
@RequestParam(value = "rpid", required = false) String rpid,
@RequestParam(value = "result", required = false) String result) {
Map<String, Object> returnMap = new HashMap<>();
List<Map<String,Object>> serviceMap = new ArrayList<>();
//调用esHighLevelClientSearchApi
SearchHit[] searchHits = null;
if (rpid !="" && rpid != null && (result =="" || result == null)) {
// 这里单独查某个用例的结果
searchHits = esHighLevelClientSearchApiDevops(searchTaskName, env, rpid, "");
System.out.println("searchHits:" + searchHits + " " + "length:" + searchHits.length);
} else {
searchHits = esHighLevelClientSearchApiDevops(searchTaskName, env, rpid, result);
System.out.println("searchHits:" + searchHits + " " + "length:" + searchHits.length);
}
for (SearchHit hit : searchHits) {
System.out.println("search -> " + hit.getSourceAsString());
JSONObject totalJson = JSONObject.parseObject(hit.getSourceAsString());
String caseObjStr = (String) totalJson.get("log_case");
String module = (String) totalJson.get("log_module");
String rpid_rpver = (String) totalJson.get("log_rpid");
String rname = (String) totalJson.get("log_rname");
if (caseObjStr != null) {
//调用
Map caseMap = JSON.parseObject(caseObjStr, Map.class);
if (interfaceType.equals("Gateway") && caseMap.get("interfaceType").equals("Gateway")) {
boolean isAdd = false;
for (int i = 0; i < serviceMap.size(); i++) {
Map tableInfo = serviceMap.get(i);
if (tableInfo.get("rpidNum").equals(rpid_rpver)) {
((List<String>) tableInfo.get("case")).add(caseObjStr);
serviceMap.set(i, tableInfo);
isAdd = true;
}
}
if (isAdd == false) {
Map tableInfo = new HashMap();
tableInfo.put("rpidNum",rpid_rpver);
tableInfo.put("module",module);
tableInfo.put("name",rname);
List<String> caseInfo = new ArrayList<>();
caseInfo.add(caseObjStr);
tableInfo.put("case",caseInfo);
serviceMap.add(tableInfo);
}
}
}
}
returnMap.put("code", 0);
returnMap.put("msg", "msg");
returnMap.put("count", serviceMap.size());
returnMap.put("data", serviceMap);
return returnMap;
}