Elasticsearch Watcher プラグインに Java API を使用しようとしています。というわけで、Watcher JAVA PUT APIを使って新しいwatchを作成する方法です。
public static void createNew(Alert reqAlert) {
WatcherClient watcherClient = watchClient.createAndGetClient(reqAlert.getCluster());
WatchSourceBuilder watchSourceBuilder = WatchSourceBuilders.watchBuilder();
watchSourceBuilder.trigger(TriggerBuilders.schedule(Schedules.cron(getcronString(reqAlert.getInterval()))));
String esQuery = getQuery(reqAlert);
System.out.println(esQuery);
SearchRequest request = Requests.searchRequest("idx").source(esQuery);
watchSourceBuilder.input(new SearchInput(request, null, null, null));
watchSourceBuilder.condition(new ScriptCondition(Script.inline("ctx.payload.hits.total > 1").build()));
EmailTemplate.Builder emailBuilder = EmailTemplate.builder();
emailBuilder.to(reqAlert.getEmail());
emailBuilder.subject(reqAlert.getSubject());
emailBuilder.textBody(reqAlert.getBody());
System.out.println("Input and COndition done");
EmailAction.Builder emailActionBuilder = EmailAction.builder(emailBuilder.build());
watchSourceBuilder.addAction("email", emailActionBuilder);
System.out.println("Email action done");
PutWatchResponse putWatchResponse = watcherClient.preparePutWatch(reqAlert.getAlertName())
.setSource(watchSourceBuilder)
.get();
System.out.println("All done");
}
関数 getQuery の実装は次のとおりです。
public static String getQuery(Alert reqAlert) {
Map main = new LinkedHashMap();
Map query = new LinkedHashMap();
Map bool = new LinkedHashMap();
Map must = new LinkedHashMap();
Map match = new LinkedHashMap();
Map filter = new LinkedHashMap();
Map range = new LinkedHashMap();
Map epoch = new LinkedHashMap();
epoch.put("gte", "1454964688008");
range.put("epoch", epoch);
filter.put("range", range);
match.put("action", reqAlert.getQueryString());
must.put("match", match);
must.put("filter", filter);
bool.put("must", must);
query.put("bool", bool);
main.put("query", query);
JSONObject mainObject = new JSONObject(main);
return mainObject.toString();
}
org.codehaus.jettison
JSONのキーのシーケンスを保持するのに役立つLinkedHashMapを使用しているため、このJSONオブジェクトを使用しています。(これは必須要件です。キーの順序を維持する必要があります)。
私のpom.xml(必要な場合)(必要なものだけを表示しています):
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>watcher</artifactId>
<version>${es.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${es.version}</version>
</dependency>
実行すると、次のようになります。
{"query":{"bool":{"must":{"match":{"action":"HI"},"filter":{"range":{"epoch":{"gte":"1454964688008"}}}}}}}
Input and COndition done
Email action done
May 20, 2016 11:29:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Rest service] in context with path [/alerting] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW] with root cause
java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW
at org.elasticsearch.common.xcontent.json.JsonXContent.<clinit>(JsonXContent.java:49)
at org.elasticsearch.common.xcontent.XContentFactory.contentBuilder(XContentFactory.java:122)
at org.elasticsearch.watcher.client.WatchSourceBuilder.buildAsBytes(WatchSourceBuilder.java:174)
at org.elasticsearch.watcher.transport.actions.put.PutWatchRequest.setSource(PutWatchRequest.java:84)
at org.elasticsearch.watcher.transport.actions.put.PutWatchRequestBuilder.setSource(PutWatchRequestBuilder.java:56)
at com.watcher.addWatcher.createNew(addWatcher.java:99)
at com.svc.addAlert.newAlert(addAlert.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:164)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:181)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:203)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:101)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:305)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:288)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1110)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:401)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
私を助けてください。