ElasticSearch 학습(13) Java 응용 프로그램에서 클러스터 관리

2074 단어 ElasticSearch
package Test;

import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.cluster.health.ClusterIndexHealth;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import java.net.InetAddress;

public class CluterDemo {

    @Test
    public void test1() throws Exception {

        // ES 
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        // ES 
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));


        ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().get();
        // 
        String clusterName = healthResponse.getClusterName();
        System.out.println("clusterName = "+clusterName);

        // 
        int numberOfDataNodes = healthResponse.getNumberOfDataNodes();
        System.out.println("numberOfDataNodes = "+numberOfDataNodes);

        // 
        int numberOfNodes = healthResponse.getNumberOfNodes();
        System.out.println("numberOfNodes = "+numberOfNodes);

        for (ClusterIndexHealth health : healthResponse.getIndices().values()){
            // 
            String index = health.getIndex();
            // 
            int numberOfShards = health.getNumberOfShards();
            // 
            int numberOfReplicas = health.getNumberOfReplicas();
            System.out.printf("index = %s,numberOfShards = %d,numberOfReplicas =%d
",index,numberOfShards,numberOfReplicas); // ClusterHealthStatus status = health.getStatus(); System.out.println(status.toString()); } } }

좋은 웹페이지 즐겨찾기