SQL to Elasticsearch java code

把Elasticsearch当成Database用,因为Elasticsearch不支持SQL,就需要把SQL转换成代码实现。

1.按某个field group by查询count

SELECT
fieldA, COUNT(fieldA)
from table
WHERE fieldC = "hoge"
AND fieldD = "huga"
AND fieldB > 10
AND fieldB < 100
group by fieldA;

对应的java code:

SearchRequestBuilder searchReq = client.prepareSearch("sample_index");
searchReq.setTypes("sample_types");
TermsBuilder termsb = AggregationBuilders.terms("my_fieldA").field("fieldA").size(100);

BoolFilterBuilder bf = FilterBuilders.boolFilter();
TermFilterBuilder tf_fieldC = FilterBuilders.termFilter("fieldC","hoge");
TermFilterBuilder tf_fieldD = FilterBuilders.termFilter("fieldD","huga");
bf.must(tf_fieldC);
bf.must(tf_fieldD);

RangeFilterBuilder rangefieldBFilter = FilterBuilders.rangeFilter("fieldB")
                .gt(10)
                .lt(100);

searchReq.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
                    FilterBuilders.andFilter(bf, rangefieldBFilter))).addAggregation(
                    termsb);
SearchResponse searchRes = searchReq.execute().actionGet();

Terms fieldATerms = searchRes.getAggregations().get("my_fieldA");
for (Terms.Bucket filedABucket : fieldATerms.getBuckets()) {
    //fieldA
    String fieldAValue = filedABucket.getKey();

    //COUNT(fieldA)
    long fieldACount = filedABucket.getDocCount();
}

2. 按某个field 和 date group by 并查询sum,时间统计图,时间间隔是1天。

SELECT
DATE(create_at), fieldA, SUM(fieldB)
from table
group by DATE(create_at), fieldA;

对应的java code:

SearchRequestBuilder searchReq = client.prepareSearch("sample_index");
searchReq.setTypes("sample_types");
DateHistogramBuilder dhb = AggregationBuilders.dateHistogram("my_datehistogram").field("create_at").interval(DateHistogram.Interval.days(1));
TermsBuilder termsb_fa = AggregationBuilders.terms("my_fieldA").field("fieldA").size(100);
termsb_fa.subAggregation(AggregationBuilders.sum("my_sum_fieldB").field("fieldB"));
dhb.subAggregation(termsb_fa)

searchReq.setQuery(QueryBuilders.matchAllQuery()).addAggregation(dhb);
SearchResponse searchRes = searchReq.execute().actionGet();

DateHistogram dateHist = searchRes.getAggregations().get("my_datehistogram");
for (DateHistogram.Bucket dateBucket : dateHist.getBuckets()) {
    //DATE(create_at)
    String create_at = dateentry.getKey();
    Terms fieldATerms = dateBucket.getAggregations().get("my_fieldA");
    for (Terms.Bucket filedABucket : fieldATerms.getBuckets()) {
        //fieldA
        String fieldAValue = filedABucket.getKey();

        //SUM(fieldB)
        Sum sumagg = filedABucket.getAggregations().get("my_sum_fieldB");
        long sumFieldB = (long)sumagg.getValues();
    }
}

3. 按两个field group by并查询sum

SELECT
fieldA, fieldC, SUM(fieldB)
from table
group by fieldA, fieldC;

对应的java code:

SearchRequestBuilder searchReq = client.prepareSearch("sample_index");
searchReq.setTypes("sample_types");

TermsBuilder termsb_fa = AggregationBuilders.terms("my_fieldA").field("fieldA").size(100);
TermsBuilder termsb_fc = AggregationBuilders.terms("my_fieldC").field("fieldC").size(50);

termsb_fc.subAggregation(AggregationBuilders.sum("my_sum_fieldB").field("fieldB"));
termsb_fa.subAggregation(termsb_fc)

searchReq.setQuery(QueryBuilders.matchAllQuery()).addAggregation(termsb_fa);
SearchResponse searchRes = searchReq.execute().actionGet();

Terms fieldATerms = searchRes.getAggregations().get("my_fieldA");
for (Terms.Bucket filedABucket : fieldATerms.getBuckets()) {
    //fieldA
    String fieldAValue = filedABucket.getKey();
    Terms fieldCTerms = filedABucket.getAggregations().get("my_fieldC");
    for (Terms.Bucket filedCBucket : fieldCTerms.getBuckets()) {
        //fieldC
        String fieldCValue = filedCBucket.getKey();

        //SUM(fieldB)
        Sum sumagg = filedCBucket.getAggregations().get("my_sum_fieldB");
        long sumFieldB = (long)sumagg.getValues();
    }
}

4. 按某个filed group by 并查询count sum 和 average

SELECT
fieldA, COUNT(fieldA), SUM(fieldB), AVG(fieldB)
from table
group by fieldA;

对应的java code:

SearchRequestBuilder searchReq = client.prepareSearch("sample_index");
searchReq.setTypes("sample_types");

