ElasticSearch的三大客户端比较
一、TransportClient
pom.xml
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
demo
public static void main(String[] args) throws Exception {
// 设置集群名称
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
// 创建client
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("node1"), 9300));
// 搜索数据
GetResponse response = client.prepareGet("你好", "测试", "1").execute().actionGet();
client.close();
}
二、JestClient
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
demo
public static void main(String[] args) throws Exception {
try {
String index = "你好";
JestResult jestResult = jestClient.execute(new CreateIndex.Builder(index).build());
System.out.println("createIndex:{}" + jestResult.isSucceeded());
} catch (IOException e) {
e.printStackTrace();
}
}
三、RestClient
pom.xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
demo
public static void main(String[] args) throws Exception {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
String method = "GET";
String endpoint = "/book";
Response response = restClient.performRequest(method, endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
四、区分优缺点
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方法带参数的请求。 |
还没有评论,来说两句吧...