java.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. name "elastic"
  51. url "https://artifacts.elastic.co/maven"
  52. }
  53. }
  54. dependencies {
  55. // Provide the x-pack jar on the classpath for compilation and at runtime
  56. compile "org.elasticsearch.client:x-pack-transport:{version}"
  57. /* ... */
  58. }
  59. --------------------------------------------------------------
  60. 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]
  61. manually, directly from our Maven repository.
  62. [float]
  63. === Obtaining the `WatcherClient`
  64. To obtain an instance of the `WatcherClient` you first need to create the
  65. `XPackClient`. The `XPackClient` is a wrapper around the standard Java
  66. Elasticsearch `Client`:
  67. [source,java]
  68. --------------------------------------------------
  69. import org.elasticsearch.client.transport.TransportClient;
  70. import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
  71. import org.elasticsearch.xpack.core.XPackClient;
  72. import org.elasticsearch.xpack.core.XPackPlugin;
  73. import org.elasticsearch.core.watcher.client.WatcherClient;
  74. ...
  75. TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
  76. .put("cluster.name", "myClusterName")
  77. ...
  78. .build())
  79. .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
  80. XPackClient xpackClient = new XPackClient(client);
  81. WatcherClient watcherClient = xpackClient.watcher();
  82. --------------------------------------------------
  83. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/java/put-watch.asciidoc
  84. include::java/put-watch.asciidoc[]
  85. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/java/get-watch.asciidoc
  86. include::java/get-watch.asciidoc[]
  87. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/java/delete-watch.asciidoc
  88. include::java/delete-watch.asciidoc[]
  89. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/java/execute-watch.asciidoc
  90. include::java/execute-watch.asciidoc[]
  91. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/java/ack-watch.asciidoc
  92. include::java/ack-watch.asciidoc[]
  93. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/java/activate-watch.asciidoc
  94. include::java/activate-watch.asciidoc[]
  95. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/java/deactivate-watch.asciidoc
  96. include::java/deactivate-watch.asciidoc[]
  97. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/java/stats.asciidoc
  98. include::java/stats.asciidoc[]
  99. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/java/service.asciidoc
  100. include::java/service.asciidoc[]