hee*_*eez 3 java elasticsearch
我想要一种不错的,快速且简便的方法,使用其Java REST客户端在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;
}
}
Run Code Online (Sandbox Code Playgroud)
本质上,我只是按照他们的文档中的建议将其称为/_cat/indices?v端点。这可以正常工作,但是我想知道是否有使用Java API的更好的方法。我似乎无法在他们当前的API中找到方法,但想知道是否有人知道我不知道的东西。必须使用s和各个s并不一定很糟糕,而只是想清理hacky字符串解析。InputStreamReader
Mar*_*man 10
从Elasticsearch 6.4.0开始,您可以使用以下方法检索所有索引:
GetIndexRequest request = new GetIndexRequest().indices("*");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
String[] indices = response.getIndices();
Run Code Online (Sandbox Code Playgroud)
目前,高级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;
}
Run Code Online (Sandbox Code Playgroud)
注意:这是高级REST客户端的路线图:https : //github.com/elastic/elasticsearch/issues/27205
| 归档时间: |
|
| 查看次数: |
2954 次 |
| 最近记录: |