Elasticsearch Java API 的使用(23)—实现集群管理

集群管理
使用 ClusterHealthResponse 实现集群信息监测和管理
ClusterHealthResponse 的实例 healths 可调用的方法分析

  • 集群健康信息总览:healths.toString()
  • 集群名称:healths.getClusterName()
  • 集群状态:healths.getStatus()
  • 集群是否超时:healths.isTimedOut()
  • 集群的节点个数:healths.getNumberOfNodes()
  • 集群的数据节点个数:healths.getNumberOfDataNodes()
  • 集群的活跃私有分片个数:healths.getActivePrimaryShards()
  • 集群的活跃分片个数:healths.getActiveShards()
  • 集群的迁移分片个数:healths.getRelocatingShards()
  • 集群的初始化分片个数: healths.getInitializingShards()
  • 集群的未分配分片个数: healths.getUnassignedShards()
  • 集群的延迟未分配分片个数:healths.getDelayedUnassignedShards()
  • 集群的等待任务的个数:healths.getNumberOfPendingTasks()
  • 集群的快速查询数:healths.getNumberOfInFlightFetch()
  • 集群的任务最大队列等待毫秒数:healths.getTaskMaxWaitingTime()
  • 集群的活跃分片百分比:healths.getActiveShardsPercent()
  • 集群的索引的详细信息:healths.getIndices()
    • 它的结构 Map<Sirng, ClusterIndexHealth> 得键值对;
    • key 是索引,value 是包含索引健康信息的实例
public class EsClusterHealth throws UnknownHostException{
    public void ClusterHealth(TransportClient client){
       ClusterHealthResponse healths = client.admin().cluster().prepareHealth().get();
       //集群名称
       String clusterName = healths.getClusterName();
       System.out.println("clusterName="+clusterName);
	
	   //数据节点的个数
	   int numberOfDataNodes = healths.getNumberOfDateNodes();
	   System.out.println("numberOfDataNodes="+numberOfDataNodes);

	   //节点的个数
	   int numberOfNodes = healths.getNumberOfNodes();
	   System.out.println("numberOfNodes="+numberOfNodes);
		
	   for(ClusterIndexHealth health : healths.getIndices().value()){
		   //循环查看索引
		   String index = health.getIndex();
		   //查看索引的分片数
		   int numberOfShards = health.getNumberOfShards();
		   //查看索引的副本数
		   int numberOfReplicas = health.getNumberOfReplicas();
		   System.out.println("index=%s,numberOfShards=%d,numberOfReplicas=%d\n", index, numberOfShards, numberOfReplicas);
			
		   //查看集群集群状态
		   ClusterHealthStatus status = health.getStatus();
		   System.out.println(status.toString());
	   }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用ElasticsearchJava API连接Elasticsearch集群,需要添加以下三个依赖:org.elasticsearch.client.elasticsearch-rest-high-level-client、org.elasticsearch.client:elasticsearch-rest-client、org.elasticsearch:elasticsearch。这些依赖可以通过Maven或Gradle添加到项目中。接下来,可以使用ElasticsearchJava API编写代码来连接Elasticsearch集群。具体步骤如下: 1. 创建RestHighLevelClient对象,该对象是与Elasticsearch集群进行通信的主要入口点。 2. 创建IndexRequest对象,该对象表示要索引的文档。 3. 使用IndexRequest对象设置索引名称、文档类型和文档ID等信息。 4. 使用IndexRequest对象设置要索引的文档内容。 5. 使用RestHighLevelClient对象执行IndexRequest对象,将文档索引到Elasticsearch集群中。 下面是一个示例代码,用于将文档索引到Elasticsearch集群中: ```java import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.rest.RestStatus; import java.io.IOException; public class ElasticsearchExample { private RestHighLevelClient client; public ElasticsearchExample(RestHighLevelClient client) { this.client = client; } public void indexDocument(String index, String type, String id, String json) throws IOException { IndexRequest request = new IndexRequest(index, type, id); request.source(json, XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); if (response.status() == RestStatus.CREATED) { System.out.println("Document indexed successfully"); } else { System.out.println("Failed to index document"); } } public static void main(String[] args) throws IOException { RestHighLevelClient client = new RestHighLevelClient(/* Elasticsearch连接配置 */); ElasticsearchExample example = new ElasticsearchExample(client); example.indexDocument("my_index", "my_type", "1", "{\"name\":\"John Doe\",\"age\":25}"); client.close(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值