Elasticsearch自定义插件
检索引擎Elasticsearch支持插件模式,有些时候你可能需要安装一些插件,甚至自己开发插件,这里就提供一个开始ES插件开发示例,ES版本为2.2.0。
自定义插件类继承org.elasticsearch.plugins.Plugin
HelloWorldPlugin:
package org.elasticsearch.plugin.helloworld;import java.util.Collection;
import java.util.Collections; import org.elasticsearch.common.inject.Module;
import org.elasticsearch.plugins.Plugin; public class HelloWorldPlugin extends Plugin { @Override public String name() { return "hello-world"; } @Override public String description() { return "hello-world"; } @Override public Collection<Module> nodeModules() { //加入自定义处理模块 return Collections.<Module> singletonList(new HelloWorldModule()); } }
HelloWorldModule:
package org.elasticsearch.plugin.helloworld;import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.rest.action.helloworld.HelloWorldAction; public class HelloWorldModule extends AbstractModule { @Override protected void configure() { bind(HelloWorldAction.class).asEagerSingleton(); }
}
HelloWorldAction:
package org.elasticsearch.rest.action.helloworld;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"; @Inject public HelloWorldAction(Settings settings, Client client, RestController controller) { super(settings, controller, client); // Define REST endpoints controller.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 exists if (request.hasContent()) { contentType = XContentFactory.xContentType(request.content()); } } if (contentType == null) { // default to JSON contentType = 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; } @Override protected 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>() { @Override public 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); } } @Override public 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 ElasticSearch
version=1.0
name=HelloWorldPlugin
jvm=true
classname=org.elasticsearch.plugin.helloworld.HelloWorldPlugin
java.version=1.7(当时我这里写的是1.7但实际装的是jdk1.8.0_91倒也能正常运行)
elasticsearch.version=2.2.0
重启es,插件便安装成功了。
注意:在编写plugin-descriptor.properties文件的时候每行的后面一定不要有空格,我一开始就是直接粘贴过去的,结果每行末尾都有两个空格,重启es会报下面这个错,耗费了很长时间,这里吐槽一下CSDN,当你在它的文本编辑器中将这个josn复制过去再展示的时候你会发现每行后面自动就会有两个空格,真是坑死我了!
[hadoop@h153 ~]$ ./elasticsearch-2.2.0/bin/elasticsearch
[2017-10-12 01:19:08,902][INFO ][node ] [node-1] version[2.2.0], pid[8548], build[8ff36d1/2016-01-27T13:32:39Z]
[2017-10-12 01:19:08,903][INFO ][node ] [node-1] initializing ...
Exception in thread "main" ElasticsearchException[Could not find plugin class [org.elasticsearch.plugin.helloworld.HelloWorldPlugin]]; nested: ClassNotFoundException[org.elasticsearch.plugin.helloworld.HelloWorldPlugin];
Likely root cause: java.lang.ClassNotFoundException: org.elasticsearch.plugin.helloworld.HelloWorldPluginat java.net.URLClassLoader.findClass(URLClassLoader.java:381)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:814)at java.lang.ClassLoader.loadClass(ClassLoader.java:357)at org.elasticsearch.plugins.PluginsService.loadPluginClass(PluginsService.java:463)at org.elasticsearch.plugins.PluginsService.loadBundles(PluginsService.java:431)at org.elasticsearch.plugins.PluginsService.<init>(PluginsService.java:129)at org.elasticsearch.node.Node.<init>(Node.java:146)at org.elasticsearch.node.Node.<init>(Node.java:128)at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:145)at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:178)at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:285)at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35)
Refer to the log for complete error details.
下面进行插件的验证:
先创建相应的索引:
[hadoop@h153 ~]$ curl -XPOST http://192.168.205.153:9200/example
{"acknowledged":true}[hadoop@h153 ~]$ curl -XGET http://192.168.205.153:9200/_hello
{"hello":"world","greeting":"Sorry, do I know you?"}[hadoop@h153 ~]$ curl -XGET http://192.168.205.153:9200/_hello/xmine
{"hello":"xmine","greeting":"Sorry, do I know you?"}[hadoop@h153 ~]$ curl -XPUT http://192.168.205.153:9200/example/person/xmine?pretty -d '{"msg":"elasticsearch"}'
{"_index" : "example","_type" : "person","_id" : "xmine","_version" : 1,"_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"created" : true
}
[hadoop@h153 ~]$ curl -XGET http://192.168.205.153:9200/_hello/xmine
{"hello":"xmine","greeting":"elasticsearch"}
补充:为插件添加页面
如果你想为你的插件添加访问页面,则可以在ES_HOME/plugins/HelloWorld目录下创建一个名为"_site"的目录,该目录名称必须为_site,然后将相应的html页面放置进_site目录即可,如果放置了一个名为index.html文件,则可以通过localhost:9200/_plugin/HelloWorld/index.html进行访问。
由于Elasticsearch提供了js客户端API,所以使用html静态页面与js就可以完成相应的功能了。
参考:
http://blog.csdn.net/xtayfjpk/article/details/47005219
http://blog.csdn.net/l253272670/article/details/54141169
https://stackoverflow.com/questions/33538903/elasticsearch-2-0-plugin-installation-info
Elasticsearch自定义插件相关推荐
- elasticsearch -- head插件
2019独角兽企业重金招聘Python工程师标准>>> elasticsearch head插件是一个入门级的elasticsearch前端插件:我们来安装下: 第一步:安装node ...
- vue里面_Vue中如何使用自定义插件(plugin)
Vue中如何使用自定义插件(plugin) 1.在根目录src下创建一个libs文件夹,在libs文件夹下面创建一个myPlugins文件夹,用来存放我们的自定义插件,在myPlugins文件夹下面再 ...
- openstack页面自定义插件使用详解(django、ajax、post)(zTree为例)
2019独角兽企业重金招聘Python工程师标准>>> 感谢朋友支持本博客,欢迎共同探讨交流,由于能力和时间有限,错误之处在所难免,欢迎指正! 如有转载,请保留源作者博客信息. Be ...
- elasticsearch 自定义_id
elasticsearch 自定义ID: curl -s -XPUT localhost:9200/web -d ' {"mappings": {"blog": ...
- JQuery自定义插件详解之Banner图滚动插件
前 言 JRedu JQuery是什么相信已经不需要详细介绍了.作为时下最火的JS库之一,JQuery将其"Write Less,Do More!"的口号发挥的极致.而帮助J ...
- idea mybatis generator插件_Mybatis使用自定义插件去掉POJO的Getter和Setter方法
Mybatis使用自定义插件去掉POJO的Getter和Setter方法
- Cordova学习--iOS自定义插件
上一篇文章中我们已经成功创建了一个App,在这一篇中,我们实现自定义原生插件,由js调用原生插件.在这里我们实现功能如下 一.创建插件文件 在plugins文件夹下创建插件EchoPlugin,继承自 ...
- MyBatis 插件原理与自定义插件-代理和拦截是怎么实现的?
问题1:四大对象什么时候被代理,也就是:代理对象是什么时候创建的? 问题2:多个插件的情况下,代理能不能被代理?代理顺序和调用顺序的关系? 问题3:谁来创建代理对象? 问题4:被代理后,调用的是什么方 ...
- MyBatis 插件原理与自定义插件-插件编写与注册
(基于spring-mybatis)运行自定义的插件,需要3 步,我们以PageHelper 为 1.编写自己的插件类 1)实现Interceptor 接口 这个是所有的插件必须实现的接口. 2)添加 ...
- Vue012_ 自定义插件
自定义插件 说明 1) Vue 插件是一个包含 install 方法的对象 2) 通过 install 方法给 Vue 或 Vue 实例添加方法, 定义全局指令等 代码展示: vue-myPlugin ...
最新文章
- 计算机用什么方式 管理程序和数据,计算机与外设之间数据传送方式有几种?各有什么特点?...
- Hadoop文件系统元数据fsimage和编辑日志edits
- Linux 2.6 下通过 ptrace 和 plt 实现用户态 API Hook
- 成功解决No handles with labels found to put in legend.
- ldap导入mysql_openLDAP 部署(亲测可用)
- mysql show命令原理_MySQL show processlist;命令详解
- HTML5期末大作业:关于旅游主题网站设计——开心网旅游网页源码(15页) HTML+CSS+JavaScript
- Unity 获取GPS经纬度
- html 动态导航菜单,导航菜单,css3,javascript,响应式菜单,html,css
- 广东取消英语和计算机考试,广东停考全国英语等级考试、全国计算机等级考试...
- 为什么我们不能坚持到底?
- 微信小程序开发者工具的使用
- 中国人不骗中国人,我的猜拳平平无奇。。
- MAC word2009 压缩图片大小转pdf
- association的使用
- rewrite break
- 元宇宙产业委共同主席倪健中:打开元宇宙的潘多拉魔盒,释放元宇宙产业无限的想象与发展空间|平安银行元宇宙与新终端创新沙龙
- python杂碎合集
- java抓取页面表格_用java实现爬虫抓取网页中的表格数据功能源码
- C#解析ASTM1394-97协议数据
热门文章
- TTL与CMOS电路
- cadz轴归零命令_CAD的Z轴归零的插件分享
- android textwatcher 延时,Android TextWatcher监控EditText中的输入内容并限制其个数
- 信捷XC系列PLC编程软件安装方法
- 关于计算机软件系统的知识,会计电算化知识点:计算机软件系统
- oracle性能优化 pdf,Oracle性能优化技术内幕 pdg-pdf
- 新书介绍:CCNA基础教程
- 【雷达与对抗】【2004.05】合成孔径雷达X波段发射机和频率分配单元的设计与实现
- springboot easyexcel 导出excel案例及文件无法打开
- 360安全卫士怎么打开加速球