贫民窟里的程序高手

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

本文转载自:http://blog.csdn.net/likui1314159/article/details/53233881

elasticsearch 5.0 获取 TransportClient 操作客户端java API 跟之前的版本有点差别了,我也是找了好一会才找到,用到的拿走

private static TransportClient  transPort = null;   
    private String esClusterName;//集群名
    private String esServerIps;//集群服务IP集合
    private Integer esServerPort;//ES集群端口


 /**
     *  ES TransPortClient 客户端连接<br>
     *  在elasticsearch平台中,可以执行创建索引,获取索引,删除索引,搜索索引等操作
     * @return
     */
    public TransportClient getTransPortClient() {
        try {
            if (transPort == null) {

                if(esServerIps == null || "".equals(esServerIps.trim())){
                    return  null;
                }

                Settings settings = Settings.builder()
//                      .put("cluster.name", esClusterName)// 集群名
                        .put("client.transport.sniff", true)
                        // 自动把集群下的机器添加到列表中

                        .build();
                transPort  = new  PreBuiltTransportClient(settings);
                String esIps[] = esServerIps.split(",");
                for (String esIp : esIps) {//添加集群IP列表
                    TransportAddress transportAddress =  new InetSocketTransportAddress(InetAddresses.forString(esIp),9300);
                    transPort.addTransportAddresses(transportAddress);
                }
                return transPort;
            } else {
                return transPort;
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (transPort != null) {
                transPort.close();
            }
            return null;
        }
    }

官网参考链接:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html

posted on 2017-11-27 13:33  贫民窟里的程序高手  阅读(1671)  评论(0编辑  收藏  举报