日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Elasticsearch自定义插件

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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自定义插件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。