elasticsearch 5.0 獲取 TransportClient 操作客戶端java API


本文轉載自: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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM