Elasticsearch虽然定位为Search Engine,但是因其可以持久化数据,很多时候,我们把Elasticsearch当成Database用,但是Elasticsearch不支持SQL,就需要把SQL逻辑转换成代码实现对应的功能。

以下列举了一些常用的SQL转换成对应的Java代码。

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 并查询另一个filed的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并查询第三个filed的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();
}

5. 按某个field group by 并按另一个filed的Sum排序,获取前10

SELECT
fieldA, SUM(fieldB)
from table
WHERE fieldC = "hoge"
group by fieldA
order by SUM(fieldB) DESC
limit 10;

对应的java code:

QueryBuilder termsc = QueryBuilders.termQuery("fieldC","hoge");
QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(termsc);
TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("my_fieldA").field("fieldA").size(10);
aggregationBuilder.subAggregation(AggregationBuilders.sum("my_sum_fieldB").field("fieldB"));
aggregationBuilder.order(Order.aggregation("my_sum_fieldB", false));
SearchResponse searchResponse = client.prepareSearch("sample_index").setQuery(queryBuilder).addAggregation(aggregationBuilder).execute().actionGet();
Terms terms = searchResponse.getAggregations().get("my_fieldA");
for (Terms.Bucket entry : terms.getBuckets()) {
String fieldAValue = entry.getKey().toString(); Sum sumagg = entry.getAggregations().get("my_sum_fieldB");
double fieldValue = sumagg.getValue();
}

代码在GitHub上:https://github.com/luxiaoxun/Code4Java

SQL to Java code for Elasticsearch的更多相关文章

  1. Java中使用elasticsearch搜索引擎实现简单查询、修改等操作-已在项目中实际应用

    以下的操作环境为:jdk:1.8:elasticsearch:5.2.0 maven架包下载坐标为: <dependency> <groupId>org.elasticsear ...

  2. A java code

    With the help of LiJun I got a piece of JAVA code. With this code, I can do below things like connec ...

  3. Elasticsearch入门系列~通过Java一系列操作Elasticsearch

    Elasticsearch索引的创建.数据的增删该查操作 上一章节已经在Linux系统上安装Elasticsearch并且可以外网访问,这节主要通过Java代码操作Elasticsearch 1.创建 ...

  4. Java语言编码规范(Java Code Conventions)

    Java语言编码规范(Java Code Conventions) 名称 Java语言编码规范(Java Code Conventions) 译者 晨光(Morning) 简介 本文档讲述了Java语 ...

  5. java code to byte code--partone--reference

    Understanding how Java code is compiled into byte code and executed on a Java Virtual Machine (JVM) ...

  6. [转]Java Code Examples for android.util.JsonReader

    [转]Java Code Examples for android.util.JsonReader The following are top voted examples for showing h ...

  7. JUnit单元测试教程(翻译自Java Code Geeks)

    JUnit单元测试教程--终极指南 JUnit单元测试教程终极指南 说明 单元测试简介 1 什么是单元测试 2 测试覆盖 3 Java中的单元测试 JUnit简介 1 使用Eclipse实现简单JUn ...

  8. Java Code Style

    近期困惑于团队成员代码风格迥异,代码质量不可控,作为一名老司机,忧患于后期服务的可维护性,多次一对一的代码Review,耗时耗力不说,效果也不明显.痛定思痛,多次反思之后得出结论:无规矩不成方圆,可靠 ...

  9. 使用Java客户端操作elasticsearch(二)

    承接上文,使用Java客户端操作elasticsearch,本文主要介绍 常见的配置 和Sniffer(集群探测) 的使用. 常见的配置 前面已介绍过,RestClientBuilder支持同时提供一 ...

随机推荐

  1. C++环境搭建与atom编译器编译C++

    Windows下安装 方法一--VS: 使用windows开发神器visio studio.这种方法比较简单,直接下载一个最新的vs安装就行.不单单是C++,C.C#.VB等都可以开发. 方法二--只 ...

  2. spring boot多环境配置 直接上代码

    spring:  profiles:    active: test  jackson:    date-format: yyyy-MM-dd HH:mm:ss  datasource:    dri ...

  3. NOIP2017划水崩盘记

    Before-Day1              自信心爆棚,老子一定能拿省一.一天啥也没干,一顿乱奶.敬等明日切T1写暴力. Day 1 哈哈哈,T1是数论,闭眼睛切啊!! ...然后就Gg了,写的 ...

  4. Hibernate学习笔记二

    Hibernate持久化类的编写规则 Hibernate是持久层的ORM映射框架,专注于数据的持久化工作.所谓持久化,就是将内存中的数据永久存储到关系型数据库中. 持久化类 一个java类与数据库表建 ...

  5. 【RabbitMQ系列】队列、绑定、交换器

    队列: 从概念上来讲,AMQP消息路由必须有三部分:交换器.队列和绑定.生产者把消息发布到交换器上:消息最终到达队列,并被消费者接收:绑定决定了消息如何从路由器路由到特定的队列. 消费者通过以下两种方 ...

  6. 使用linux下的crontab定时任务跑定时脚本

    使用linux下的crontab定时任务跑定时脚本 tags:定时任务 定时脚本 crontab linux定时脚本 linux 引言:应该有许多人曾经很好奇一些定时脚本是怎么做出来的.我们这次就来说 ...

  7. 敏捷冲刺每日报告四(Java-Team)

    第四天报告(10.28  周六) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://gi ...

  8. 项目Alpha冲刺Day8

    一.会议照片 二.项目进展 1.今日安排 前端界面框架基本完成,剩下侧边栏与权限相关部分未完成.前端路由异常拦截完成.项目结构与开发流程规定完成.后台开发规定小变更. 2.问题困难 组件的拆分与否和组 ...

  9. selenium 爬虫

    from selenium import webdriver import time driver = webdriver.PhantomJS(executable_path="D:/pha ...

  10. 玩转Leveldb原理及源码--拙见1

    可以说是不知天高地厚.. 可以说是班门弄斧.. 但是,我今天还就这样走了,我喜欢!!!!!! 注:后续文章,限于篇幅,不懂名词都有 紫色+下划线 超链接,有兴趣,可以查阅: 网上关于Leveldb 的 ...