ElasticSearch學習(7)-Java鏈接ElasticSearch集羣

Java 操做ElasticSearch集羣很是簡單,和操做單個節點同樣,只是鏈接方式不一樣app

Java鏈接ElasticSearch集羣只須要主節點的ip和端口號,而後配置集羣的名字便可。ui

private static final String host = "192.168.15.38";

    private static final int port = 9300;
    private static TransportClient client = null;

    private static final String CLUSTER_NAME = "my-application";

    private static Settings.Builder builder = Settings.builder().put("cluster.name", CLUSTER_NAME);

    public static TransportClient cilentFactory() {
        client = new PreBuiltTransportClient(builder.build()).addTransportAddress(new TransportAddress(new InetSocketAddress(host, port)));
        return client;
    }

    public static void close() {
        if (client != null) {
            client.close();
        }
    }
相關文章
相關標籤/搜索