ES简单工具类ESUtil

摘要:
=null){map.put(key+"",beanMap.get(key));}}}returnmap;}}
packagecom.alibaba.otter.canal.utils;

importcom.alibaba.otter.canal.constants.ModuleEnum;
importcom.alibaba.otter.canal.custom.CanalLogUtil;
importlombok.extern.slf4j.Slf4j;
importorg.apache.commons.beanutils.BeanMap;
importorg.apache.commons.lang3.StringUtils;
importorg.apache.http.HttpHost;
importorg.elasticsearch.client.RestClient;
importorg.elasticsearch.client.RestClientBuilder;
importorg.elasticsearch.client.RestHighLevelClient;

importjava.io.IOException;
importjava.util.Arrays;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Objects;

/*** @authorkaikai_zheng
 * @version1.0.0
 * @className ESUtil
 * @description //ES工具类
 * @data 2020-07-13 11:19
 */@Slf4j
public classESUtil {

     //改为从配置中读取集群节点
//private final static String HOST_IP1 = "47.101.179.162";
//private final static String HOST_IP2 = "10.2.6.58";
//private final static String HOST_IP3 = "10.2.6.59";
//private final static String HOST_IP4 = "10.2.6.60";
//private final static int PORT = 9200;
    /*** 超时时间设为5分钟
     */
    private static final int TIME_OUT = 5 * 60 * 1000;
    private static final int ADDRESS_LENGTH = 2;
    private static final String HTTP_SCHEME = "http";

//
//private volatile static ESUtil esUtil;
//
    publicESUtil(){}

    //双重校验锁
//public static ESUtil getInstance() {
//if (null == esUtil) {
//synchronized (ESUtil.class){
//if (null == esUtil) {
//esUtil = new ESUtil();
//}
//}
//}
//return esUtil;
//}

    publicRestHighLevelClient getClient() {
        RestHighLevelClient client = newRestHighLevelClient(
                restClientBuilder()
//RestClient.builder(
//new HttpHost(HOST_IP1, PORT, PROTOCAL)
//new HttpHost(HOST_IP2, PORT, PROTOCAL),
//new HttpHost(HOST_IP3, PORT, PROTOCAL),
//new HttpHost(HOST_IP4, PORT, PROTOCAL)
);
        returnclient;
    }


    public voidcloseClient(RestHighLevelClient client) {
        try{
            client.close();
        } catch(IOException e) {
            CanalLogUtil.error(ModuleEnum.CANAL_CLIENT.getCode(), "colse es client exception,error=", e);
            e.printStackTrace();
        }
    }

    publicRestClientBuilder restClientBuilder() {
        String ipAddress[] = ResourceUtil.getResource().getProperty("es_cluster").split(",");
        CanalLogUtil.info(ModuleEnum.CANAL_CLIENT.getCode(),"load es cluster nodes result="+ipAddress);
        HttpHost[] hosts =Arrays.stream(ipAddress)
                .map(this::makeHttpHost)
                .filter(Objects::nonNull)
                .toArray(HttpHost[]::new);
        returnRestClient.builder(hosts);
    }

    privateHttpHost makeHttpHost(String s) {
        assertStringUtils.isNotEmpty(s);
        String[] address = s.split(":");
        if (address.length ==ADDRESS_LENGTH) {
            String ip = address[0];
            int port = Integer.parseInt(address[1]);
            System.err.println(ip+"+"+port);
            return newHttpHost(ip, port, HTTP_SCHEME);
        } else{
            return null;
        }
    }

    public static <T> Map<String, Object>beanToMap(T bean) {
        Map<String, Object> map = new HashMap<>();
        if (bean != null) {
            BeanMap beanMap = newBeanMap(bean);
            for(Object key : beanMap.keySet()) {
                if (beanMap.get(key) != null) {
                    map.put(key + "", beanMap.get(key));
                }
            }
        }
        returnmap;
    }

}

免责声明:文章转载自《ES简单工具类ESUtil》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇wpf中遍历界面控件的方法Android开发 设备横屏与竖屏的详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

将MP3文件嵌入到exe中并播放

需要编写一个exe文件,而其中嵌入了一段我设计好的音乐,打开exe后音乐自动播放。最重要的是除了 exe 文件外不能有额外的附加文件,将这个exe拷到其他(安装有framework的)电脑上,需要能正常运行。 1.资源嵌入 这个比较简单,vs2008 提供了非常方便的方法,新增一个项目后,打开 Properties 文件夹下的 Resources.res...

ToString和Convert.ToString处理null值

http://www.cnblogs.com/qinge/p/5687806.html文章来源 1.Convert.ToString能处理字符串为null的情况。 测试代码如下: 1 2 3 4 5 6 static void Main(string[] args) {   string msg = null;   Console.W...

Guava LoadingCache不能缓存null值

测试的时候发现项目中的LoadingCache没有刷新,但是明明调用了refresh方法了。后来发现LoadingCache是不支持缓存null值的,如果load回调方法返回null,则在get的时候会抛出异常。 通过几个例子开看这个问题: public void test_loadNull() { LoadingCache<String,...

Shiro快速入门

写在前面:   最近项目中使用了Shiro,虽然不是自己在负责这一块,但还是抽空学习了下,也可以让自己对shiro有基本的了解。毕竟Shiro安全框架在项目中还是挺常用的。   对于Apache Shiro的基本概念就不在这里一一描述了,资料网上都有,主要还是记录下代码相关的,能够先让自己快速学会使用。   这里的demo(可以测试登录认证,登出,以及授权...

redis实现接口限流

总结一下,之前的一个项目需要做限流:秒杀商品的接口。 其他一些博客有很多文章介绍用谷歌的插件:guaue(针对于单机的),如果是微服务的话推荐使用:senilxxxxx啥来的。具体有些忘记了。 这篇文章分享的内容是用redis+自定义注解+拦截器是实现限流(相对简单的一种) 废话不多说:首先reids的配置类:RedisConfig import com....

C# DataGridView控件中数据导出到Excel

方法一: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Form...