*elasticsearch(后面简称es)
-
背景: 目前项目应用中对es的操作用的是http(自己封装)的一套方法;有些数据处理起来还是需要定制开发处理,不是很方便。正好需要对本项目重新进行改造,于是采用第三方工具包jest 对es的操作进行重新的梳理改造。
-
why use jest
官方有个大致的介绍:Jest is a Java HTTP Rest client for ElasticSearch.It is actively developed and tested by Searchly.jset优势
1)提供Restful API, 原生ES API不具备;
2)Jest支持不同版本的es基本操作 主要是http rest client; -
maven 管理项目:
<dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>2.0.3</version> </dependency>
-
jest使用
网上有很多(上述图片中)的方法实例,下面就不具体的介绍了(链接如下)
http://blog.csdn.net/u010466329/article/details/75020956
https://github.com/searchbox-io/Jest结合源代码封装了一个可以直接传递url的method,该方法的实现就是结合了jest 源码,实现了自己想要的接口(用于用户页面自定义查询 更方便)。 代码如下:
AbstractLocalHttpAction (本地HttpAction)
/** * methodName "http" "delete" "put" "get" “post” * queryParam 参数 * url 请求链接 */ public abstract class AbstractLocalHttpAction <T extends JestResult> extends AbstractAction<T> { protected String methodName; private String queryParam; private String url; public AbstractLocalHttpAction(AbstractLocalHttpAction.Builder builder) { super(builder); this.queryParam = builder.queryParam; this.methodName = builder.methodName; this.url = builder.url; } public String getMethodName() { return this.methodName; } public String getQueryParam() { return this.queryParam; } public String getUrl() { return this.url; } protected String buildURI() { StringBuilder sb = new StringBuilder(super.buildURI()); return sb.toString(); } protected abstract static class Builder<T extends AbstractLocalHttpAction, K> extends io.searchbox.action.AbstractAction.Builder<T, K> { private String url; private String methodName; private String queryParam; protected Builder() { } public K url(String url) { this.url = url; return (K)this; } public K methodName(String methodName) { this.methodName = methodName; return (K)this; } public K queryParam(String queryParam) { this.queryParam = queryParam; return (K)this; } } }
LocalResultAbstractAction
public abstract class LocalResultAbstractAction extends AbstractLocalHttpAction<JestResult> {
public LocalResultAbstractAction(Builder builder) {
super(builder);
}
public JestResult createNewElasticSearchResult(String responseBody, int statusCode, String reasonPhrase, Gson gson) {
return (JestResult)this.createNewElasticSearchResult(new JestResult(gson), responseBody, statusCode, reasonPhrase, gson);
}
}
JestLocalHttpClient(用法build入口)
public class JestLocalHttpClient extends LocalResultAbstractAction {
private String query;
protected JestLocalHttpClient(JestLocalHttpClient.Builder builder) {
super(builder);
this.setURI(this.buildURI()+""+getUrl());
this.query = getQueryParam();
}
public String getRestMethodName() {
return getMethodName();
}
public String getData(Gson gson) {
String data = this.query;
return data;
}
public static class Builder extends AbstractLocalHttpAction.Builder<JestLocalHttpClient, JestLocalHttpClient.Builder> {
public Builder(String url, String methodName,String queryParam) {
this.url(url);
this.methodName(methodName);
this.queryParam(queryParam);
}
public JestLocalHttpClient build() {
return new JestLocalHttpClient(this);
}
}
}
jestManager 封装如下
public class JestManager {
private static final Logger LOGGER = LoggerFactory.getLogger(JestManager.class);
/**
* 获取JestClient对象
* @return
*/
public static JestClient getJestClient(String clustName) {
JestClientFactory factory = new JestClientFactory();
Cluster cluster = CLUSTERMAP.get(clusterName);
try {
notes.add("http://"+cluster.getHost()+":"+cluster.getHttprt());
HttpClientConfig.Builder httpClientConfig = new HttpClientConfig
.Builder(notes)
.connTimeout(1500)
.readTimeout(3000)
.multiThreaded(true);
factory.setHttpClientConfig(httpClientConfig.build());
}catch (Exception e){
e.printStackTrace();
LOGGER.error("初始化jestclient实例失败:"+e.getMessage());
}
return factory.getObject();
}
}
上面自己的封装方法调用如下
public JestResult httpProxy(String clustName, String url, String methodName, String queryParam) {
JestResult result = null ;
try {
JestLocalHttpClient jestLocalHttpClient = new JestLocalHttpClient.Builder(url,methodName,queryParam).build();
result = JestManager.getJestClient(clustName).execute(jestLocalHttpClient);
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("jestLocalHttpClient失败:"+e.getStackTrace());
}
return result ;
}
测试如下
url:/demo/_search?pretty
method:get
queryParam:null
调用接口:返回信息
"{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
"
jest 用起来很方便,方法的封装让我们代码写起来更为简单。以上的内容希望能对大家有所帮助。