Java教程

ES通过ResultfulAPI查询集群状态(包含集群认证+node.role角色)

本文主要是介绍ES通过ResultfulAPI查询集群状态(包含集群认证+node.role角色),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;


import java.util.*;


/**
 * @author Miller
 * @date 2021/7/30
 */

public class testESInfo {

    public static Map<String, Object> getIndexInfoByIndexName(String queryName) {
        //参数为查询的
        Map<String, Object> indexInfoMap = new HashMap<>();

        // 拼接URL,任意主节点 IP + 9200 端口
        String indexInfoUrl = "http://192.168.**.**:9200/_cat/";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        String auth = "username" + ":" + "password";
        //设置请求头,添加认证
        headers.set("Authorization",
                "Basic " +
                        Base64.getEncoder()
                                .encodeToString(auth.getBytes()));
    	//indexInfoUrl + queryName拼接等价于服务器curl查询
     	//例如:参数为"nodes/?v",则等价于  curl -u 'username:password' "http://192.168.**.**:9200/_cat/nodes/?v""
        try {
            //返回为:行通过换行符,每个字段之间通过n(n>0)个空格。(一个大字符串)
            //ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
    	//192.168.**.**           18          85   0    0.01    0.02     0.05 dilrtw    -      node-**
    	//192.168.**.**           20          73   0    0.01    0.02     0.05 cilmrtv   m      node-**
            ResponseEntity<String> responseEntity = restTemplate.exchange
                    (indexInfoUrl + queryName, HttpMethod.GET, new HttpEntity<>(headers), String.class);

            if (responseEntity != null) {

                String body = responseEntity.getBody();
                String[] split = body.split("\n");
    		//根据查询不同处理逻辑不同
             /*   for (String s : split){
                 //表头不做处理
                    if (s.startsWith("health")){
                        continue;
                    }else {
                        //切割每个字段的值:  正则 "\\s+" 可切割字段建空格为n(n>0)字符串
                        String[] split1 = s.split("\\s+");
                       /* Arrays.stream(split1).forEach(
                                i -> System.out.println(i)
                        );*/
                    }
                    System.out.println(s);
                }
    	*/
     
     
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return indexInfoMap;
    }

    public static void main(String[] args) {
       	// getIndexInfoByIndexName("indices/?v");
            getIndexInfoByIndexName("nodes/?v");
      
    }
}

ES节点角色:

c : code node
d : data node				
f : frozen node
h : hot node
i : ingest node
l : machine learning node
m : master eligible node
r : remote cluster client node
s : content node
t : transform node
v : voting-only node
w : warm node
-  : coordinating node only

ES查询官网API

https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-health.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-health.html

这篇关于ES通过ResultfulAPI查询集群状态(包含集群认证+node.role角色)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!