截止7.15版本TransportClient tcp长连接方式,HighLevelRestClient Http rest连接方式官网已淘汰。
使用最新 ElasticsearchClient连接 *** 作ES完整教程: 1、添加依赖2、连接es集群(根据情况是否设置账号和密码,我这边是需要的)co.elastic.clients elasticsearch-java7.16.2 com.fasterxml.jackson.core jackson-databind2.12.3
package com.alcatel.asb.hdm.report.device.util; import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.json.jackson.JacksonJsonpMapper; import co.elastic.clients.transport.ElasticsearchTransport; import co.elastic.clients.transport.rest_client.RestClientTransport; 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.impl.client.BasicCredentialsProvider; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import java.util.Arrays; public class EsConfig { private String clusterNodes;//es集群节点 例://192.168.1.96:9200,192.168.1.97:9200,192.168.1.98:9200 private String account;//账号 例:elastic private String passWord;//密码 例:123456 public static ElasticsearchClient client; //http集群 public void esClient(){ HttpHost[] httpHosts = Arrays.stream(clusterNodes.split(",")).map(x -> { String[] hostInfo = x.split(":"); return new HttpHost(hostInfo[0], Integer.parseInt(hostInfo[1])); }).toArray(HttpHost[]::new); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(account, passWord));//设置账号密码 RestClientBuilder builder = RestClient.builder(httpHosts) .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); // Create the low-level client RestClient restClient = builder.build(); // Create the transport with a Jackson mapper ElasticsearchTransport transport = new RestClientTransport( restClient, new JacksonJsonpMapper()); // And create the API client client = new ElasticsearchClient(transport);//获取连接 } }3、使用ElasticsearchClient连接查询es
官网现在使用Lambda表达式来进行 *** 作,代码简洁明了很多,缺点就是官网提供的教程太少。
新版本的搜索,根据sql理解为:
select * from logs-itms-nbiproc where timestamp >= startTime and timestamp <= endTime and msg_type = registration or msg_type = activation order by timestamp asc;
package com.alcatel.asb.hdm.report.device.manager; import co.elastic.clients.elasticsearch._types.SortOrder; import co.elastic.clients.elasticsearch.core.SearchResponse; import co.elastic.clients.elasticsearch.core.search.Hit; import co.elastic.clients.json.JsonData; import com.alcatel.asb.hdm.report.device.entity.ESEntity; import com.alcatel.asb.hdm.report.device.util.EsConfig; import java.text.SimpleDateFormat; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.*; public class ESChartManagerImpl implements ESChartManager { @Override public List getESList() { String startTime = ZonedDateTime.now().minusHours(1).format(DateTimeFormatter.ISO_INSTANT); String endTime = ZonedDateTime.now().format(DateTimeFormatter.ISO_INSTANT); System.out.println("开始时间:"+startTime); System.out.println("结束时间:"+endTime); MapesMap = new linkedHashMap<>(); try { // select * from logs-itms-nbiproc where timestamp >= startTime and timestamp <= endTime and msg_type = registration or msg_type = activation order by timestamp asc; SearchResponse
注:旧版和新版所返回的数据格式相比,旧版返回数据格式的为json字符串对象,当前新版返回的则是linkedHashMap对象集合,可直接get属性取值。
(((linkedHashMap) hit.source()).get("msg_type")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)