java处理通过RESTful API 获取索引和属性信息

上个博客写了通过RESTful API 获取es索引和属性信息的方式点我就能看

这次记录下拿到数据后的一个处理,不废话直接上代码 ~

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

/**
* @Auther  wdd
* @Date  2020/9/21 9:09
* @Desc  通过http方式查询es索引信息
*/
public class EsHttpBasic {

   
    private static String baseURL;

     //有参构造
    public EsHttpBasic(String baseURL){
        this.baseURL = baseURL;
    }

    /**
     * 获取所有索引信息
     * @param url
     * @return
     */
    public static List getIndices(String url){
        String method = "GET";
        String para = url;
        String restfulResult = excute(para, method, null);
        JSONArray picArray = JSONArray.parseArray(restfulResult);
        List<String> list = new ArrayList<>();
        if (null != picArray) {
            for(int i = 0; i < picArray.size(); i++) {
                JSONObject jsonObject = picArray.getJSONObject(i);
                list.add(jsonObject.getString("index"));
            }
        }
        return list;
    }

    public static void main(String[] args) {

        EsHttpBasic EsHttpBasic = new EsHttpBasic("http://你的host:端口"); 
        System.out.println("所有索引信息");
        System.out.println(getIndices("/_cat/indices?format=json"));
        System.out.println("根据索引获取属性信息");
        System.out.println(getIndicesField("xxxx_index").toString());
    }

    /**
     * 根据索引获取属性信息
     * @param indices
     * @return
     */
    public static Map<String, Object>  getIndicesField(String indices){
        //TreeMap 用来做key排序
        Map<String, Object> map = new TreeMap<>();
        String method = "GET";
        String para = "/"+indices;
        String excute = excute(para, method, null);
        Assert.isTrue( StringUtils.isNotBlank(excute) ,"获取失败 ...");
        JSONObject jsonObject = JSONObject.parseObject(excute);
        JSONObject person_index = jsonObject.getJSONObject(indices);
        JSONObject mappings = person_index.getJSONObject("mappings");
        Assert.isTrue(1 == mappings.keySet().size() ,"mappings 解析错误...");

        //mappings层获取key值 只有一个
        String key ="";
        for(String str:mappings.keySet()){
            key = str;
            break;
        }
        JSONObject person = mappings.getJSONObject(key);
        JSONObject properties = person.getJSONObject("properties");
        Set<String> strings = properties.keySet();
        for(String str:strings){
            //单个属性此处可以直接拿到type
            JSONObject obj = properties.getJSONObject(str);
            String type = obj.getString("type");
            //嵌套属性没有type,需要再深处解析properties
            if(StringUtils.isBlank(type)){
                JSONObject properties_2 = obj.getJSONObject("properties");
                Set<String> strings1 = properties_2.keySet();
                for(String str_2:strings1){
                    JSONObject obj_2 = properties_2.getJSONObject(str_2);
                    String type_2 = obj_2.getString("type");
                    //多层嵌套key为 第一层.第二层
                    map.put(str+"."+str_2,type_2);
                }
            }else {
                map.put(str,type);
            }
        }
        return  map;

    }
    private  static String excute(String para,String method,String body){

        StringBuilder out = new StringBuilder();
        try {
            URL url = new URL(baseURL+para);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(method);
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type","application/json");
            if(body !=null){
                byte[] outputInBytes = body.getBytes("UTF-8");
                OutputStream os = connection.getOutputStream();
                os.write(outputInBytes);
                os.close();
            }
            InputStream content = (InputStream)connection.getInputStream();
            BufferedReader in  = new BufferedReader (new InputStreamReader (content));
            String line;
            while ((line = in.readLine()) != null) {
                out.append(line);
            }
            in.close();
            connection.disconnect();

        } catch(Exception e) {
            e.printStackTrace();
        }
        return out.toString();
    }
}

执行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值