Elasticsearch 5.x中, 获取集群的信息可以用 TransportClient中的方法获取, 代码如下:
ClusterStatsResponse clusterStatsResponse
= TransportClient
.admin()
.cluster()
.clusterStats(new ClusterStatsRequest())
.actionGet();
但是在 ES7.x 中,今天在更新Api时,未找到对应org.elasticsearch.action.admin.cluster.stats.ClusterStatsRequest 放在哪个client ()中去请求,
RestHighLevelClient中未找到
网上找到另一种解决方法, 可以用 org.elasticsearch.client.RestHighLevelClient#getLowLevelClient 类去发送 http请求,获取 response回复来获取数据,代码如下 :
// 此处代替http请求 GET /_cluster/stats
Response resp = RestHighLevelClient.getLowLevelClient().performRequest(new Request("GET", "/_cluster/stats"));
// http-core中的 HttpEntity
org.apache.http.HttpEntity entity = resp.getEntity();
// 直接返回http请求结果
String str = org.apache.http.util.EntityUtils.toString(entity , "UTF-8");
// 转换为jsonObject
JSONObject jsonObject = JSONObject.fromObject(str);
if (null != entity) {
BigDecimal gb = new BigDecimal(1024 * 1024 * 1024);
if (jsonObject instanceof JSONObject) {
Object fs = ((JSONObject) jsonObject.get("nodes")).get("fs");
if (fs instanceof JSONObject) {
JSONObject fsObj = (JSONObject) fs;
// 这里是获取Elasticsearch所在磁盘的 使用情况
long total = new BigDecimal(String.valueOf(fsObj.get("total_in_bytes"))).longValue();
long available = new BigDecimal(String.valueOf(fsObj.get("available_in_bytes"))).longValue();
}
}
}
以下是直接发 GET /_cluster/stats 获取到的返回
{
"_nodes": {
"total": 1,
"successful": 1,
"failed": 0
},
"cluster_name": "esearch",
"cluster_uuid": "HCeyhsi6TAyPhjdGyzcEcw",
"timestamp": 1653482240155,
"status": "yellow",
"indices": {
"count": 22,
"shards": {
"total": 56,
"primaries": 56,
"replication": 0.0,
"index": {
"shards": {
"min": 2,
"max": 5,
"avg": 2.5454545454545454
},
"primaries": {
"min": 2,
"max": 5,
"avg": 2.5454545454545454
},
"replication": {
"min": 0.0,
"max": 0.0,
"avg": 0.0
}
}
},
"docs": {
"count": 460,
"deleted": 246
},
"store": {
"size_in_bytes": 2203406,
"reserved_in_bytes": 0
},
"fielddata": {
"memory_size_in_bytes": 14148,
"evictions": 0
},
"query_cache": {
"memory_size_in_bytes": 0,
"total_count": 0,
"hit_count": 0,
"miss_count": 0,
"cache_size": 0,
"cache_count": 0,
"evictions": 0
},
"completion": {
"size_in_bytes": 0
},
"segments": {
"count": 62,
"memory_in_bytes": 455568,
"terms_memory_in_bytes": 360352,
"stored_fields_memory_in_bytes": 30256,
"term_vectors_memory_in_bytes": 10736,
"norms_memory_in_bytes": 39808,
"points_memory_in_bytes": 0,
"doc_values_memory_in_bytes": 14416,
"index_writer_memory_in_bytes": 0,
"version_map_memory_in_bytes": 0,
"fixed_bit_set_memory_in_bytes": 0,
"max_unsafe_auto_id_timestamp": -1,
"file_sizes": {}
},
"mappings": {
"field_types": [
{
"name": "date",
"count": 9,
"index_count": 4
},
{
"name": "geo_point",
"count": 1,
"index_count": 1
},
{
"name": "integer",
"count": 119,
"index_count": 17
},
{
"name": "keyword",
"count": 334,
"index_count": 22
},
{
"name": "long",
"count": 33,
"index_count": 10
},
{
"name": "object",
"count": 5,
"index_count": 1
},
{
"name": "text",
"count": 326,
"index_count": 21
}
]
},
"analysis": {
"char_filter_types": [],
"tokenizer_types": [],
"filter_types": [],
"analyzer_types": [
{
"name": "custom",
"count": 42,
"index_count": 21
}
],
"built_in_char_filters": [],
"built_in_tokenizers": [
{
"name": "ik_max_word",
"count": 41,
"index_count": 21
},
{
"name": "keyword",
"count": 1,
"index_count": 1
}
],
"built_in_filters": [
{
"name": "lowercase",
"count": 1,
"index_count": 1
},
{
"name": "word_delimiter",
"count": 20,
"index_count": 20
}
],
"built_in_analyzers": [
{
"name": "ik_smart",
"count": 78,
"index_count": 17
},
{
"name": "standard",
"count": 4,
"index_count": 2
},
{
"name": "whitespace",
"count": 50,
"index_count": 19
}
]
}
},
"nodes": {
"count": {
"total": 1,
"coordinating_only": 0,
"data": 1,
"data_cold": 1,
"data_content": 1,
"data_hot": 1,
"data_warm": 1,
"ingest": 1,
"master": 1,
"ml": 1,
"remote_cluster_client": 1,
"transform": 1,
"voting_only": 0
},
"versions": [
"7.10.1"
],
"os": {
"available_processors": 4,
"allocated_processors": 4,
"names": [
{
"name": "Linux",
"count": 1
}
],
"pretty_names": [
{
"pretty_name": "CentOS Linux 7 (Core)",
"count": 1
}
],
"mem": {
"total_in_bytes": 8202833920,
"free_in_bytes": 179916800,
"used_in_bytes": 8022917120,
"free_percent": 2,
"used_percent": 98
}
},
"process": {
"cpu": {
"percent": 0
},
"open_file_descriptors": {
"min": 476,
"max": 476,
"avg": 476
}
},
"jvm": {
"max_uptime_in_millis": 118940504,
"versions": [
{
"version": "15.0.1",
"vm_name": "OpenJDK 64-Bit Server VM",
"vm_version": "15.0.1+9",
"vm_vendor": "AdoptOpenJDK",
"bundled_jdk": true,
"using_bundled_jdk": true,
"count": 1
}
],
"mem": {
"heap_used_in_bytes": 978245832,
"heap_max_in_bytes": 2147483648
},
"threads": 59
},
"fs": {
"total_in_bytes": 19094568960,
"free_in_bytes": 635289600,
"available_in_bytes": 635289600
},
"plugins": [
{
"name": "analysis-ik",
"version": "7.10.1",
"elasticsearch_version": "7.10.1",
"java_version": "1.8",
"description": "IK Analyzer for Elasticsearch",
"classname": "org.elasticsearch.plugin.analysis.ik.AnalysisIkPlugin",
"extended_plugins": [],
"has_native_controller": false
}
],
"network_types": {
"transport_types": {
"security4": 1
},
"http_types": {
"security4": 1
}
},
"discovery_types": {
"zen": 1
},
"packaging_types": [
{
"flavor": "default",
"type": "tar",
"count": 1
}
],
"ingest": {
"number_of_pipelines": 1,
"processor_stats": {
"gsub": {
"count": 0,
"failed": 0,
"current": 0,
"time_in_millis": 0
},
"script": {
"count": 0,
"failed": 0,
"current": 0,
"time_in_millis": 0
}
}
}
}
}