Elasticsearch Java Rest Client简述
ESJavaClient的历史
JavaAPI Client
- 优势:基于transport进行数据访问,能够使用ES集群内部的性能特性,性能相对好
- 劣势:client版本需要和es集群版本一致,数据序列化通过java实现,es集群或jdk升级,客户端需要伴随升级。
REST Client
- 优势:REST风格交互,符合ES设计初衷;兼容性强;
- 劣势:性能相对API较低
Java Rest Client
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "pangu",
"_type": "normal",
"_id": "2088201805281551",
"_score": 0.2876821,
"_source": {
"age": 8,
"country": "UK",
"id": "2088201805281551",
"name": "baby"
}
},
{
"_index": "pangu",
"_type": "normal",
"_id": "2088201805281552",
"_score": 0.2876821,
"_source": {
"age": 8,
"country": "UK",
"id": "2088201805281552",
"name": "baby"
}
}
]
}
}
Demo
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* @author yanlei
* @version $Id: RestClientDemo.java, v 0.1 2018年05月26日 下午12:27 yanlei Exp $
*/
public class RestClientDemo { private static final Logger LOGGER = LoggerFactory.getLogger(RestClientDemo.class); /**
* index 名称
*/
private static final String INDEX_NAME = "pangu"; /**
* type 名称
*/
private static final String TYPE_NAME = "normal"; private static RestClient restClient; static {
restClient = RestClient.builder(new HttpHost("search.alipay.com", 9999, "http"))
.setFailureListener(new RestClient.FailureListener() { // 连接失败策略
@Override
public void onFailure(HttpHost host) {
LOGGER.error("init client error, host:{}", host);
}
})
.setMaxRetryTimeoutMillis(10000) // 超时时间
.setHttpClientConfigCallback(new HttpClientConfigCallback() { // 认证
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("example", "WnhmUwjU"));
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
})
.build();
} public boolean index(String id, String jsonString) {
String method = "PUT";
String endpoint = new StringBuilder().append("/").append(INDEX_NAME).append("/").append(TYPE_NAME).append("/").append(id)
.toString();
LOGGER.info("method={}, endpoint={}", method, endpoint); HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
Response response = null;
try {
response = restClient.performRequest(method, endpoint, Collections.emptyMap(), entity);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED
|| response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
LOGGER.info("index success");
return true;
} else {
LOGGER.error("index error : {}", response.toString());
return false;
}
} catch (IOException e) {
LOGGER.error("system error, id={}, jsonString={}", id, jsonString, e);
return false;
}
} public <T> T search(String searchString, CallbackSearch<T> callbackSearch) {
String method = "GET";
String endpoint = new StringBuilder().append("/").append(INDEX_NAME).append("/").append(TYPE_NAME).append("/").append("_search")
.toString();
LOGGER.info("method={}, endpoint={}", method, endpoint); HttpEntity entity = new NStringEntity(searchString, ContentType.APPLICATION_JSON);
Response response = null;
try {
response = restClient.performRequest(method, endpoint, Collections.emptyMap(), entity);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 提取数据
String resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
return callbackSearch.get(resultString);
} else {
LOGGER.error("index false, error : {}", response);
return null;
}
} catch (IOException e) {
LOGGER.error("system error, searchString={}", searchString, e);
return null;
}
} public interface CallbackSearch<T> {
T get(String responseString);
} public static void main(String[] args) {
RestClientDemo restClientDemo = new RestClientDemo(); // 1. 索引数据
User user = new User();
user.setId("2088201805281345");
user.setName("nick");
user.setAge(16);
user.setCountry("USA");
boolean indexOK = restClientDemo.index(user.getId(), JSON.toJSONString(user));
LOGGER.info("index param={} result={}", user, indexOK); // 2. 数据检索
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
.query(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("name", "nick")))
.sort("id", SortOrder.DESC)
.from(0).size(10); List<User> searchResult = restClientDemo.search(sourceBuilder.toString(), new CallbackSearch<List<User>>() {
@Override
public List<User> get(String responseString) {
LOGGER.info("responseString={}", responseString);
List<User> result = new ArrayList<>();
JSONObject responseObj = JSON.parseObject(responseString);
JSONObject hits = responseObj.getJSONObject("hits");
if (hits.getIntValue("total") != 0) {
JSONArray innerHits = hits.getJSONArray("hits");
for (int i = 0; i < innerHits.size(); i++) {
JSONObject innerhit = innerHits.getJSONObject(i);
User user = innerhit.getObject("_source", User.class);
result.add(user);
}
}
return result;
}
});
LOGGER.info("search param={} result={}", sourceBuilder, searchResult); // 3. 聚合查询
SearchSourceBuilder aggSearchSourceBuilder = new SearchSourceBuilder()
.query(QueryBuilders.matchAllQuery())
.aggregation(AggregationBuilders.avg("age_avg").field("age")); Double aggResult = restClientDemo.search(aggSearchSourceBuilder.toString(), new CallbackSearch<Double>() { @Override
public Double get(String responseString) {
LOGGER.info("responseString={}", responseString);
JSONObject responseObj = JSON.parseObject(responseString);
JSONObject aggregations = responseObj.getJSONObject("aggregations");
Double result = aggregations.getJSONObject("age_avg").getDouble("value"); return result;
}
}); LOGGER.info("aggregation param={} result={}", aggSearchSourceBuilder, aggResult);
}
}
Java High Level REST Client
Demo
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import com.alibaba.fastjson.JSON; import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
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.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* @author yanlei
* @version $Id: RestClientDemo.java, v 0.1 2018年05月26日 下午12:27 yanlei Exp $
*/
public class HighLevelRestClientDemo { private static final Logger LOGGER = LoggerFactory.getLogger(HighLevelRestClientDemo.class); private static final String INDEX_NAME = "pangu"; private static final String TYPE_NAME = "normal"; private static RestHighLevelClient client; static {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("example", "WnhmUwjU")); RestClientBuilder builder = RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http"))
.setFailureListener(new RestClient.FailureListener() { // 连接失败策略
@Override
public void onFailure(HttpHost host) {
LOGGER.error("init client error, host:{}", host);
}
})
.setMaxRetryTimeoutMillis(10000) // 超时时间
.setHttpClientConfigCallback(new HttpClientConfigCallback() { // 认证
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}); client = new RestHighLevelClient(builder);
} public boolean index(String id, String jsonString) {
IndexRequest request = new IndexRequest(
INDEX_NAME,
TYPE_NAME,
id);
request.source(jsonString, XContentType.JSON); IndexResponse response = null;
try {
response = client.index(request);
if(response.status().getStatus() == HttpStatus.SC_CREATED || response.status().getStatus() == HttpStatus.SC_OK) {
LOGGER.info("index success");
return true;
}else {
LOGGER.error("index error : {}", response.toString());
return false;
}
} catch (IOException e) {
LOGGER.error("系统异常", e);
return false;
}
} public <T> T search(SearchSourceBuilder sourceBuilder, CallbackSearch<T> callbackSearch) {
SearchRequest searchRequest = new SearchRequest()
.indices(INDEX_NAME)
.types(TYPE_NAME)
.source(sourceBuilder); SearchResponse response = null;
try {
response = client.search(searchRequest);
if(response.status().getStatus() == HttpStatus.SC_OK) {
// 提取数据
return callbackSearch.get(response);
}else {
LOGGER.error("index false, error : {}", response);
return null;
}
} catch (IOException e) {
LOGGER.error("系统异常", e);
return null;
}
} public interface CallbackSearch<T> {
T get(SearchResponse response);
} public static void main(String[] args) {
HighLevelRestClientDemo restClientDemo = new HighLevelRestClientDemo(); // 1. 索引数据
User user = new User();
user.setId("2088201805281552");
user.setName("baby");
user.setAge(8);
user.setCountry("UK");
boolean indexOK = restClientDemo.index(user.getId(), JSON.toJSONString(user));
LOGGER.info("index param={} result={}", user, indexOK); // 2. 数据检索
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
.query(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("name", "baby")))
//.sort("age", SortOrder.DESC)
.from(0).size(10); List<User> searchResult = restClientDemo.search(sourceBuilder, new CallbackSearch<List<User>>() {
@Override
public List<User> get(SearchResponse response) {
List<User> result = new ArrayList<>();
response.getHits().forEach(hit -> {
User user = JSON.parseObject(hit.getSourceAsString(), User.class);
result.add(user);
return result;
}
});
LOGGER.info("search param={} result={}", sourceBuilder, searchResult); }
}
JestClient
DEMO
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.SearchResult.Hit;
import io.searchbox.core.search.aggregation.MetricAggregation;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* @author yanlei
* @version $Id: JestClientDemo.java, v 0.1 2018年05月23日 下午5:45 yanlei Exp $
*/
public class JestClientDemo { private static final Logger LOGGER = LoggerFactory.getLogger(JestClientDemo.class); private static final String INDEX_NAME = "pangu"; private static final String TYPE_NAME = "normal"; private static JestClient client; static {
JestClientFactory factory = new JestClientFactory(); // 定制化gson配置
Gson gson = new GsonBuilder().setDateFormat("yyyyMMdd hh:mm:ss").create(); factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://search.alipay.com:9999") // 集群地址
.defaultCredentials("example", "WnhmUwjU") // 认证信息
.gson(gson) // 启用定制化gson,可使用默认
.multiThreaded(true)
.defaultMaxTotalConnectionPerRoute(2)
.maxTotalConnection(10)
.build()); client = factory.getObject();
} public boolean index(String id, Object obj) {
Index index = new Index.Builder(obj).index(INDEX_NAME).type(TYPE_NAME).id(id).build();
try {
JestResult result = client.execute(index);
if(result.isSucceeded()) {
LOGGER.info("index success");
return true;
}else {
LOGGER.error("index error : {}", result.getErrorMessage());
return false;
}
} catch (IOException e) {
LOGGER.error("系统异常", e);
return false;
}
} public boolean index(User user) {
Index index = new Index.Builder(user).index(INDEX_NAME).type(TYPE_NAME).build();
try {
JestResult result = client.execute(index);
if(result.isSucceeded()) {
LOGGER.info("index success");
return true;
}else {
LOGGER.error("index error : {}", result.getErrorMessage());
return false;
}
} catch (IOException e) {
LOGGER.error("系统异常", e);
return false;
}
} public <T> List<T> search(SearchSourceBuilder sourceBuilder, CallbackSearch<T> callbackSearch, Class<T> response) {
Search search = new Search.Builder(sourceBuilder.toString())
.addIndex(INDEX_NAME)
.addType(TYPE_NAME)
.build(); SearchResult result = null;
try {
result = client.execute(search);
if(result.isSucceeded()) {
// 提取数据
List<Hit<T, Void>> hits = result.getHits(response);
return callbackSearch.getHits(hits);
}else {
LOGGER.error("index false, error : {}", result.getErrorMessage());
return null;
}
} catch (IOException e) {
LOGGER.error("系统异常", e);
return null;
}
} public <T> T aggregation(SearchSourceBuilder sourceBuilder, CallbackAggregation<T> callbackAggregation) {
Search search = new Search.Builder(sourceBuilder.toString())
.addIndex(INDEX_NAME)
.addType(TYPE_NAME)
.build(); SearchResult result = null;
try {
result = client.execute(search);
if(result.isSucceeded()) {
return callbackAggregation.getAgg(result.getAggregations());
}else {
LOGGER.error("index false, error : {}", result.getErrorMessage());
return null;
}
} catch (IOException e) {
LOGGER.error("系统异常", e);
return null;
}
} public interface CallbackSearch<T> {
List<T> getHits(List<Hit<T, Void>> hits);
} public interface CallbackAggregation<T> {
T getAgg(MetricAggregation metricAggregation);
} public static void main(String[] args) {
JestClientDemo demo = new JestClientDemo(); // 1. 索引数据
User user = new User();
user.setId("2088201805281205");
user.setName("nick");
user.setAge(18);
user.setCountry("CHINA");
boolean indexOK = demo.index(user);
LOGGER.info("index param={} result={}", user, indexOK); // 2. 数据检索
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
.query(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("name", "nick")))
.sort("id", SortOrder.DESC)
.from(0).size(10);
List<User> searchResult = demo.search(sourceBuilder, new CallbackSearch<User>() {
@Override
public List<User> getHits(List<Hit<User, Void>> hits) {
List<User> userList = new ArrayList<>();
hits.forEach(hit -> {
userList.add(hit.source);
});
return userList;
}
}, User.class);
LOGGER.info("search param={} result={}", sourceBuilder, searchResult); // 3. 聚合查询
SearchSourceBuilder aggSearchSourceBuilder = new SearchSourceBuilder()
.query(QueryBuilders.matchAllQuery())
.aggregation(AggregationBuilders.avg("age_avg").field("age"));
double aggResult = demo.aggregation(aggSearchSourceBuilder, metricAggregation -> {
double avgAge = metricAggregation.getAvgAggregation("age_avg").getAvg();
return avgAge;
});
LOGGER.info("aggregation param={} result={}", aggSearchSourceBuilder, aggResult);
}
}
小结
Elasticsearch Java Rest Client简述的更多相关文章
- Elasticsearch Java Rest Client API 整理总结 (二) —— SearchAPI
目录 引言 Search APIs Search API Search Request 可选参数 使用 SearchSourceBuilder 构建查询条件 指定排序 高亮请求 聚合请求 建议请求 R ...
- Elasticsearch Java Rest Client API 整理总结 (三)——Building Queries
目录 上篇回顾 Building Queries 匹配所有的查询 全文查询 Full Text Queries 什么是全文查询? Match 全文查询 API 列表 基于词项的查询 Term Term ...
- Elasticsearch Java Rest Client API 整理总结 (一)——Document API
目录 引言 概述 High REST Client 起步 兼容性 Java Doc 地址 Maven 配置 依赖 初始化 文档 API Index API GET API Exists API Del ...
- Elasticsearch Java Rest Client API 整理总结 (一)
http://www.likecs.com/default/index/show?id=39549
- Elasticsearch【JAVA REST Client】客户端操作
ES系统作为集群,环境搭建非常方便简单. 现在在我们的应用中,如何对这个集群进行操作呢? 我们利用ES系统,通常都是下面的架构: 在这里,客户端的请求通过LB进行负载均衡,因为操作任何一个ES的实例, ...
- elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...
- Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...
- springboot 配置elasticsearch Java High Rest Client
前提声明 在新版本的spring boot中逐渐放弃了对Spring Data Elasticsearch的支持,所以不推荐使用,使用ES官方推出的Java High Rest Client. 引入依 ...
- elasticsearch Java Client用户指南
这里使用的Java客户端版本是5.1.2,Elasticsearch的版本号也要是5.1.2,否则一些功能可能不支持. 之前介绍过Spring Data Elasticsearch,那里也是使用了本文 ...
随机推荐
- 巧记 In/hasOwnProperty/for…in/for…of/forEach区别
写在前面 上面提到的这些东西,反复的记忆,反复的忘记.现分享一种巧记方法,有需要,请参考,希望你也过目不忘. 仅提供巧记思路.仅提供巧记思路.仅提供巧记思路. 1. in 用于判断属性是否在对象上(包 ...
- spring中WebApplicationContext、DispatcherServlet与web容器的ServletContext关系梳理
学习源码过程中,对各种context(上下文)表示很懵逼.特地留此一篇. 1.要了解各个上下文之间的关系.首先走一遍spring在web容器(tomcat)中的启动过程 a) ServletConte ...
- php + h5 实现socket推送技术
在socket出现之前已经有ajax定时请求.长轮询等方案,但都不能满足需求,socket就应用而生了. socket基本函数socket 总结下常用的socket函数 服务端: socket_cre ...
- Docker搭建Zentao(禅道)
禅道搭建方式有很多种,可参考官方文档搭建,这里介绍的是参考官方文档以docker方式搭建. 禅道内部默认会自动安装mysql数据库. 一.下载地址 禅道开源版: http://dl.cnezsof ...
- python的with语法的深入理解
如果你有一些对象(比如一个文件.网络连接或锁),需要支持 with 语句,下面介绍两种定义方法. 方法(1): 首先介绍下with 工作原理 (1)紧跟with后面的语句被求值后,返回对象的“__en ...
- python之提升程序性能的解决方案
Python在性能方面不卓越,但是使用一些小技巧,可以提高Python程序的性能,避免不必要的资源浪费. 1. 使用局部变量 尽可能使用局部变量替代全局变量,可以是程序易于维护并且有助于提高性能节约成 ...
- Dapper use Table Value Parameter in C# (Sql Server 数组参数)
Dapper 也可以使用 数组参数 参考:Blog on Github Dapper 调用存储过程 :单个参数 static void Main(string[] args) { var connec ...
- npm install 和 npm ci 的主要区别
npm install 和 npm ci 的主要区别: 该项目必须有一个 package-lock.json 或 npm-shrinkwrap.json. 如果 package-lock.json 中 ...
- CodeForces - 666E: Forensic Examination (广义SAM 线段树合并)
题意:给定字符串S,然后M个字符串T.Q次询问,每次给出(L,R,l,r),问S[l,r]在L到R这些T字符串中,在哪个串出现最多,以及次数. 思路:把所有串建立SAM,然后可以通过倍增走到[l,r] ...
- Objective-C 消息发送与转发机制原理(摘)
八面玲珑的 objc_msgSend 此函数是消息发送必经之路,但只要一提 objc_msgSend,都会说它的伪代码如下或类似的逻辑,反正就是获取 IMP 并调用: id objc_msgSend( ...