Elasticsearch整理汇总-RestHighLevelClient使用JAVA(二)

声明:所有内容均为本人查找网上资料汇总整理,非本人原创,但有本人整理心得;感谢每位学习爱好者对知识的传承和分享!!!

Java REST Client 有两种风格:

  Java Low Level REST Client :用于Elasticsearch的官方低级客户端。它允许通过http与Elasticsearch集群通信。将请求编排和响应反编排留给用户自己处理。它兼容所有的Elasticsearch版本。(PS:学过WebService的话,对编排与反编排这个概念应该不陌生。可以理解为对请求参数的封装,以及对响应结果的解析)

  Java High Level REST Client :用于Elasticsearch的官方高级客户端。它是基于低级客户端的,它提供很多API,并负责请求的编排与响应的反编排。(PS:就好比是,一个是传自己拼接好的字符串,并且自己解析返回的结果;而另一个是传对象,返回的结果也已经封装好了,直接是对象,更加规范了参数的名称以及格式,更加面对对象一点)

 

项目依赖:

POM文件

<dependencies>

<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.10.1</version>
        </dependency>  
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

<repositories> <repository> <id>aliyun</id> <url>https://repo1.maven.org/maven2/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>

 

JAVA代码配置客户端:

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.com.taiji.common.pub.StringTools;

@Configuration
public class ElasticsearchConfig {

    @Value("#{esProperties.hostIp}")
     private String hostIp;
    
    @Value("#{esProperties.hostPort}")
     private int hostPort;
    
    @Value("#{esProperties.userName}")
    private String userName;
    
    @Value("#{esProperties.password}")
    private String password;
        
        
    @Bean
    public RestHighLevelClient restHighLevelClient() {
         String[] hosts = this.hostIp.split(";");
         HttpHost[] httpHosts = new HttpHost[hosts.length];
         for(int i=0;i<hosts.length;i++) {
             httpHosts[i] = new HttpHost(hosts[i], hostPort, "http");
         }
         RestClientBuilder builder = RestClient.builder(httpHosts);
         if(StringTools.hasText(userName)){
             final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
             credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));                 
             builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
         }
         RestHighLevelClient client = new RestHighLevelClient(builder);           
         return client;
    }

}

 

 

主要参考博客:https://www.cnblogs.com/cjsblog/p/10232581.html

posted @ 2020-12-26 16:06  jiajinhao  阅读(197)  评论(0编辑  收藏  举报