Java 使用 HttpClient 调用 es restful api 操作es

前言

今天交付 ES 管理平台,因为 ES 有两套集群,分别是5.x 和 6.x 为了代码的通用性,需要把 Transport Client 的相关操作全部废弃,改为直接调用 rest api

准备工作

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>

        <dependency>
            <groupId>com.arronlong</groupId>
            <artifactId>httpclientutil</artifactId>
            <version>1.0.4</version>
        </dependency>
<!--        工具集-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.8</version>
        </dependency>

相关操作

1.根据ip地址获取所有索引

/**
     * 获取所有索引index
     * @param ip
     * @return
     */
    public static List<IndexInfo> getAllIndex(String ip) {
   
   
        HttpConfig config = HttpConfig.custom().url("http://"+ip+":9200/_cat/indices?format=json&pretty");
        String jsonStr = null;
        try {
   
   
            jsonStr = HttpClientUtil.get(config);
        } catch (HttpProcessException e) {
   
   
            e.printStackTrace();
            return null;
        }
        JSONArray jsonArray = JSONUtil.parseArray(jsonStr);

        List<IndexInfo> list = new ArrayList<>();
        List<Map> maps = jsonArray.toList(Map.class);
        for(Map map : maps) {
   
   
            IndexInfo index = new IndexInfo();
            index.setHealth ((String)map.get("health"));
            index.setStatus ((String)map.get("status"));
            index.setIndex ((String)map.get("index"));</
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值