• 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是一样的,但是易用性更好
  • 相关阅读:
    java 大数据处理类 BigDecimal 解析
    关于纠正 C/C++ 之前在函输内改变 变量的一个错误想法。
    C++ 制作 json 数据 并 传送给服务端(Server) 的 php
    介绍一个很爽的 php 字符串特定检索函数---strpos()
    如何 判断 设备 是否 连接 上 了 wifi
    android 通过访问 php 接受 or 传送数据
    正则匹配抓取input 隐藏输入项和 <td>标签内的内容
    手把手教你Chrome扩展开发:本地存储篇
    HTML5之本地存储localstorage
    初尝CDN:什么是分布式服务节点?
  • 原文地址:https://www.cnblogs.com/zby9527/p/13221377.html
Copyright © 2020-2023  润新知