bboss ES SQL是针对es jdbc的替代解决方案
the best elasticsearch highlevel java rest api-----bboss
bboss 提供一组sql和fetchQuery API,可替代官方es jdbc模块;采用bboss即可拥有bboss的客户端自动发现和容灾能力、对es、jdk、spring boot的兼容性能力,又可以拥有es jdbc的所有功能,同时还解决了因为引入es jdbc导致项目对es版本的强依赖和兼容性问题,参考demo:
目前官方es sql提供的功能有限,也可以在bboss中使用Elasticsearch-sql插件提供的功能,下面有专门的章节介绍。
本文详细说明上面的案例:
集合orm查询
单文orm档查询
fetchsize orm查询
1 orm查询
1.1 定义orm查询的实体bean
package org.bboss.elasticsearchtest.sql;
import com.frameworkset.orm.annotation.Column;
import java.util.Date;
public class DocObject {
private int isnew;
private Date createtime;
private String content;
private int documentId;
private int channelId;
/**
* 通过column指定索引文档和对象属性的映射关系
* 通过column注解还可以指定日期格式和时区信息
* @Column(name="docInfo.author",dataformat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",timezone = "Etc/UTC",locale = "zh")
*
*/
@Column(name="docInfo.author")
private String docInfoAuthor;
public int getIsnew() {
return isnew;
}
public void setIsnew(int isnew) {
this.isnew = isnew;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getDocumentId() {
return documentId;
}
public void setDocumentId(int documentId) {
this.documentId = documentId;
}
public int getChannelId() {
return channelId;
}
public void setChannelId(int channelId) {
this.channelId = channelId;
}
public String getDocInfoAuthor() {
return docInfoAuthor;
}
public void setDocInfoAuthor(String docInfoAuthor) {
this.docInfoAuthor = docInfoAuthor;
}
}
实体定义说明:
通过column指定索引文档和对象属性的映射关系,指定日期格式和时区信息,示例如下:
@Column(name="docInfo.author")
private String docInfoAuthor;
指定属性的映射关系、日期格式和时区信息,示例如下:
@Column(name="docInfo.author",dataformat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",timezone = "Etc/UTC",locale = "zh")
执行orm查询
以rest sql api为例来介绍es 6.3.0的sql orm查询功能
package org.bboss.elasticsearchtest.sql;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.sql.SQLRestResponse;
import org.frameworkset.elasticsearch.entity.sql.SQLRestResponseHandler;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 以rest sql api为例来介绍es 6.3.0的sql orm查询功能
*/
public class SQLOrmTest {
/**
* 代码中的sql检索,返回Map类型集合,亦可以返回自定义的对象集合
*/
@Test
public void testDemoQuery(){
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
List json = clientUtil.sql(Map.class,"{\"query\": \"SELECT * FROM demo\