1. 程式人生 > >elasticsearch5.2.2 外掛開發(二) 第一個有實際功能的外掛

elasticsearch5.2.2 外掛開發(二) 第一個有實際功能的外掛

實現的功能也非常簡單。擴充套件ES的API功能。比如你ES裝在本地(預設埠),訪問http://127.0.0.1:9200/_mytest1  則返回我們的資訊,訪問http://127.0.0.1:9200/_mytest1 /myaction則返回帶myaction的資訊。

程式碼如下

import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.AnalysisPlugin;
import org.elasticsearch.plugins.Plugin;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.rest.RestHandler;


import java.util.Collections;
import java.util.List;
public class MyFirstPlugin extends Plugin implements ActionPlugin{
    private final static Logger LOGGER = LogManager.getLogger(MyFirstPlugin.class);
    public MyFirstPlugin() {
        super();
        LOGGER.warn("Create the Basic Plugin and installed it into elasticsearch");
    }
    @Override
    public List<Class<? extends RestHandler>> getRestHandlers() {
        return Collections.singletonList(MyRestAction.class);
    }
}
實際處理的程式碼
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.rest.action.RestBuilderListener;

import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;

public class MyRestAction  extends BaseRestHandler {
    private final static Logger LOGGER = LogManager.getLogger(MyRestAction.class);

    @Inject
    public MyRestAction(Settings settings, RestController controller) {
        super(settings);
        controller.registerHandler(GET, "_mytest1/{action}", this);
        controller.registerHandler(GET, "_mytest1", this);
    }

    @Override
    protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
        LOGGER.info("Handle _jettro endpoint");

        String action = request.param("action");
        String strInputAction = "exists";
        if (action != null) {
            LOGGER.info("do something action");
            return createDoSomethingResponse(request, client);
        } else {
            return createMessageResponse(request);
        }
    }

    private RestChannelConsumer createDoSomethingResponse(final RestRequest request, NodeClient client) {
        return channel -> {
            String action = request.param("action");
            Something message = new Something();
            message.action = action;
            XContentBuilder builder = channel.newBuilder();
            builder.startObject();
            message.toXContent(builder, request);
            builder.endObject();
            channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
        };
    }

    private RestChannelConsumer createMessageResponse(RestRequest request) {
        return channel -> {
            Message message = new Message();
            XContentBuilder builder = channel.newBuilder();
            builder.startObject();
            message.toXContent(builder, request);
            builder.endObject();
            channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
        };
    }


    class Message implements ToXContent {
        @Override
        public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
            return builder.field("message", "This is my first plugin");
        }
    }

    class Something implements ToXContent {
        public String action;

        @Override
        public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
            String strReturn;
            if (action != null)
            {
                strReturn = "I can do action " + action;
            }
            else
            {
                strReturn = "I can do anthing here. you order null";
            }
            return builder.field("some", strReturn);
        }
    }
}


然後先解除安裝上次的外掛

bin\elasticsearch-plugin remove myart1
重新執行安裝
mvn clean install
bin\elasticsearch-plugin install file:///D:\greesoft\elastic\myart1-5.2.2-SNAPSHOT.zip
啟動ES,然後用瀏覽器執行,就可以得到我們需要的結果了。非常簡單吧。