我想要一种不错的,快速且简便的方法,使用它们的Java REST client来获取Elasticsearch中的所有索引。目前,我可以通过抓住他们的较低级别的客户端来做到这一点,就像这样:
public void fetchIndices() throws IOException {
List<String> indices = null;
RestClient restClient = client.getLowLevelClient();
Response response = null;
try {
response = restClient.performRequest("GET", "/_cat/indices?v");
} catch (IOException e) {
LOGGER.log(Level.WARNING, e.toString(), e);
}
InputStream inputStream = null;
if (response != null) {
try {
inputStream = response.getEntity().getContent();
} catch (IOException e) {
LOGGER.log(Level.WARNING, e.toString(), e);
}
}
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
indices = new ArrayList<>();
String line;
while ((line = bufferedReader.readLine()) != null) {
// Get tokens with no whitespace
String[] tokens = line.split("\\s+");
for (String token : tokens) {
// TODO - make the startsWith() token configurable
if (token.startsWith(SOME_TOKEN)) {
LOGGER.log(Level.INFO, "Found elasticsearch index " + token);
indices.add(token);
break;
}
}
}
}
// Only update if we got data back from our REST call
if (indices != null) {
this.indices = indices;
}
}
基本上,我只是称呼/_cat/indices?v
端点as recommended in their docs。这可以正常工作,但是我想知道是否有使用Java API的更好的方法。我似乎无法在他们当前的API中找到方法,但想知道是否有人知道我不知道的东西。必须使用InputStream
和各种Reader
并不一定很可怕,而只是想清理hacky字符串解析。
答案 0 :(得分:6)
从Elasticsearch 6.4.0开始,您可以使用以下方法检索所有索引:
GetIndexRequest request = new GetIndexRequest().indices("*");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
String[] indices = response.getIndices();
答案 1 :(得分:3)
目前,高级REST客户端不支持此功能。您可以继续使用低级客户端调用_cat/indices
API,但尝试在查询字符串参数中添加&format=json
。这样,您将获得相同的信息,但格式为JSON,这使得解析起来容易得多(例如,使用Jackson库):
List<String> indices = null;
RestClient restClient = client.getLowLevelClient();
Response response = null;
try {
response = restClient.performRequest("GET", "/_cat/indices?v&format=json");
} catch (IOException e) {
LOGGER.log(Level.WARNING, e.toString(), e);
}
// parse the JSON response
List<Map<String, String>> list = null;
if (response != null) {
String rawBody = EntityUtils.toString(response.getEntity());
TypeReference<List<HashMap<String, String>>> typeRef = new TypeReference<List<HashMap<String, String>>>() {};
list = mapper.readValue(rawBody, typeRef);
}
// get the index names
if (list != null) {
indices = list.stream()
.map(x -> x.get("index"))
.collect(Collectors.toList());
}
// Only update if we got data back from our REST call
if (indices != null) {
this.indices = indices;
}
注意:以下是高级REST客户端的路线图:https://github.com/elastic/elasticsearch/issues/27205
答案 2 :(得分:0)
尝试使用:/ _cat / indices?h = i
InputStream inputStream = restHighLevelClient.getLowLevelClient()
.performRequest("GET", "/_cat/indices?h=i")
.getHttpResponse()
.getEntity()
.getContent();
List<String> indexes = new BufferedReader(new InputStreamReader(inputStream))
.lines()
.collect(Collectors.toList());
此外,如果您要使用正则表达式进行搜索:/ _cat / indices?h = i&index = test *
答案 3 :(得分:0)
对于ES版本7.3
不幸的是,Elasticsearch路线图页面中开发人员的答案是:
“认为添加对cat API的支持没有道理”
因此,我只看到一种解决方案-使用低级客户端。
在我的应用程序中,我使用以下示例:
AttributeError: type object 'Deck' has no attribute 'deck'
映射器-来自Jackson库
ObjectMapper映射器=新的ObjectMapper();
希望此代码对您有帮助:)
答案 4 :(得分:0)
在es 6.8上,如果没有索引抛出NoSuchIndexException,请在*
中使用_all
或GetIndexRequest
。
我发现这是更安全,没有抛出的方法:
GetMappingsResponse response = esClient.indices().getMapping(new GetMappingsRequest().indices("*"),
RequestOptions.DEFAULT);
return new ArrayList<>(response.mappings().keySet());