如何使用 Elastic 的高级 Rest 客户端获取所有索引?

2022-09-04 05:42:08

我想要一种很好,快速,简单的方法来使用他们的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;
    }
}

从本质上讲,我只是按照他们的文档中的建议调用端点。这工作正常,但我想知道是否有更好的方法来使用Java API来做到这一点。我似乎无法在他们当前的API中找到方法,但想知道是否有人知道我不知道的东西。必须使用s和各种s并不一定很糟糕,但只是想清理黑客字符串解析。/_cat/indices?vInputStreamReader


答案 1

从 Elasticsearch 7.5.0 开始,您可以使用以下内容来检索所有索引:

    GetIndexRequest request = new GetIndexRequest("*");
    GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
    String[] indices = response.getIndices();

答案 2

目前,高级 REST 客户端不支持此功能。您可以使用低级客户端继续调用 API,但尝试添加查询字符串参数。这样,您将获得相同的信息,但格式为JSON,这更容易解析(例如,使用Jackson库):_cat/indices&format=json

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


推荐