Java 使用 HttpClient 呼叫 es restful api 操作es
阿新 • • 發佈:2020-08-13
前言
今天交付 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")); index.setUuid ((String)map.get("uuid")); index.setPri ((String)map.get("pri")); index.setRep ((String)map.get("rep")); index.setDocs ((String)map.get("docs.count")); index.setDeleted ((String)map.get("docs.deleted")); index.setStoreSize ((String)map.get("store.size")); index.setPriStoreSize ((String)map.get("pri.store.size")); list.add(index); } return list; }
未完待續