Elasticsearch-如何获得单个索引的容量
因为考虑到索引容量过大,要将其设置为只读,创建新的索引;考虑到定时检测索引大小,高级REST没有获得索引大小的函数,在之前REST学习时,结合Postman做了大量的PUT、POST、GET等练习,其中查看elasticsearch中索引状态时,是有store.size这个字段名的,查了一下,果然有Java API;另外,在这之前,了解到
RestHighLevelClient是没有获得索引大小的函数,但是TransportClient是有的,庆幸使用的elasticsearch是7.x的;
众所周知,TransportClient在7.x已经开始弃用了,到8.x已经完全删除了。所以,我使用两种方法获得索引大小;
第一种:使用cat indices。
String responseAsString = EntityUtils.toString(client.getLowLevelClient().performRequest(new Request("GET", "/_cat/indices/twitter?v&h=store.size")).getEntity());
这里,如果不想得到字段头,可以把v去掉,v就是表示返回的结果中包含字段头名称,但是千万要注意,这里?不能少,否则会报错!!
第二种:使用
TransportClient
//获取单个索引总容量 public long getTotalDisk(String index){ IndicesStatsResponse response = client.admin().indices().prepareStats(index).execute().actionGet(); CommonStats stats = response.getTotal(); return stats.getStore().getSize().getBytes(); }
这里getTotal方法只有TransportClient才有的;
当然,还要注入
TransportClient
@Autowired private TransportClient client;
如果运行代码有遇到
TransportClient类似问题,如,要求定义一个Bean的情况,请看下一篇文章!!

浙公网安备 33010602011771号