Elasticsearch自定义插件
生活随笔
收集整理的這篇文章主要介紹了
Elasticsearch自定义插件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
檢索引擎Elasticsearch支持插件模式,有些時候你可能需要安裝一些插件,甚至自己開發插件,這里就提供一個開始ES插件開發示例,ES版本為2.2.0。
自定義插件類繼承org.elasticsearch.plugins.Plugin
HelloWorldPlugin:
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 ~]$ 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自定义插件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Android App中集成Googl
- 下一篇: 数组转换为字符串方法