• ElastaticSearch ---es客户端 TransportClient


    TransportClient

    TransportClient客户端,官方在es 7.0版本中将弃用TransportClient客户端,且在8.0版本中完全移除它.
    es 7.0及以上的版本,请使用 RestHighLevelClient
    如果项目中使用的es版本不高,可以使用 TransportClient。

    依赖

            
                org.elasticsearch
                elasticsearch
                5.5.1
            
            
                org.elasticsearch.client
                transport
                5.5.1
            
            
                org.elasticsearch.plugin
                transport-netty4-client
                5.5.1
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    初始化TransportClient 客户端

    
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.InetSocketTransportAddress;
    import org.elasticsearch.transport.client.PreBuiltTransportClient;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    
    public class EsTransportClient {
    
        /**
         * 初始化TransportClient 客户端
         *
         * @param clusterName es集群名称
         * @param ip es的ip地址
         * @param port es的端口
         * @return
         */
        public static TransportClient getClient(String clusterName, String ip, int port) {
            Settings settings = Settings.builder()
                    .put("cluster.name", clusterName)
                    .put("client.transport.sniff", true)
                    .build();
    
            TransportClient transportClient = null;
            try {
                transportClient = new PreBuiltTransportClient(settings);
                transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), port));
                return transportClient;
            } catch (UnknownHostException e) {
                log.error("UnknownHostException exception.");
            }
            return transportClient;
        }
    
    
        /**
         * 多节点的es集群
         * @param nodes 格式为:ip:端口,多个节点用逗号隔开  ,比如 10.123.666.1:9300,10.123.666.2:9300
         * @param clusterName 集群名称
         * @return
         */
        public static TransportClient getClientByNodes(String nodes, String clusterName) {
            String[] hosts = nodes.split(",");
            Settings settings = Settings.builder().put("cluster.name", clusterName)
                    .put("client.transport.sniff", true)
                    .put("client.transport.ping_timeout", "10s")
                    .build();
            TransportClient esClient = null;
    
            try {
                esClient = new PreBuiltTransportClient(settings);
                for (String host : hosts) {
                    String[] hostAndPort = host.split(":");
                    esClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAndPort[0]),
                                Integer.parseInt(hostAndPort[1])));
                }
            } catch (NumberFormatException | UnknownHostException e) {
                log.error("getClientByNodes exception.", e);
            }
            return esClient;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
  • 相关阅读:
    搭建docker镜像仓库(二):使用harbor搭建本地镜像仓库
    国产超高清音视频标准首次用于世界杯直播
    SpringBoot集成Tomcat服务
    Leetcode 541:反转字符串II
    21.1 Python 使用PEfile分析PE文件
    Compose 布局
    剖析虚幻渲染体系(14)- 延展篇:现代渲染引擎演变史Part 4(结果期)
    在爬虫的时候发现request 中的from data 是一串数据格式
    【MySQL】事务的概念、特性及其分类
    语义分割算法及在无人驾驶的应用
  • 原文地址:https://blog.csdn.net/sinat_32502451/article/details/134368467