官网地址:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.7/java-rest-high-get-settings.html
//获取shards
override lazy val numberOfShards : Int = {
LoggerUtils logInfo ("获取(shards)===============" )
val client = ESUtils.getRestHighLevelClient(elasticsearchHost, elasticsearchPort)
val shards = ESUtils.getShards(elasticsearchIndex, client)
ESUtils.closeESClient(client)
shards
}
object ESUtils{
//获取shards
def getShards(indexName: String, client: RestHighLevelClient): Int={
val request = new GetSettingsRequest().indices(indexName)
request.names("index.number_of_shards")
val response = client.indices().getSettings(request, RequestOptions.DEFAULT)
val shardsString = response.getSetting(indexName, "index.number_of_shards")
client.close()
val shards = shardsString.toInt
shards
}
}
该代码片段展示了如何利用Elasticsearch的Java REST High Level Client从官方文档链接获取指定索引的分片数量。首先初始化客户端,然后创建GetSettingsRequest,设置索引名并请求特定的设置(index.number_of_shards)。接着通过调用indices().getSettings()获取响应,从中解析出分片数,并确保客户端关闭。
1778

被折叠的 条评论
为什么被折叠?



