java.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. [[api-java]]
  2. == Java API
  3. deprecated[7.0.0, The `TransportClient` is deprecated in favour of the {java-rest}/java-rest-high.html[Java High Level REST Client] and will be removed in Elasticsearch 8.0. The {java-rest}/java-rest-high-level-migration.html[migration guide] describes all the steps needed to migrate.]
  4. {xpack} provides a Java client called `WatcherClient` that adds native Java
  5. support for the {watcher}.
  6. To obtain a `WatcherClient` instance, make sure you first set up the
  7. `XPackClient`.
  8. [float]
  9. === Installing XPackClient
  10. You first need to make sure the +x-pack-transport-{version}+ JAR file is in the classpath.
  11. You can extract this jar from the downloaded {xpack} bundle.
  12. If you use Maven to manage dependencies, add the following to the `pom.xml`:
  13. ["source","xml",subs="attributes,callouts"]
  14. --------------------------------------------------
  15. <project ...>
  16. <repositories>
  17. <!-- add the elasticsearch repo -->
  18. <repository>
  19. <id>elasticsearch-releases</id>
  20. <url>https://artifacts.elastic.co/maven</url>
  21. <releases>
  22. <enabled>true</enabled>
  23. </releases>
  24. <snapshots>
  25. <enabled>false</enabled>
  26. </snapshots>
  27. </repository>
  28. ...
  29. </repositories>
  30. ...
  31. <dependencies>
  32. <!-- add the x-pack jar as a dependency -->
  33. <dependency>
  34. <groupId>org.elasticsearch.client</groupId>
  35. <artifactId>x-pack-transport</artifactId>
  36. <version>{version}</version>
  37. </dependency>
  38. ...
  39. </dependencies>
  40. ...
  41. </project>
  42. --------------------------------------------------
  43. If you use Gradle, add the dependencies to `build.gradle`:
  44. ["source","groovy",subs="attributes,callouts"]
  45. --------------------------------------------------------------
  46. repositories {
  47. /* ... Any other repositories ... */
  48. // Add the Elasticsearch Maven Repository
  49. maven {
  50. url "https://artifacts.elastic.co/maven"
  51. }
  52. }
  53. dependencies {
  54. // Provide the x-pack jar on the classpath for compilation and at runtime
  55. compile "org.elasticsearch.client:x-pack-transport:{version}"
  56. /* ... */
  57. }
  58. --------------------------------------------------------------
  59. You can also download the https://artifacts.elastic.co/maven/org/elasticsearch/client/x-pack-transport/{version}/x-pack-transport-{version}.jar[X-Pack Transport JAR]
  60. manually, directly from our Maven repository.
  61. [float]
  62. === Obtaining the `WatcherClient`
  63. To obtain an instance of the `WatcherClient` you first need to create the
  64. `XPackClient`. The `XPackClient` is a wrapper around the standard Java
  65. Elasticsearch `Client`:
  66. [source,java]
  67. --------------------------------------------------
  68. import org.elasticsearch.client.transport.TransportClient;
  69. import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
  70. import org.elasticsearch.xpack.core.XPackClient;
  71. import org.elasticsearch.xpack.core.XPackPlugin;
  72. import org.elasticsearch.core.watcher.client.WatcherClient;
  73. ...
  74. TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
  75. .put("cluster.name", "myClusterName")
  76. ...
  77. .build())
  78. .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
  79. XPackClient xpackClient = new XPackClient(client);
  80. WatcherClient watcherClient = xpackClient.watcher();
  81. --------------------------------------------------
  82. include::java/put-watch.asciidoc[]
  83. include::java/get-watch.asciidoc[]
  84. include::java/delete-watch.asciidoc[]
  85. include::java/execute-watch.asciidoc[]
  86. include::java/ack-watch.asciidoc[]
  87. include::java/activate-watch.asciidoc[]
  88. include::java/deactivate-watch.asciidoc[]
  89. include::java/stats.asciidoc[]
  90. include::java/service.asciidoc[]