java-如何使用Elastic的高级Rest Client获取所有索引?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何使用Elastic的高级Rest Client获取所有索引?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1775字,纯文字阅读大概需要3分钟。
内容图文
![java-如何使用Elastic的高级Rest Client获取所有索引?](/upload/InfoBanner/zyjiaocheng/692/309227d2ddc246f693c1478051791ddc.jpg)
我想要一个不错的,快速且简便的方法来使用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中找到方法,但想知道是否有人知道我不知道的东西.不得不使用InputStreams和各种Reader并不一定很糟糕,而只是想清理hacky字符串解析.
解决方法:
从Elasticsearch 6.4.0开始,您可以使用以下方法检索所有索引:
GetIndexRequest request = new GetIndexRequest().indices("*");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
String[] indices = response.getIndices();
内容总结
以上是互联网集市为您收集整理的java-如何使用Elastic的高级Rest Client获取所有索引?全部内容,希望文章能够帮你解决java-如何使用Elastic的高级Rest Client获取所有索引?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。