client.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. WARNING: The `TransportClient` is aimed to be replaced by the Java High Level REST
  20. Client, which executes HTTP requests instead of serialized Java requests. The
  21. `TransportClient` will be deprecated in upcoming versions of Elasticsearch and it
  22. is advised to use the Java High Level REST Client instead.
  23. [[transport-client]]
  24. === Transport Client
  25. The `TransportClient` connects remotely to an Elasticsearch cluster
  26. using the transport module. It does not join the cluster, but simply
  27. gets one or more initial transport addresses and communicates with them
  28. in round robin fashion on each action (though most actions will probably
  29. be "two hop" operations).
  30. [source,java]
  31. --------------------------------------------------
  32. // on startup
  33. TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
  34. .addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9300))
  35. .addTransportAddress(new TransportAddress(InetAddress.getByName("host2"), 9300));
  36. // on shutdown
  37. client.close();
  38. --------------------------------------------------
  39. Note that you have to set the cluster name if you use one different than
  40. "elasticsearch":
  41. [source,java]
  42. --------------------------------------------------
  43. Settings settings = Settings.builder()
  44. .put("cluster.name", "myClusterName").build();
  45. TransportClient client = new PreBuiltTransportClient(settings);
  46. //Add transport addresses and do something with the client...
  47. --------------------------------------------------
  48. The Transport client comes with a cluster sniffing feature which
  49. allows it to dynamically add new hosts and remove old ones.
  50. When sniffing is enabled, the transport client will connect to the nodes in its
  51. internal node list, which is built via calls to `addTransportAddress`.
  52. After this, the client will call the internal cluster state API on those nodes
  53. to discover available data nodes. The internal node list of the client will
  54. be replaced with those data nodes only. This list is refreshed every five seconds by default.
  55. Note that the IP addresses the sniffer connects to are the ones declared as the 'publish'
  56. address in those node's Elasticsearch config.
  57. Keep in mind that the list might possibly not include the original node it connected to
  58. if that node is not a data node. If, for instance, you initially connect to a
  59. master node, after sniffing, no further requests will go to that master node,
  60. but rather to any data nodes instead. The reason the transport client excludes non-data
  61. nodes is to avoid sending search traffic to master only nodes.
  62. In order to enable sniffing, set `client.transport.sniff` to `true`:
  63. [source,java]
  64. --------------------------------------------------
  65. Settings settings = Settings.builder()
  66. .put("client.transport.sniff", true).build();
  67. TransportClient client = new PreBuiltTransportClient(settings);
  68. --------------------------------------------------
  69. Other transport client level settings include:
  70. [cols="<,<",options="header",]
  71. |=======================================================================
  72. |Parameter |Description
  73. |`client.transport.ignore_cluster_name` |Set to `true` to ignore cluster
  74. name validation of connected nodes. (since 0.19.4)
  75. |`client.transport.ping_timeout` |The time to wait for a ping response
  76. from a node. Defaults to `5s`.
  77. |`client.transport.nodes_sampler_interval` |How often to sample / ping
  78. the nodes listed and connected. Defaults to `5s`.
  79. |=======================================================================
  80. [[client-connected-to-client-node]]
  81. === Connecting a Client to a Coordinating Only Node
  82. You can start locally a {ref}/modules-node.html#coordinating-only-node[Coordinating Only Node]
  83. and then simply create a <<transport-client,`TransportClient`>> in your
  84. application which connects to this Coordinating Only Node.
  85. This way, the coordinating only node will be able to load whatever plugin you
  86. need (think about discovery plugins for example).