Elasticsearch教程

ES 集群API

Elasticsearch 集群API

在 Elasticsearch 中,集群 API 获取有关集群及其节点的信息。它还对它们进行了进一步的更改。借助 Cluster API,我们可以在集群级别执行 21 项操作。我们可以使用这个 API 来管理我们的集群。我们需要指定节点名称、地址或 _local 关键字以及 GET 方法来调用这个 API。
正如我们所说,这是一个分布式环境,我们有很多节点,而这个 Elasticsearch 环境可以是多节点的节点部署也是如此。因此,在这种情况下,集群 API 为我们提供了有关集群的信息。下面是集群API的一些要点-
大多数集群 API 都允许指定要执行的节点。 在 API 中,我们可以使用节点的内部节点名称、节点 ID、自定义属性、地址或仅接收 _local 节点请求来识别节点。
例如
GET http://localhost:9200/_nodes/_local
{ }
响应
您将得到与以下响应相同的输出。
{
         "nodes": {
	"total": 1,
	"successful": 1,
	"failed": 0
            }
	"cluster_name": "elasticsearch",
	"nodes": {
	       "DhlQVbbYS-SDo0HJC9pGjw": {
		"name": "DESKTOP_MAJMT36",
		"transport_address": "127.0.0.1:9300",
		"host": "127.0.0.1",
		"ip": "127.0.0.1",
		"version": "7.8.0",
		"build_flovor": "default",
		"build_type": "zip",
		"build_hash": "757314695644ea9a1dc2fecd26d1a43856725e65",
		"total_indexing_buffer": 107374182,
		.
		.
		.
		.
		.
		.
		.
		.
		"has_native_controller": false
	      }
	],
	"ingest": {
	       "processors": [
	       {
		"type": "append"
	       },
	       {
		"type": "bytes"
	       },
	       {
		"type": "urldecode"
	       },
	       {
		"type": "user_agent"
	       }
	   ]
	}
         }
     }
}
以下是一些命令,可以执行这些命令以获取有关集群的更多信息:
集群运行状况 集群状态 集群统计数据 待处理的集群任务 节点统计数据 节点 hot_thread

集群健康

使用这个API,我们可以检查集群的健康状态。集群健康 API 有助于获取集群的健康状况。它将集群状态返回为红色、黄色或绿色,分别表示停止、运行或就绪。
将 _cluster 和 health 关键字附加到URL 并获取集群健康状况。执行以下代码并获得响应:
复制代码
GET http://localhost:9200/_cluster/health
{ }
响应
您将得到如下响应的输出。
{
	"cluster_name": "elasticsearch",
	"status": "yellow",
	"timed_out": false,
	"number_of_nodes": 1,
	"number_of_data_nodes": 1,
	"active_primary_nodes": 4,
	"active_shards": 4,
	"relocating_shards": 0,
	"initializing_shards": 0,
	"unassigned_shards": 4,
	"delayed_unassigned_shards": 0,
	"number_of_pending_tasks": 0,
	"number_of_in_flight_fetch": 0,
	"task_max_waiting_in_queue_millis": 0,
	"active_shards_percent_as_number": 50
}
屏幕截图
Elasticsearch Cluster APIs

集群状态

