client.asciidoc 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. The client must have the same major version (e.g. `2.x`, or `5.x`) as the
  14. nodes in the cluster. Clients may connect to clusters which have a different
  15. minor version (e.g. `2.3.x`) but it is possible that new funcionality may not
  16. be supported. Ideally, the client should have the same version as the
  17. cluster.
  18. ==============================
  19. [[transport-client]]
  20. === Transport Client
  21. The `TransportClient` connects remotely to an Elasticsearch cluster
  22. using the transport module. It does not join the cluster, but simply
  23. gets one or more initial transport addresses and communicates with them
  24. in round robin fashion on each action (though most actions will probably
  25. be "two hop" operations).
  26. [source,java]
  27. --------------------------------------------------
  28. // on startup
  29. Client client = TransportClient.builder().build()
  30. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
  31. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
  32. // on shutdown
  33. client.close();
  34. --------------------------------------------------
  35. Note that you have to set the cluster name if you use one different than
  36. "elasticsearch":
  37. [source,java]
  38. --------------------------------------------------
  39. Settings settings = Settings.settingsBuilder()
  40. .put("cluster.name", "myClusterName").build();
  41. Client client = TransportClient.builder().settings(settings).build();
  42. //Add transport addresses and do something with the client...
  43. --------------------------------------------------
  44. The client allows sniffing the rest of the cluster, which adds data nodes
  45. into its list of machines to use. In this case, note that the IP addresses
  46. used will be the ones that the other nodes were started with (the
  47. "publish" address). In order to enable it, set the
  48. `client.transport.sniff` to `true`:
  49. [source,java]
  50. --------------------------------------------------
  51. Settings settings = Settings.settingsBuilder()
  52. .put("client.transport.sniff", true).build();
  53. TransportClient client = TransportClient.builder().settings(settings).build();
  54. --------------------------------------------------
  55. Other transport client level settings include:
  56. [cols="<,<",options="header",]
  57. |=======================================================================
  58. |Parameter |Description
  59. |`client.transport.ignore_cluster_name` |Set to `true` to ignore cluster
  60. name validation of connected nodes. (since 0.19.4)
  61. |`client.transport.ping_timeout` |The time to wait for a ping response
  62. from a node. Defaults to `5s`.
  63. |`client.transport.nodes_sampler_interval` |How often to sample / ping
  64. the nodes listed and connected. Defaults to `5s`.
  65. |=======================================================================
  66. [[client-connected-to-client-node]]
  67. === Connecting a Client to a Client Node
  68. You can start locally a {ref}/modules-node.html#client-node[Client Node] and then simply create
  69. a <<transport-client,`TransportClient`>> in your application which connects to this Client Node.
  70. This way, the client node will be able to load whatever plugin you need (think about discovery plugins for example).