• ElasticSearch三种Java客户端


    预备知识

    官方客户端地址

    Java客户端分析

    • 分为Java API Client和Java Rest Client
    • Java API Client默认连接的是9300端口,传输协议,依赖netty
    • Java API Client不同版本有兼容问题
    • Java API Client通过方法调用完成交互,有丰富的API
    • Java Rest Client默认连接的是9200端口,http协议
    • Java Rest Client不同版本没有兼容问题
    • Java Rest Client分为High和Low两种
    • Java Low Level Rest Client使用Apache HttpClient进行HTTP调用,简单封装了一下,需要自己处理请求和响应,还是面向HTTP请求的,API简单
    • Java High Level Rest Client基于Java Low Level Rest Client封装,提供了面向方法的API。同时请求参数和响应参数使用了elasticsearch定义的实体,方便从Java API Client迁移。
    • Java High Level Rest Client完成elasticsearch请求响应实体转换为Java Low Level Rest Client的请求响应。即解决了Java API Client兼容问题,又解决了Java Low Level Rest Client封装使用问题

    Java API Client

    Demo

    	<dependency>
    		<groupId>org.elasticsearch.client</groupId>
    		<artifactId>transport</artifactId>
    		<version>7.5.2</version>
    	</dependency>
    
    package com.zby;
    
    import java.io.IOException;
    import java.net.InetAddress;
    
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.TransportAddress;
    import org.elasticsearch.transport.client.PreBuiltTransportClient;
    
    /**
     * @author zby
     * @title ElasticSearchApiDemo
     * @date 2020年7月1日
     * @description
     */
    @SuppressWarnings({"deprecation", "resource"})
    public class ElasticSearchApiDemo {
    
        public static void main(String[] args) throws IOException {
            TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
            GetResponse response = client.get(new GetRequest("customer", "1")).actionGet();
            System.out.println(response);
            client.close();
    
    
    
        }
    
    }
    

    Java Low Level Rest Client

    Demo

    	<dependency>
    		<groupId>org.elasticsearch.client</groupId>
    		<artifactId>elasticsearch-rest-client</artifactId>
    		<version>7.5.2</version>
    	</dependency>
    
    package com.zby;
    
    import java.io.IOException;
    
    import org.apache.http.HttpHost;
    import org.apache.http.util.EntityUtils;
    import org.elasticsearch.client.Request;
    import org.elasticsearch.client.Response;
    import org.elasticsearch.client.RestClient;
    
    /**
     * @author zby
     * @title ElasticSearchLowLevelApiDemo
     * @date 2020年7月1日
     * @description
     */
    public class ElasticSearchLowLevelApiDemo {
    
        public static void main(String[] args) throws IOException {
            RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
            Response response = restClient.performRequest(new Request("GET", "/customer/_doc/1"));
            System.out.println(EntityUtils.toString(response.getEntity()));
            restClient.close();
        }
    
    }
    
    
    • 请求需要自己组装http请求
    • 响应需要自己解析http响应
    • 提供了HttpClient对ElasticSearch简单封装

    Java High Level Rest Client

    Demo

    	<dependency>
    		<groupId>org.elasticsearch.client</groupId>
    		<artifactId>elasticsearch-rest-high-level-client</artifactId>
    		<version>7.5.2</version>
    	</dependency>
    
    package com.zby;
    
    import java.io.IOException;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.get.GetRequest;
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    
    /**
     * @author zby
     * @title ElasticSearchHighLevelApiDemo
     * @date 2020年7月1日
     * @description
     */
    public class ElasticSearchHighLevelApiDemo {
    
        public static void main(String[] args) throws IOException {
            RestHighLevelClient client =
                    new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
            GetResponse getResponse = client.get(new GetRequest("customer", "1"), RequestOptions.DEFAULT);
            System.out.println(getResponse);
            client.close();
        }
    
    }
    
    

    总结

    • Java High Level Rest Client兼顾另外两种api的优点,推荐使用
    • Java High Level Rest Client的请求响应跟Java API Client是一样的,但是易用性更好
  • 相关阅读:
    win10安装.net3.5
    VS2015密钥
    wordpress目录文件结构说明
    js | javascript获取和设置元素的属性
    wordpress | WP Mail SMTP使用QQ邮箱发布失败的解决办法
    jquery 实时监听输入框值变化方法
    XPath编写规则学习
    如何将portfolio产品图片上的悬停去掉?
    wordpress怎么禁止文章复制
    js | javascript实现浏览器窗口大小被改变时触发事件的方法
  • 原文地址:https://www.cnblogs.com/zby9527/p/13221377.html
Copyright © 2020-2023  润新知