ES 7.16 Java API Client 新特性(使用ElasticsearchClient连接操作ES完整demo)

截止7.15版本TransportClient tcp长连接方式,HighLevelRestClient Http rest连接方式官网已淘汰。

使用最新 ElasticsearchClient连接操作ES完整教程:

1、添加依赖

    
      co.elastic.clients
      elasticsearch-java
      7.16.2
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.12.3
    

2、.properties


# es 服务地址
elasticsearch.clusterNodes=192.168.1.96:9200,192.168.1.97:9200,192.168.1.98:9200

# es 账号
elasticsearch.account=elastic

# es 密码
elasticsearch.passWord=123456

3、连接es集群(根据情况是否设置账号和密码,我这边是需要的)

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;

/**
 * @author Liner
 * todo ES连接配置工具
 * @date 2021/12/9 21:36
 */
public class EsConfig {

    @Value(value = "${elasticsearch.clusterNodes}")
    private String clusterNodes;//es集群节点 例://192.168.1.96:9200,192.168.1.97:9200,192.168.1.98:9200

    @Value(value = "${elasticsearch.account}")
    private String account;//账号 例:elastic

    @Value(value = "${elasticsearch.passWord}")
    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);
        Map esMap = 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 search = EsConfig.client.search(s -> s
                            .index("logs-itms-nbiproc")// index
                            .sort(s1 -> s1.field(f -> f.field("timestamp").order(SortOrder.Asc))) //排序
                            .scroll(t -> t.offset(0)) 
                            .from(0)    //从第0条开始
                            .size(500) //一次查500条
                            .query(q -> q.bool(t -> t.must(q1 -> q1
                                    .range(t1 -> t1.field("timestamp").lte(JsonData.of(endTime)).gte(JsonData.of(startTime))))// >= <=
                                    .must(q2 -> q2.bool(t2 -> t2 // and
                                            .should(q3 -> q3.term(t3 ->  t3.field("msg_type").value(v3 -> v3.stringValue("registration")))) // or
                                            .should(q4 -> q4.term(t4 -> t4.field("msg_type").value(v4 -> v4.stringValue("activation"))))))
                            )),
                    Object.class);
               String scrollId = search.scrollId();//获取游标
               while (search.hits().hits().size()>0) {
                List> hits = search.hits().hits();
                for (Hit hit : hits) {
                    System.out.println(hit.source());//拿到数据
                    (((LinkedHashMap) hit.source()).get("msg_type"));//取属性值
                }
                search = EsConfig.client.scroll(t -> t.scrollId(scrollId), Object.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return list;
    }
} 
  

注:旧版和新版所返回的数据格式相比,旧版返回数据格式的为json字符串对象,当前新版返回的则是LinkedHashMap对象集合,可直接get属性取值。

(((LinkedHashMap) hit.source()).get("msg_type")

你可能感兴趣的:(es,elasticsearch,java,大数据)