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

ES 7.16 Java API Client 新特性(使用ElasticsearchClient连接 *** 作ES完整demo),第1张

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、连接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;


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);
        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")

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5704800.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)