client.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. [[client]]
  2. == Client
  3. You can use the *Java client* in multiple ways:
  4. * Perform standard <<java-docs-index,index>>, <<java-docs-get,get>>,
  5. <<java-docs-delete,delete>> and <<java-search,search>> operations on an
  6. existing cluster
  7. * Perform administrative tasks on a running cluster
  8. Obtaining an elasticsearch `Client` is simple. The most common way to
  9. get a client is by creating a <<transport-client,`TransportClient`>>
  10. that connects to a cluster.
  11. *Important:*
  12. ______________________________________________________________________________________________________________________________________________________________
  13. Please note that you are encouraged to use the same version on client
  14. and cluster sides. You may hit some incompatibility issues when mixing
  15. major versions.
  16. ______________________________________________________________________________________________________________________________________________________________
  17. [[transport-client]]
  18. === Transport Client
  19. The `TransportClient` connects remotely to an Elasticsearch cluster
  20. using the transport module. It does not join the cluster, but simply
  21. gets one or more initial transport addresses and communicates with them
  22. in round robin fashion on each action (though most actions will probably
  23. be "two hop" operations).
  24. [source,java]
  25. --------------------------------------------------
  26. // on startup
  27. Client client = TransportClient.builder().build()
  28. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
  29. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
  30. // on shutdown
  31. client.close();
  32. --------------------------------------------------
  33. Note that you have to set the cluster name if you use one different than
  34. "elasticsearch":
  35. [source,java]
  36. --------------------------------------------------
  37. Settings settings = Settings.settingsBuilder()
  38. .put("cluster.name", "myClusterName").build();
  39. Client client = TransportClient.builder().settings(settings).build();
  40. //Add transport addresses and do something with the client...
  41. --------------------------------------------------
  42. The client allows sniffing the rest of the cluster, which adds data nodes
  43. into its list of machines to use. In this case, note that the IP addresses
  44. used will be the ones that the other nodes were started with (the
  45. "publish" address). In order to enable it, set the
  46. `client.transport.sniff` to `true`:
  47. [source,java]
  48. --------------------------------------------------
  49. Settings settings = Settings.settingsBuilder()
  50. .put("client.transport.sniff", true).build();
  51. TransportClient client = TransportClient.builder().settings(settings).build();
  52. --------------------------------------------------
  53. Other transport client level settings include:
  54. [cols="<,<",options="header",]
  55. |=======================================================================
  56. |Parameter |Description
  57. |`client.transport.ignore_cluster_name` |Set to `true` to ignore cluster
  58. name validation of connected nodes. (since 0.19.4)
  59. |`client.transport.ping_timeout` |The time to wait for a ping response
  60. from a node. Defaults to `5s`.
  61. |`client.transport.nodes_sampler_interval` |How often to sample / ping
  62. the nodes listed and connected. Defaults to `5s`.
  63. |=======================================================================
  64. [[client-connected-to-client-node]]
  65. === Connecting a Client to a Coordinating Only Node
  66. You can start locally a {ref}/modules-node.html#coordinating-only-node[Coordinating Only Node]
  67. and then simply create a <<transport-client,`TransportClient`>> in your
  68. application which connects to this Coordinating Only Node.
  69. This way, the coordinating only node will be able to load whatever plugin you
  70. need (think about discovery plugins for example).