我想要一个不错的,快速且简便的方法来使用Java REST client来获取elasticsearch中的所有索引.我现在能够通过抓住其较低级别的客户端来做到这一点,如下所示:
public void fetchIndices() throws IOException {
List 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中找到方法,但想知道是否有人知道我不知道的东西.不得不使用InputStreams和各种Reader并不一定很糟糕,而只是想清理hacky字符串解析.