client.asciidoc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 functionality 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. TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
  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.builder()
  40. .put("cluster.name", "myClusterName").build();
  41. TransportClient client = new PreBuiltTransportClient(settings);
  42. //Add transport addresses and do something with the client...
  43. --------------------------------------------------
  44. The Transport client comes with a cluster sniffing feature which
  45. allows it to dynamically add new hosts and remove old ones.
  46. When sniffing is enabled, the transport client will connect to the nodes in its
  47. internal node list, which is built via calls to `addTransportAddress`.
  48. After this, the client will call the internal cluster state API on those nodes
  49. to discover available data nodes. The internal node list of the client will
  50. be replaced with those data nodes only. This list is refreshed every five seconds by default.
  51. Note that the IP addresses the sniffer connects to are the ones declared as the 'publish'
  52. address in those node's elasticsearch config.
  53. Keep in mind that the list might possibly not include the original node it connected to
  54. if that node is not a data node. If, for instance, you initially connect to a
  55. master node, after sniffing, no further requests will go to that master node,
  56. but rather to any data nodes instead. The reason the transport client excludes non-data
  57. nodes is to avoid sending search traffic to master only nodes.
  58. In order to enable sniffing, set `client.transport.sniff` to `true`:
  59. [source,java]
  60. --------------------------------------------------
  61. Settings settings = Settings.settingsBuilder()
  62. .put("client.transport.sniff", true).build();
  63. TransportClient client = new PreBuiltTransportClient(settings);
  64. --------------------------------------------------
  65. Other transport client level settings include:
  66. [cols="<,<",options="header",]
  67. |=======================================================================
  68. |Parameter |Description
  69. |`client.transport.ignore_cluster_name` |Set to `true` to ignore cluster
  70. name validation of connected nodes. (since 0.19.4)
  71. |`client.transport.ping_timeout` |The time to wait for a ping response
  72. from a node. Defaults to `5s`.
  73. |`client.transport.nodes_sampler_interval` |How often to sample / ping
  74. the nodes listed and connected. Defaults to `5s`.
  75. |=======================================================================
  76. [[client-connected-to-client-node]]
  77. === Connecting a Client to a Coordinating Only Node
  78. You can start locally a {ref}/modules-node.html#coordinating-only-node[Coordinating Only Node]
  79. and then simply create a <<transport-client,`TransportClient`>> in your
  80. application which connects to this Coordinating Only Node.
  81. This way, the coordinating only node will be able to load whatever plugin you
  82. need (think about discovery plugins for example).