基于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 {
@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());
}
}
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();
}
}
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
site=${elasticsearch.plugin.site}
jvm=true
classname=org.elasticsearch.plugin.helloworld.HelloWorldPlugin
java.version=1.7
elasticsearch.version=2.2.0
isolated=${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"}