欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Elasticsearch Java 客户端的选择

程序员文章站 2022-07-01 15:26:52
...
Elasticsearch 提供了三种Java API接口:
Java client:
这是TransportClient 提供的原生客户端,不能执行原生DSL语句必须使用它的Java API方法。

注意:
在Elasticsearch 7 版本中Elasticsearch官方宣布计划在7.0版本TransportClient被标记为Deprecated,将在
8.0版本删除。

REST API:
这种客户端可以使用DSL语句拼接字符串直接传递给服务端 然后返回json字符进行解析。
Rest API有提供了2种分别为低阶(low level)和高阶(High Level)
低阶的API获取的信息较少,高阶的API直接的功能较多。

结论:
在实际生产开发中优先选择使用REST API的High-level API。



Java REST API:
The Java REST Client comes in 2 flavors:

Java Low Level REST Client: the official low-level client for Elasticsearch. It allows to communicate 
with an Elasticsearch cluster through http. Leaves requests marshalling and responses un-marshalling 
to users. It is compatible with all Elasticsearch versions.

The low-level client’s features include:

minimal dependencies
load balancing across all available nodes
failover in case of node failures and upon specific response codes
failed connection penalization (whether a failed node is retried depends on how many consecutive 
times it failed; the more failed attempts the longer the client will wait before trying that same
 node again)
persistent connections
trace logging of requests and responses
optional automatic discovery of cluster nodes

Maven中使用的:
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>7.5.1</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client-sniffer</artifactId>
    <version>7.5.1</version>
</dependency>


Gradle中使用:
dependencies {
    compile 'org.elasticsearch.client:elasticsearch-rest-client:7.5.1'
}
dependencies {
    compile 'org.elasticsearch.client:elasticsearch-rest-client-sniffer:7.5.1'
}


低阶:
The low-level Java REST client internally uses the Apache Http Async Client to send http requests. 
It depends on the following artifacts, namely the async http client and its own transitive dependencies:

org.apache.httpcomponents:httpasyncclient
org.apache.httpcomponents:httpcore-nio
org.apache.httpcomponents:httpclient
org.apache.httpcomponents:httpcore
commons-codec:commons-codec
commons-logging:commons-logging



Java High Level REST Client: the official high-level client for Elasticsearch. Based on the low-level 
client, it exposes API specific methods and takes care of requests marshalling and responses
 un-marshalling.
依赖的软件包:
The High Level Java REST Client depends on the following artifacts and their transitive dependencies:

org.elasticsearch.client:elasticsearch-rest-client
org.elasticsearch:elasticsearch


<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.5.1</version>
</dependency>

Gradle的配置:
dependencies {
    compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.5.1'
}

功能介绍:

Java client:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>7.5.1</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
</dependency>

若使用log4j2:
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>2.11.1</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.24</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.21</version>
</dependency>

Java客户端的使用:
You can use the Java client in multiple ways:

Perform standard index, get, delete and search operations on an existing cluster
Perform administrative 
tasks on a running cluster
Obtaining an Elasticsearch Client is simple. The most common way to get a client is by creating a TransportClient
 that connects to a cluster.


警告:
TransportClient
Deprecated in 7.0.0.
The TransportClient is deprecated in favour of the Java High Level REST Client and will be removed in 
Elasticsearch 8.0. The migration guide describes all the steps needed to migrate.



Elasticsearch和spring boot结合使用需要导入的包:
<!-- https://spring.io/projects/spring-boot -->

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>


<!-- https://github.com/searchbox-io/Jest -->
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>6.3.1</version>
</dependency>

<!-- 	https://github.com/java-native-access/jna -->
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>5.5.0</version>
</dependency>


Elasticsearch 6.3 版本的选择:
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
   <groupId>io.searchbox</groupId>
   <artifactId>jest</artifactId>
   <version>5.3.3</version>
</dependency>


<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
   <groupId>net.java.dev.jna</groupId>
   <artifactId>jna</artifactId>
   <version>4.5.1</version>
</dependency>


参考:
https://www.elastic.co/guide/en/elasticsearch/client/index.html

 

相关标签: Elasticsearch