TermsBuilder termsb = AggregationBuilders.terms("my_fieldA").field("fieldA").size(100);
termsb.subAggregation(AggregationBuilders.sum("my_sum_fieldB").field("fieldB"));
termsb.subAggregation(AggregationBuilders.avg("my_avg_fieldB").field("fieldB"));

searchReq.setQuery(QueryBuilders.matchAllQuery()).addAggregation(termsb);
SearchResponse searchRes = searchReq.execute().actionGet();
Terms fieldATerms = searchRes.getAggregations().get("my_fieldA");
for (Terms.Bucket filedABucket : fieldATerms.getBuckets()) {
    //fieldA
    String fieldAValue = filedABucket.getKey();

    //COUNT(fieldA)
    long fieldACount = filedABucket.getDocCount();

    //SUM(fieldB)
    Sum sumagg = filedABucket.getAggregations().get("my_sum_fieldB");
    long sumFieldB = (long)sumagg.getValues();

    //AVG(fieldB)
    Avg avgagg = filedABucket.getAggregations().get("my_avg_fieldB");
    double avgFieldB = avgagg.getValues();
}
时间: 2024-10-19 18:28:59

SQL to Elasticsearch java code的相关文章

SQL state [72000]; error code [1013]; ORA-03111: 通信通道收到中断; java.sql.SQLException: ORA-01745: 无效的主机/绑定变量名;java.sql.SQLException: ORA-01013: 用户请求取消当前的操作

1.oracle批量插入 2.java.sql.SQLException: ORA-01745: 无效的主机/绑定变量名(数据太多的时候出现解决方法限制条数插入) /** * 按指定大小,分隔集合,将集合按规定个数分为n个部分 * @param <T> * * @param list * @param len * @return */ public static <T> List<List<T>> splitList(List<T> list,

java.sql.date与java.util.date区别以及数据库中插入带时分秒的时间

java.sql.Date,java.sql.Time和java.sql.Timestamp三个都是java.util.Date的子类(包装类). java.sql.Date是java.util.Date的子类,是一个包装了毫秒值的瘦包装器,允许 JDBC 将毫秒值标识为 SQL DATE 值.毫秒值表示自 1970 年 1 月 1 日 00:00:00 GMT 以来经过的毫秒数. 为了与 SQL DATE 的定义一致,由 java.sql.Date 实例包装的毫秒值必须通过将时间.分钟.秒和毫

JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法[转]

1.如何将java.util.Date转化为java.sql.Date? 转化: java.sql.Date sd; java.util.Date ud; //initialize the ud such as ud = new java.util.Date(); sd = new java.sql.Date(ud.getTime()); 2.如何将获取到的java.sql.Date转换为年-月-日输出 java.sql.Date sd; String dateTime = sd.toStrin

ElasticSearch Java Api -创建索引

ElasticSearch JAVA API官网文档:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html 一.生成JSON 创建索引的第一步是要把对象转换为JSON字符串.官网给出了四种创建JSON文档的方法: 1.1手写方式生成 String json = "{" + "\"user\":\"kimchy\"

SQL state [HY000]; error code [3]; 问题分析

工作流表单采用了freemarker技术将表单与后台数据库绑定,每次修改表单定义将会对该表单对应的数据库执行alter操作.今天测试系统在执行保存表单定义操作时,报如下错误: HTTP Status 500 - StatementCallback; uncategorized SQLException for SQL [ALTER TABLE T_DYMC_20140522141004 CHANGE f_xqdbm f_xqdbm VARCHAR(50) COMMENT '需求单编码';]; S

Java Code Convention Rules

Rules available in this category: Avoid_subclassing_java_lang_Thread Always_declare_a_class_having_only_private_constructors_as_final Replace_enumeration_with_iterator Combine_if_statements_using_boolean_short_circuit_operator Avoid_using_fully_quali

ElasticSearch Java Api -检索索引库

上篇博客记录了如何用java调用api把数据写入索引,这次记录下如何搜索. 一.准备数据 String data1 = JsonUtil.model2Json(new Blog(1, "git简介", "2016-06-19", "SVN与Git最主要的区别...")); String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介绍与简单使用", "2016-0

在线数据库表(sql语句)生成java实体类工具

相信每个做java开发的读者,都接触过SQL建表语句,尤其是在项目开发初期,因为数据库是项目的基石. 在现代项目开发中,出现了许多ORM框架,通过简单的实体映射,即可实现与数据库的交互,然而我们最初设计的一定是数据库表结构,而不是实体类.实体类仅仅是对底层数据结构的有损压缩,它仅仅是数据载体,不具备数据归档能力. 因此,很多时候,我们需要将原始的SQL建表语句转换成java实体类,这项工作看似简单,但若人工完成,工作量也是相当可观的,而且难免会出现差错. 到目前为止,笔者还没有发现比较靠谱的此类

How to generate UML Diagrams from Java code in Eclipse

UML diagrams compliment inline documentation ( javadoc ) and allow to better explore / understand a design. Moreover, you can print and bring them to table to discuss a design.In this post, we will install and use the ObjectAid plugin for Eclipse to