该API用于获取集群状态信息。状态信息包含集群名称、state_uuid、版本、主节点、元数据、路由表、块等。
附加 _cluster 和 状态关键字,获取集群状态信息。执行以下代码,获取状态信息:
Copy Code
GET http://localhost:9200/_cluster/state
响应
您将得到如下响应的输出。
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
	"cluster_name": "elasticsearch",
	"cluster_uuid": "goGjM-AcQJyB8gb3-tC1KA",
	"version": 230,
	"state_uuid": "f2CIE4YhT8WBW4Dg6NJrjA",
	"master_node": "DhlQVbbYS-SDo0HJC9pGjw",
	"blocks": { }
	"nodes": {
	      "DhlQVbbYS-SDo0HJC9pGjw": {
	      "name": "DESKTOP-MAJMT36",
	      "ephemeral_id": "NnB3ZSyPQJ-V-J2n4eENXA",
	      "transport_address": "127.0.0.1:9300",
	      "attributes": {
		"ml.machine_memory": "4194447360",
		"xpack.installed": "true",
		"transform.node": "true",
		"ml.max_open_jobs": "20"
	         }
	    }
          },
	"metadata": {
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
屏幕截图
Elasticsearch Cluster APIs

集群统计数据

统计数据代表统计数据。通过使用 stats 关键字,我们可以检索有关集群的统计信息。它返回有关集群的统计信息以及分片数量、内存使用情况、存储大小、角色、操作系统、节点数量和文件系统。
附加 _cluster 和 stats关键字,获取集群统计信息。执行如下代码,获取集群的统计信息:
复制代码
GET http://localhost:9200/_cluster/stats
{ }
响应
您将得到如下响应的输出。
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
	"cluster_name": "elasticsearch",
	"cluster_uuid": "goGjM-AcQJyB8gb3-tC1KA",
	"timestamp": 1595314120010,
	"status": "yellow",
	"indices": {
	       "count": 4,
	       "primaries": 4,
	       "replication": 0,
	       "index": {
	      	 "shards": {
		      "min": 1,
		      "max": 1,
		      "avg": 1
		},
		"primaries": {
		      "min": 1,
		      "max": 1,
		      "avg": 1
		},
		"replication": {
		      "min": 1,
		      "max": 1,
		      "avg": 1
		}
	    }
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
屏幕截图
Elasticsearch Cluster APIs

挂起的集群任务

挂起的集群任务 API 管理任何集群中的所有挂起的任务。它监控创建索引、分配或失败分片以及更新映射等任务。要监控这些任务,请在 URL 中附加 _cluster 和 pending_tasks 并获取所有剩余的/集群中的待处理任务。复制以下代码并执行查询:
复制代码
GET http://localhost:9200/_cluster/pending_tasks
{ }
响应
如果集群中没有挂起的任务,您将得到如下响应的输出。
{
         "tasks": [ ]	
}
屏幕截图
Elasticsearch Cluster APIs

集群重新路由

集群重新路由 API 负责将分片从一个节点移动到另一个节点。它还可以分配任何未分配的分片并取消任何分片的分配。要成功执行此查询,我们需要使用 POST 请求方法。查看下面的示例并在您的系统上执行它:
复制代码
GET http://localhost:9200/_cluster/reroute
{ }
响应
您将得到如下响应的输出。
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
         "routing table": {
	"indices": {
	    "new_student": {
	         "shards": {
		"0": [
		{	
		       "state": "STARTED",
		        "primary": true,
		       "node": "DhlQVbbYS-SDo0HJC9pGjw",
		       "relocating_node": null,
		       "shard": 0,
		       "index": "new_student",
		        "allocation_id": {
			"id": "Crrs3vwTT92n-NLyb_AJTA"
		        }
		   }
		   "state": "UNASSIGNED",
		   "primary": false,
		   "node": null,
		   "relocating_node": null,
		   "shard": 0,
		   "index": "new_student",
		   "recovery_source": {
			"type": "PEER"
		     },
		    "unassigned_info": {
			"reason": "CLUSTER_RECOVERED",
			"at": "2020-07-21T05:53:05.282Z",
			"delayed": false,
			"allocated_status": "no_attempt"
		     }
		}
	       ]
  }
          },
          "student1": {
	     "shards": {
		"0": [
		{	
		       "state": "STARTED",
		        "primary": true,
		       "node": "DhlQVbbYS-SDo0HJC9pGjw",
		       "relocating_node": null,
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
屏幕截图
Elasticsearch Cluster APIs

集群更新设置

此 API 有助于更新集群的设置。使用 settings 关键字更新集群设置。大多数集群级别的 API 允许用户指定要执行的节点。有两种类型的设置-
持久-它在整个重启过程中应用。 瞬态-在集群完全重启后无法存活。
执行以下代码并得到响应:
复制代码
GET http://localhost:9200/_cluster/settings/
{ }
Response
通过执行上述查询,您将得到如下响应的输出。
{  
         	"persistent": { },
	"transient": { }
}
屏幕截图
Elasticsearch Cluster APIs
Persistent API
我们可以将设置更新为持久设置。下面是一个持久更新的例子:
复制代码
PUT http://localhost:9200/_cluster/settings/
{ 
    "persistent": {
        "indices.recovery.max_bytes_per_sec": "30mb"
    }
}
Response
通过执行上述查询,您将获得如下响应的输出。
{  
         	"acknowledged": true,
	"persistent": { 
	    "indices": {
	          "recovery": {
		   "max_bytes_per_sec": "30mb"
		}
	       }
	},
	"transient": { }
}
屏幕截图
Elasticsearch Cluster APIs
瞬态 API
我们可以将设置更新为瞬态设置。下面是一个持久更新的例子:
复制代码
PUT http://localhost:9200
{ 
    "transient": {
        "indices.recovery.max_bytes_per_sec": "50mb"
    }
}
响应
通过执行上述查询,您将得到如下响应的输出。
{  
         	"acknowledged": true,
	"persistent": { },
	"transient": {
		   "indicies.recovery.max_bytes_per_sec": "50mb"
	}
}
屏幕截图
Elasticsearch Cluster APIs

Node Stats

节点统计 API 有助于检索有关集群节点的统计信息。节点统计信息和集群统计信息几乎相同。我们可以检索任意数量节点的统计信息。要检索节点的统计信息,请在 URL 中附加 _stats 关键字和 _nodes 关键字。
复制以下代码并在您的系统上执行查询:
复制代码
GET http://localhost:9200/_nodes/stats/
{ }
Response
通过执行上述查询,您将得到如下响应的输出。
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
	"cluster_name": "elasticsearch",
	"nodes": {
	       "DhlQVbbYS-SDo0HJC9pGjw": {
		"timestamp": 1595403071348,
            "name": "DESKTOP_MAJMT36",
		"transport_address": "127.0.0.1:9300",
		"host": "127.0.0.1",
		"ip": "127.0.0.1:9300",
		"roles": [
		    "data" ,
		    "ingest" ,
		    "master" ,
		    "ml" ,
		    "remote_cluster_client" ,
		    "transform" 
		]
		"attribute": {
		     "ml.machine.memory": "4194447360"
		     "xpack.installed": "true",
		     "transform.node": "true",
		     "ml.max_open_jobs": "20"
		},
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
屏幕截图
您将获得与以下屏幕截图相同的输出。
Elasticsearch Cluster APIs

节点hot_threads

该API负责检索每个节点当前的热点线程信息在一个集群中。要检索每个节点上的热线程信息,请在 URL 中附加 hot_threads 关键字和 _nodes 关键字,并在响应正文中获取响应。 GET 方法用于发送此http 请求。
复制以下代码并在您的系统上执行查询。完全执行需要一点时间。
复制代码
GET http://localhost:9200/_nodes/hot_threads/
{ }
Response
通过执行上述查询,您将得到如下响应的输出。
{
"_nodes": {
   "total": 0,
   "successful": 0,
   "failed": 0
},	
"cluster_name": "elasticsearch",
    "nodes": { }
} 
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4