1、pom.xml中集成rest-high-level-client
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.7.1</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.7.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.7.1</version>
</dependency>
2、java代码
package com.ruoyi.project.log.controller;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.client.GetAliasesResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class t {
RestHighLevelClient client = null;
/**
* 获取api操作客户端
*
* @return
*/
public void getClient() {
client = new RestHighLevelClient(RestClient.builder(
new HttpHost("192.168.81.129", 9200, "http")
));
}
/**
* 关闭客户端
*/
public void closeClient() {
try {
if (client != null) {
client.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取所有es索引
*/
public Set<String> getAllIndices() {
try {
getClient();
GetAliasesRequest request = new GetAliasesRequest();
GetAliasesResponse getAliasesResponse = client.indices().getAlias(request, RequestOptions.DEFAULT);
Map<String, Set<AliasMetaData>> map = getAliasesResponse.getAliases();
Set<String> indices = map.keySet();
return indices;
} catch (IOException e) {
e.printStackTrace();
} finally {
closeClient();
}
return new HashSet<>();
}
// 查询所有下所有字段
public void test() {
try {
//指定索引
GetMappingsRequest getMappings = new GetMappingsRequest().indices("wxjc2-logstash-2020.08.21");
//调用获取
getClient();
GetMappingsResponse getMappingResponse = client.indices().getMapping(getMappings, RequestOptions.DEFAULT);
//处理数据
Map<String, MappingMetaData> allMappings = getMappingResponse.mappings();
List<Map<String, Object>> mapList = new ArrayList<>();
for (Map.Entry<String, MappingMetaData> indexValue : allMappings.entrySet()) {
Map<String, Object> mapping = indexValue.getValue().sourceAsMap();
Iterator<Map.Entry<String, Object>> entries = mapping.entrySet().iterator();
entries.forEachRemaining(stringObjectEntry -> {
if (stringObjectEntry.getKey().equals("properties")) {
Map<String, Object> value = (Map<String, Object>) stringObjectEntry.getValue();
for (Map.Entry<String, Object> ObjectEntry : value.entrySet()) {
Map<String, Object> map = new HashMap<>();
String key = ObjectEntry.getKey();
Map<String, Object> value1 = (Map<String, Object>) ObjectEntry.getValue();
map.put(key, value1.get("type"));
mapList.add(map);
}
}
});
}
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writeValueAsString(mapList));
} catch (IOException e) {
e.printStackTrace();
}finally {
closeClient();
}
}
public static void main(String[] args) {
t t = new t();
System.out.println(t.getAllIndices());
}
}
附Java High Level REST Client文档地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html