ElasticSearch learning (7) - Java connection to ElasticSearch cluster

It is very simple to operate an ElasticSearch cluster in Java. It is the same as operating a single node, but the connection method is different.

Java only needs the ip and port number of the master node to connect to the ElasticSearch cluster, and then configure the name of the cluster.

    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();
        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325646607&siteId=291194637