日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ElasticSearch5.3插件开发(二)获取集群健康信息

發布時間:2024/1/23 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ElasticSearch5.3插件开发(二)获取集群健康信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前面一篇,簡單講過插件開發,打印信息,今天我們開發一個有實際意義的插件,獲得集群健康信息。

先看代碼

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插件开发(二)获取集群健康信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。