ElasticSearch的三大客户端比较

小鱼儿 2021-11-22 09:26 404阅读 0赞

一、TransportClient

pom.xml

  1. <dependency>
  2. <groupId>org.elasticsearch</groupId>
  3. <artifactId>elasticsearch</artifactId>
  4. <version>${elasticsearch.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.elasticsearch.client</groupId>
  8. <artifactId>transport</artifactId>
  9. <version>${elasticsearch.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.elasticsearch.plugin</groupId>
  13. <artifactId>transport-netty4-client</artifactId>
  14. <version>${elasticsearch.version}</version>
  15. </dependency>

demo

  1. public static void main(String[] args) throws Exception {
  2. // 设置集群名称
  3. Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
  4. // 创建client
  5. TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("node1"), 9300));
  6. // 搜索数据
  7. GetResponse response = client.prepareGet("你好", "测试", "1").execute().actionGet();
  8. client.close();
  9. }

二、JestClient

pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>io.searchbox</groupId>
  7. <artifactId>jest</artifactId>
  8. <version>${elasticsearch.version}</version>
  9. </dependency>

demo

  1. public static void main(String[] args) throws Exception {
  2. try {
  3. String index = "你好";
  4. JestResult jestResult = jestClient.execute(new CreateIndex.Builder(index).build());
  5. System.out.println("createIndex:{}" + jestResult.isSucceeded());
  6. } catch (IOException e) {
  7. e.printStackTrace();
  8. }
  9. }

三、RestClient

pom.xml

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>rest</artifactId>
  4. <version>${elasticsearch.version}</version>
  5. </dependency>

demo

  1. public static void main(String[] args) throws Exception {
  2. RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
  3. String method = "GET";
  4. String endpoint = "/book";
  5. Response response = restClient.performRequest(method, endpoint);
  6. System.out.println(EntityUtils.toString(response.getEntity()));
  7. }

四、区分优缺点
























TransportClient JestClient RestClient
优点 启动速度快,轻量级,可创建极多连接,与应用程序解耦;推荐使用原生的,ES本身就很简单,灵活性很高。 提供Restful API, 原生ES API不具备;若ES集群使用不同的ES版本,使用原生ES API会有问题,而Jest不会;更安全(可以在Http层添加安全处理);JestClient是ElasticSearch的Java HTTP Rest客户端; JestClient填补了 ElasticSearch缺少HttpRest接口客户端的空白; JestClient可以跨版本。 RestClient 使用HTTP API elasticsearch代替内部协议。这需要更少依赖关系。你也不需要关注那么多版本;RestClient可以跨版本,但是也是要这个api版本都支持 才可以跨。
缺点 分发或查询数据速度较慢,不能获取指定节点数据。 暂时未发现 HttpClient和Jsoup都不直接支持发送DELETE方法带参数的请求。

发表评论

表情:
评论列表 (有 0 条评论,404人围观)

还没有评论,来说两句吧...

相关阅读