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
- Elasticsearch自定义插件开发
- elasticsearch 自定义similarity 插件开发
- elasticsearch 自定义similarity 插件开发
- Elasticsearch自定义分词插件
- Elasticsearch自定义插件
- elasticsearch插件开发
- intellij 开发调试elasticsearch插件
- ElasticSearch自定义分析器-集成结巴分词插件
- ElasticSearch自定义分析器-集成结巴分词插件
- ElasticSearch自定义分析器-集成结巴分词插件
- ElasticSearch自定义分析器-集成结巴分词插件
- ElasticSearch的ik分词插件开发
- [源码]Elasticsearch源码6(5.4插件开发)
- jquery 自定义插件开发
- jquery 自定义插件开发
- cordova开发自定义插件
- maven 自定义插件开发
- Cordova 开发自定义插件
- 正则表达式之量词与匹配模式
- spring+mybatis返回json日期处理(日期全局处理相差8个小时解决方法)
- Android通用网络请求解析框架.6(自定义解析器)
- jquery 模拟点击 a 连接
- 哈哈 我什么也不知道
- Elasticsearch自定义插件开发
- 2016总结——在路上,我走到哪儿了?
- php5.6新特性
- windows系统oracle数据导入
- Spring mvc+freeMarker 使用jsp自定义标签
- SpringMVC注解说明
- Android通用网络请求解析框架.7(同步请求,公共部分)
- 高级java的招聘要求
- 用Java对JSON字符串进行格式化输出