elastic java client_java-如何使用Elastic的高级Rest Client获取所有...

我想要一个不错的,快速且简便的方法来使用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字符串解析.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值