如何利用elasticsearch插件进行开发?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
检索引擎Elasticsearch支持插件模式。有些时候你可能须要安装一些插件。甚至自己开发插件,这里就提供一个開始ES插件开发演示样例,ES版本号为1.5.2。
一、插件类继承自org.elasticsearch.plugins.AbstractPlugin
package org.elasticsearch.plugin.helloworld;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.AbstractPlugin;
import org.elasticsearch.rest.RestModule;
public class HelloWorldPlugin extends AbstractPlugin {
final ESLogger logger = Loggers.getLogger(getClass());
@Override
public String name() {
//插件名称
return "HelloWorld";
}
@Override
public String description() {
//插件描写叙述
return "Hello World Plugin";
}
//处理模块,由于系统中有非常多种Module,所以须要对其类型进行推断
@Override
public void processModule(Module module) {
if(module instanceof RestModule) {
((RestModule)module).addRestAction(HelloWorldHandler.class);
}
if(module instanceof HelloModule) {
logger.info("############## process hello module #####################");
}
}
@Override
public Collection<Module> modules(Settings settings) {
//创建自己的模块集合
//假设没有自己定义模块,则能够返回空
HelloModule helloModule = new HelloModule();
ArrayList<Module> list = new ArrayList<>();
list.add(helloModule);
Collections.unmodifiableList(list);
return list;
}
@SuppressWarnings("rawtypes")
@Override
public Collection<Class<? extends LifecycleComponent>> services() {
//创建自己的服务类集合,服务类须要继承自LifecycleComponent。ES会自己主动创建出服务类实例,并调用其start方法
//假设没有自己定义服务类。则能够返回空
Collection<Class<?
extends LifecycleComponent>> list = new ArrayList<>();
list.add(HelloService.class);
return list;
}
}
Module类事实上就是定义了依赖注入规则。假设不清楚,能够去查看Google Guice的文档,基本上是一致的。如上例中的HelloModule:
package org.elasticsearch.plugin.helloworld;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Scopes;
public class HelloModule extends AbstractModule {
@Override
protected void configure() {
//将InjectableService接口类型绑定到InjectableServiceImpl实现类
//在须要注入InjectableService的地方,就会使用InjectableServiceImpl实例
bind(InjectableService.class).to(InjectableServiceImpl.class);
//使HelloService为单例状态
bind(HelloService.class).in(Scopes.SINGLETON);
}
}
不同的模块有不同的处理方式,比如样例中对于RestModule,加入了一个Handler:
package org.elasticsearch.plugin.helloworld;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
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;
import org.elasticsearch.rest.RestRequest.Method;
import org.elasticsearch.rest.RestResponse;
public class HelloWorldHandler extends BaseRestHandler {
//注入对象
@Inject
protected HelloWorldHandler(Settings settings, RestController controller, Client client) {
super(settings, controller, client);
//将该Handler绑定到某訪问路径
controller.registerHandler(Method.GET, "/hello/", this);
controller.registerHandler(Method.GET, "/hello/{name}", this);
}
//处理绑定路径的请求訪问
@Override
protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
logger.debug("HelloWorldAction.handleRequest called");
final String name = request.hasParam("name") ? request.param("name") : "world";
String content = "{\"success\":true, \"message\":\"hello " +name+ "\"}";
RestResponse response = new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, content);
channel.sendResponse(response);
}
}
最后在类路径根文件夹下加入一个名为es-plugin.properties属性文件,指定插件实现类:
plugin=org.elasticsearch.plugin.helloworld.HelloWorldPlugin
二、将插件打成jar包后安装
如果ES_HOME代表Elasticsearch安装文件夹。在ES_HOME/plugins文件夹下创建一个名为HelloWorld的文件夹。该文件夹名称必须与插件名称同样(区分大写和小写),然后将jar包拷贝至HelloWorld文件夹,又一次启动就可以,当你运行:
curl -GET localhost:9200/hello,就会返回对应结果了。
三、为插件加入页面
假设你想为你的插件加入訪问页面。则能够在ES_HOME/plugins/HelloWorld文件夹下创建一个名为"_site"的文件夹,该文件夹名称必须为_site,然后将对应的html页面放置进_site文件夹就可以,假设放置了一个名为index.html文件,则能够通过
localhost:9200/_plugin/HelloWorld/index.html进行訪问。
因为Elasticsearch提供了jsclientAPI。所以使用html静态页面与js就能够完毕对应的功能了。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。