• ES简单工具类ESUtil


    package com.alibaba.otter.canal.utils;
    
    import com.alibaba.otter.canal.constants.ModuleEnum;
    import com.alibaba.otter.canal.custom.CanalLogUtil;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.beanutils.BeanMap;
    import org.apache.commons.lang3.StringUtils;
    import org.apache.http.HttpHost;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestClientBuilder;
    import org.elasticsearch.client.RestHighLevelClient;
    
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Objects;
    
    /**
     * @author kaikai_zheng
     * @version 1.0.0
     * @className ESUtil
     * @description //ES工具类
     * @data 2020-07-13 11:19
     */
    @Slf4j
    public class ESUtil {
    
         // 改为从配置中读取集群节点
    //    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;
    //
        public ESUtil(){}
    
        // 双重校验锁
    //    public static ESUtil getInstance() {
    //        if (null == esUtil) {
    //            synchronized (ESUtil.class){
    //                if (null == esUtil) {
    //                    esUtil = new ESUtil();
    //                }
    //            }
    //        }
    //        return esUtil;
    //    }
    
        public RestHighLevelClient getClient() {
            RestHighLevelClient client = new RestHighLevelClient(
                    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)
            );
            return client;
        }
    
    
        public void closeClient(RestHighLevelClient client) {
            try {
                client.close();
            } catch (IOException e) {
                CanalLogUtil.error(ModuleEnum.CANAL_CLIENT.getCode(), "colse es client exception,error=", e);
                e.printStackTrace();
            }
        }
    
        public RestClientBuilder 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);
            return RestClient.builder(hosts);
        }
    
        private HttpHost makeHttpHost(String s) {
            assert StringUtils.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 new HttpHost(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 = new BeanMap(bean);
                for (Object key : beanMap.keySet()) {
                    if (beanMap.get(key) != null) {
                        map.put(key + "", beanMap.get(key));
                    }
                }
            }
            return map;
        }
    
    }
  • 相关阅读:
    CentOS7.6下安装qt-creator
    chromedriver下载地址
    selenium 基于Autolt工具对于Windows窗口上传文件操作
    Selenium+java
    jmeter删除历史打开的脚本记录
    echarts 各种特效图
    SpringBoot启动-问题解决:Could not locate executable nullinwinutils.exe in the Hadoop binaries
    pinyin4j
    JMeter内存溢出:java.lang.OutOfMemoryError: Java heap space解决方法
    selenium自动化测试中升级chrome78版本带来的问题
  • 原文地址:https://www.cnblogs.com/zk-blog/p/13409750.html
Copyright © 2020-2023  润新知