ElasticSearch5.3插件开发(二)获取集群健康信息
来源:互联网 发布:军训教官 知乎 编辑:程序博客网 时间:2024/06/18 01:43
前面一篇,简单讲过插件开发,打印信息,今天我们开发一个有实际意义的插件,获得集群健康信息。
先看代码
MyFirstPlugin.java
package es.plugins;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin;
import java.util.Collections;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.rest.RestHandler;
public class MyFirstPlugin extends Plugin implements ActionPlugin{
private final static Logger LOGGER = LogManager.getLogger(MyFirstPlugin.class);
public MyFirstPlugin() {
super();
LOGGER.info("This is my fisrt Plugin");
LOGGER.info("This is my fisrt Plugin");
LOGGER.info("This is my fisrt Plugin");
LOGGER.info("This is my fisrt Plugin");
LOGGER.info("This is my fisrt Plugin");
LOGGER.info("This is my fisrt Plugin");
LOGGER.info("This is my fisrt Plugin");
}
public List<Class<? extends RestHandler>> getRestHandlers() {
return Collections.singletonList(MyRestAction.class);
}
}
MyRestAction.java
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package es.plugins;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import java.util.Locale;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Table;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestResponseListener;
import org.elasticsearch.rest.action.cat.AbstractCatAction;
import org.elasticsearch.rest.action.cat.RestTable;
public class MyRestAction extends AbstractCatAction {
public MyRestAction(Settings settings, RestController controller) {
super(settings);
controller.registerHandler(GET, "/_cat/health", this);
}
@Override
protected void documentation(StringBuilder sb) {
sb.append("/_cat/health\n");
}
@Override
public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
ClusterHealthRequest clusterHealthRequest = new ClusterHealthRequest();
return channel -> client.admin().cluster().health(clusterHealthRequest, new RestResponseListener<ClusterHealthResponse>(channel) {
@Override
public RestResponse buildResponse(final ClusterHealthResponse health) throws Exception {
return RestTable.buildResponse(buildTable(health, request), channel);
}
});
}
@Override
protected Table getTableWithHeader(final RestRequest request) {
Table t = new Table();
t.startHeadersWithTimestamp();
t.addCell("cluster", "alias:cl;desc:cluster name");
t.addCell("status", "alias:st;desc:health status");
t.addCell("node.total", "alias:nt,nodeTotal;text-align:right;desc:total number of nodes");
t.addCell("node.data", "alias:nd,nodeData;text-align:right;desc:number of nodes that can store data");
t.addCell("shards", "alias:t,sh,shards.total,shardsTotal;text-align:right;desc:total number of shards");
t.addCell("pri", "alias:p,shards.primary,shardsPrimary;text-align:right;desc:number of primary shards");
t.addCell("relo", "alias:r,shards.relocating,shardsRelocating;text-align:right;desc:number of relocating nodes");
t.addCell("init", "alias:i,shards.initializing,shardsInitializing;text-align:right;desc:number of initializing nodes");
t.addCell("unassign", "alias:u,shards.unassigned,shardsUnassigned;text-align:right;desc:number of unassigned shards");
t.addCell("pending_tasks", "alias:pt,pendingTasks;text-align:right;desc:number of pending tasks");
t.addCell("max_task_wait_time", "alias:mtwt,maxTaskWaitTime;text-align:right;desc:wait time of longest task pending");
t.addCell("active_shards_percent", "alias:asp,activeShardsPercent;text-align:right;desc:active number of shards in percent");
t.endHeaders();
return t;
}
private Table buildTable(final ClusterHealthResponse health, final RestRequest request) {
Table t = getTableWithHeader(request);
t.startRow();
t.addCell(health.getClusterName());
t.addCell(health.getStatus().name().toLowerCase(Locale.ROOT));
t.addCell(health.getNumberOfNodes());
t.addCell(health.getNumberOfDataNodes());
t.addCell(health.getActiveShards());
t.addCell(health.getActivePrimaryShards());
t.addCell(health.getRelocatingShards());
t.addCell(health.getInitializingShards());
t.addCell(health.getUnassignedShards());
t.addCell(health.getNumberOfPendingTasks());
t.addCell(health.getTaskMaxWaitingTime().millis() == 0 ? "-" : health.getTaskMaxWaitingTime());
t.addCell(String.format(Locale.ROOT, "%1.1f%%", health.getActiveShardsPercent()));
t.endRow();
return t;
}
}
打包成jar包,在plugin目录下新建一个文件夹与插件同名MyFirstPlugin,然后放入jar包,添加配置文件plugin-descriptor.properties
文件内容如下
description=hello for ElasticSearch
version=1.0
name=HelloWorldPlugin
site=${elasticsearch.plugin.site}
jvm=true
classname=org.elasticsearch.plugin.helloworld.MyFirstPlugin //类全名
java.version=1.7
elasticsearch.version=2.2.0
isolated=${elasticsearch.plugin.isolated}
通过浏览器访问:http://localhost:9200/_cat/health
获得信息:
1495415968 09:19:28 elasticsearch yellow 1 1 5 5 0 0 5 0 - 50.0%
- ElasticSearch5.3插件开发(二)获取集群健康信息
- ElasticSearch5.3插件开发(一)控制台打印信息
- elasticsearch5.2.2 插件开发(二) 第一个有实际功能的插件
- elasticsearch5.2.2 插件开发(一)
- elasticsearch5.3linux集群
- Elasticsearch5.4集群(二)java API升级
- Elasticsearch5.5.1插件开发指南
- rcp(插件开发) 获取插件版本信息
- elasticsearch5.2.2 插件开发(三)ScriptPlugin 的实现
- 怒写一篇基于Windows10的最新ElasticSearch5.3的搜索引擎的开发博客( 二)
- elasticsearch5.0集群+kibana5.0+head插件插件的安装
- ElasticSearch5.3安装Head插件
- elasticsearch5.3安装插件head
- elasticsearch5.x集群搭建,ik分词器,head插件
- elasticsearch5.3集群 java 调用实例
- Centos7.3部署Elasticsearch5.4.0集群
- Elasticsearch5.4集群(一)安装部署
- Elasticsearch5.4集群(一)安装部署
- error c3872:"0Xa0":此字符不允许在标识符中使用
- rocketmq集群搭建(多Master多Slave模式异步复制)
- 微信小程序 开发列表
- iOS 开发中经常使用的到宏
- Spring JdbcTemplate使用实例
- ElasticSearch5.3插件开发(二)获取集群健康信息
- 微信小程序ajax请求到自己的网站
- unittest 中用于跳过 test method, test class,的相关装饰器
- 最详细的Log4教程
- 配置ADS,使ADS编译出bin文件,并使用uboot下载运行成功
- 二分图——洛谷P3386 【模板】二分图匹
- CodeForces 810C Do you want a date? 【数学】【排序】
- 漫画赏析:Linux 内核到底长啥样
- Django 博客开发教程 16