Elasticsearch自定义插件开发

来源:互联网 发布:淘宝airbnb优惠靠谱吗 编辑:程序博客网 时间:2024/05/20 21:19

基于Elasticsearch版本2.2.0的自定义插件开发


自定义插件类继承org.elasticsearch.plugins.Plugin

import java.util.Collection;import java.util.Collections;import org.elasticsearch.common.inject.Module;import org.elasticsearch.plugins.Plugin;public class HelloWorldPlugin extends Plugin {@Overridepublic String name() {return "hello-world";}@Overridepublic String description() {return "hello-world";}@Overridepublic Collection<Module> nodeModules() {//加入自定义处理模块return Collections.<Module> singletonList(new HelloWorldModule());}}

import org.elasticsearch.common.inject.AbstractModule;import org.elasticsearch.rest.action.helloworld.HelloWorldAction;public class HelloWorldModule extends AbstractModule {@Overrideprotected void configure() {bind(HelloWorldAction.class).asEagerSingleton();}}
import static org.elasticsearch.rest.RestRequest.Method.GET;import java.io.IOException;import org.elasticsearch.action.ActionListener;import org.elasticsearch.action.get.GetRequest;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.client.Client;import org.elasticsearch.common.inject.Inject;import org.elasticsearch.common.io.stream.BytesStreamOutput;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.xcontent.XContentBuilder;import org.elasticsearch.common.xcontent.XContentBuilderString;import org.elasticsearch.common.xcontent.XContentFactory;import org.elasticsearch.common.xcontent.XContentType;import org.elasticsearch.index.get.GetField;import org.elasticsearch.rest.BaseRestHandler;import org.elasticsearch.rest.BytesRestResponse;import org.elasticsearch.rest.RestChannel;import org.elasticsearch.rest.RestController;import org.elasticsearch.rest.RestRequest;import org.elasticsearch.rest.RestStatus;public class HelloWorldAction extends BaseRestHandler {public static String INDEX = "example";public static String TYPE = "person";@Injectpublic HelloWorldAction(Settings settings, Client client,RestController controller) {super(settings, controller, client);// Define REST endpointscontroller.registerHandler(GET, "/_hello/", this);controller.registerHandler(GET, "/_hello/{name}", this);}public static XContentBuilder restContentBuilder(RestRequest request)throws IOException {XContentType contentType = XContentType.fromRestContentType(request.header("Content-Type"));if (contentType == null) {// try and guess it from the body, if existsif (request.hasContent()) {contentType = XContentFactory.xContentType(request.content());}}if (contentType == null) {// default to JSONcontentType = XContentType.JSON;}BytesStreamOutput out = new BytesStreamOutput();XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(contentType), out);if (request.paramAsBoolean("pretty", false)) {builder.prettyPrint();}String casing = request.param("case");if (casing != null && "camelCase".equals(casing)) {builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.CAMELCASE);} else {builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.NONE);}return builder;}@Overrideprotected void handleRequest(final RestRequest request,final RestChannel channel, Client client) throws Exception {logger.info("HelloWorldAction.handleRequest called");final String name = request.hasParam("name")? request.param("name"): "world";logger.info("name={}", name);final GetRequest getRequest = new GetRequest(INDEX, TYPE, name);getRequest.operationThreaded(true);String[] fields = {"msg"};getRequest.fields(fields);client.get(getRequest, new ActionListener<GetResponse>() {@Overridepublic void onResponse(GetResponse response) {try {XContentBuilder builder = restContentBuilder(request);GetField field = response.getField("msg");String greeting = (field != null)? (String) field.getValues().get(0): "Sorry, do I know you?";builder.startObject().field(new XContentBuilderString("hello"), name).field(new XContentBuilderString("greeting"),greeting).endObject();if (!response.isExists()) {channel.sendResponse(new BytesRestResponse(RestStatus.NOT_FOUND, builder));} else {channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));}} catch (Exception e) {onFailure(e);}}@Overridepublic void onFailure(Throwable e) {try {channel.sendResponse(new BytesRestResponse(channel, RestStatus.OK, e));} catch (IOException e1) {logger.error("Failed to send failure response", e1);}}});}}

打包后j将jar包上传至${ES_HOME}/plugins/HelloWorld目录下(新建HelloWorld

HelloWorld目录下新建文件plugin-descriptor.properties,文件内容如下

description=hello for ElasticSearchversion=1.0name=HelloWorldPluginsite=${elasticsearch.plugin.site}jvm=trueclassname=org.elasticsearch.plugin.helloworld.HelloWorldPluginjava.version=1.7elasticsearch.version=2.2.0isolated=${elasticsearch.plugin.isolated}

重启es,插件便安装成功了。


下面进行插件的验证

输入命令:

curl -XGET http://localhost:9200/_hello

结果如下:

{"hello":"world","greeting":"Sorry, do I know you?"}


输入命令:

curl -XGET http://localhost:9200/_hello/xmine

结果:

{"hello":"xmine","greeting":"Sorry, do I know you?"}


输入命令:

curl -XPUT  http://10.1.1.127:9200/example/person/xmine?pretty -d '{"msg":"elasticsearch"}'

curl -XGET http://10.1.1.127:9200/_hello/xmine

结果

{"hello":"xmine","greeting":"elasticsearch"}


1 0