jest for elasticsearch
*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 用起来很方便,方法的封装让我们代码写起来更为简单。以上的内容希望能对大家有所帮助。
jest for elasticsearch的更多相关文章
- 【Es】jest操作elasticsearch
https://blog.csdn.net/niuchenliang524/article/details/82869319 操作es的客房端有多个,在此例出三种(具体区别自行百度),本文讲的是jes ...
- jest操作 Elasticsearch
package com.lgmall.search; import com.lgmall.search.esEntity.Article;import com.lgmall.search.esEnti ...
- springboot集成elk 四:springboot + Elasticsearch+Jest
依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...
- Spring Boot 整合 elasticsearch
一.简介 我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的 首选.他可以快速的存储.搜索和分析海量数据.Spring Boot通过整合Spring Data E ...
- SpringBoot笔记十六:ElasticSearch
目录 ElasticSearch官方文档 ElasticSearch安装 ElasticSearch简介 ElasticSearch操作数据,RESTful风格 存储 检查是否存在 删除 查询 更新 ...
- ELK学习笔记之ElasticSearch简介
0x00 什么是Elasticsearch Elasticsearch (ES)是一个基于 Lucene 的开源搜索引擎,它不但稳定.可靠.快速,而且也具有良好的水平扩展能力,是专门为分布式环境设计的 ...
- Elasticsearch Java API深入详解
0.题记 之前Elasticsearch的应用比较多,但大多集中在关系型.非关系型数据库与Elasticsearch之间的同步.以上内容完成了Elasticsearch所需要的基础数据量的供给.但想要 ...
- 3.2_springBoot2.1.x检索之JestClient操作ElasticSearch
这里介绍Jest方式交互, 导入jest版本 <!--导入jest--> <dependency> <groupId>io.searchbox</groupI ...
- spring boot application.properties 属性详解
2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...
随机推荐
- 计算生日是星期几-soj
编写一个程序,只要输入年月日,就能回答那天是星期几. 输入一个日期,包括年.月.日.(一组测试数据) 输出这个日期是星期几. 输入: 1 1 1 2 1 1 2006 7 10 输出: Monday ...
- NGUI_01
序言:这是张三疯第一次开始NGUI插件的学习,刚开始学习,肯定有很多漏洞,后期会及时的补上的.希望大家可以见谅,希望大佬多多指教. 扩充:为提供和我一样的小白找不到免费的NGUI插件,这里分享百度网盘 ...
- 爬虫day 04_01(爬百度页面)
import urllib.request import http.cookiejar from lxml import etree head = { 'Connection': 'Keep-Aliv ...
- WCF、WebAPI、WCFREST、WebService之间的区别和选择
转载翻译,原文:http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-a ...
- LINQ学习系列-----3.1 查询非泛型集合
一.问题起源 LINQ to object在设计时,是配合IEnumerable<T>接口的泛型集合类型使用的,例如字典.数组.List<T>等,但是对于继承了IEnumera ...
- 自己动手实现mvc框架
用过springmvc的可能都知道,要集成springmvc需要在web.xml中加入一个跟随web容器启动的DispatcherServlet,然后由该servlet初始化一些东西,然后所有的web ...
- Azure 基础:使用 Traffic Manager 分流用户请求
为了减少 web 服务器的宕机时间,同时也提高服务器的响应性能,我们往往部署多个站点并通过负载均衡来对外提供服务.Azure 提供的 Traffic Manager 服务属于负载均衡的一种,特点是工作 ...
- yii框架开启事务
public function actionAdd() { $model = new Goods(); $model->setScenario('insert'); if ($model-> ...
- java 接口测试
学习java 时间也不算太长,大学有点基础,学起来也顺手多了,其实我感觉更是因为学习完python之后,在学习别的语言的时候就简单多了,几乎所有的语言学起来都差不多.前几篇写过的都是python接口测 ...
- Redis在Linux安装详细步骤
一.准备环境: 1.VMware 2.CentOS 3.redis-3.0.4.tar.gz 4.下载地址 Http://redis.io/ 英文版 Http://www.redis.cn/ 中 ...