问题介绍
要在 Java 代码中实现类似 SQL 中的 GroupBy 分组聚合运算,是比较繁琐的,通常先要声明数据结构(Java 实体类),然后用 Java 集合进行循环遍历,最后根据分组条件添加到某个子集合中。Java 8 有了 Lambda(stream)代码简洁了许多,分组后往往还要跟着聚合操作,仍然需要单写聚合函数 sum(),count(*),topN()等。这些还都是最常规的分组和聚合运算,遇到对位分组、枚举分组、多重分组等非常规分组加上其他聚集函数 (FIRST,LAST…),代码就变得非常冗长且不通用。如果能有一个中间件专门负责这类计算,采用类似 SQL 脚本做算法描述,在 Java 中直接调用脚本并返回结果集就好了。Java 版集算器和 SPL 脚本,就是这样的机制,下面举例说明如何使用。
SPL 实现
- 常规分组
duty.xlsx 文件中保存着每个人的加班记录:
![6609b089456e5615f5c4338d6ca6ec49.png](https://i-blog.csdnimg.cn/blog_migrate/e4222c5ba6fb5cc56e8a40d0cb0a07c8.png)
汇总每个人的值班天数:
![3d3c8e54ffdfbf977c48ca0be6a63ac6.png](https://i-blog.csdnimg.cn/blog_migrate/dff8316d81ad1d9fdde25b8cc4c20917.png)
![9799025cde7d644d13a5a9b9e0dcad8e.png](https://i-blog.csdnimg.cn/blog_migrate/e4c123e290d801838056c0a09b367aef.png)
保存脚本文件CountName.dfx(嵌入 Java 会用到)
- 每组 TopN
取每个月、每个人、头三天的加班记录
![748a665f7e410a5f5a2c32777e48949e.png](https://i-blog.csdnimg.cn/blog_migrate/197471aa512f5187c5677b62071f2cfa.png)
![d0c7c213d24b5e47b84364f51a1eadf6.png](https://i-blog.csdnimg.cn/blog_migrate/2b2258c53187be14163b92126001f69d.png)
保存脚本文件RecMonTop3.dfx(嵌入 Java 会用到)
Java 调用
SPL 嵌入到 Java 应用程序十分方便,通过 JDBC 调用存储过程方法加载,用常规分组保存的文件CountName.dfx,示例调用如下:
...
Connection con = null;
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
//调用存储过程,其中CountName是dfx的文件名
st =(com. esproc.jdbc.InternalCStatement)con.prepareCall("call CountName()");
//执行存储过程
st.execute();
//获取结果集
ResultSet rs = st.getResultSet();
...
替换成 RecMonTop3.dfx 是同样的道理,只需 call RecMonTop3() 即可,也可同时返回两个结果集。这里只用 Java 片段粗略解释了如何嵌入 SPL,详细步骤请参阅 Java 如何调用 SPL 脚本 ,也非常简单,不再赘述。同时,SPL 也支持 ODBC 驱动,集成到支持 ODBC 的语言,嵌入过程类似。
拓展节选
之前没有相关的总结,其实关于数据分组,细分起来其实还有很多种,对位分组、枚举分组、多重分组…,在乾学院 SPL 官方论坛都有总结和示例,这里节选其中两种。
- SPL 对位分组
示例 1:按顺序分别列出使用 Chinese、English、French 作为官方语言的国家数量
MySQL8:
with t(name,ord) as (select 'Chinese',1
union all select 'English',2
union all select 'French',3)
select t.name, count(countrycode) cnt
from t left join world.countrylanguage s on t.name=s.language
where s.isofficial='T'
group by name,ord
order by ord;
注意:表的字符集和数据库会话的字符集要保持一致。
(1) show variables like ’character_set_connection’查看当前会话字符集
(2) show create table world.countrylanguage 查看表的字符集
(3) set character_set_connection=[字符集] 更新当前会话字符集
集算器 SPL:
![9b46bb0a9da6b7614e0a3d023aec9387.png](https://i-blog.csdnimg.cn/blog_migrate/4d1e6df89431e2f69f73a54e83fa3928.png)
A1: 连接数据库
A2: 查询出所有官方语言的记录
A3: 需要列出的语言
A4: 将所有记录按 Language 对位到 A3 相应位置
A5: 构造以语言和使用此语言为官方语言的国家数量的序表
![4d30a88a16ff3286b0fa9940429d1542.png](https://i-blog.csdnimg.cn/blog_migrate/18de9f5e12029a27ecf874d64270971f.png)
示例 2:按顺序分别列出使用 Chinese、English、French 及其它语言作为官方语言的国家数量
MySQL8:
with t(name,ord) as (select 'Chinese',1 union all select 'English',2
union all select 'French',3 union all select 'Other', 4),
s(name, cnt) as (
select language, count(countrycode) cnt
from world.countrylanguage s
where s.isofficial='T' and language in ('Chinese','English','French')
group by language
union all
select 'Other', count(distinct countrycode) cnt
from world.countrylanguage s
where isofficial='T' and language not in ('Chinese','English','French')
)
select t.name, s.cnt
from t left join s using (name)
order by t.ord;
集算器 SPL:
![cff34a9297256da624f295c7199ba0f7.png](https://i-blog.csdnimg.cn/blog_migrate/e2a28011da9beefd796b3940d1edf7c2.png)
A4: 将所有记录按 Language 对位到 A3.to(3) 相应位置,并追加一组用于存放不能对位的记录
A5: 第 4 组计算不同 CountryCode 的数量
![c327635c18a56133eb754611bca0e70a.png](https://i-blog.csdnimg.cn/blog_migrate/c4a451f6ea53cd998b5ff281a2b3d502.png)
- SPL 枚举分组
示例 1:按顺序列出各类型城市的数量
MySQL8:
with t as (select * from world.city where CountryCode='CHN'),
segment(class,start,end) as (select 'tiny', 0, 200000
union all select 'small', 200000, 1000000
union all select 'medium', 1000000, 2000000
union all select 'big', 2000000, 100000000
)
select class, count(1) cnt
from segment s join t on t.population>=s.start and t.population<s.end
group by class, start
order by start;
集算器 SPL:
![bae69620076c7db7f43c471bd53e785e.png](https://i-blog.csdnimg.cn/blog_migrate/b8cf1118b5cd450ed557133f77f07f35.png)
A3: ${…} 宏替换,以大括号内表达式的结果作为新表达式进行计算,结果为序列 [“?<200000”,“?<1000000”,“?<2000000”,“?<100000000”]
A5: 针对 A2 中每条记录,寻找 A3 中第 1 个成立的条件,并追加到对应的组中
![ce8992a72cdb16406b657848b59ddbc9.png](https://i-blog.csdnimg.cn/blog_migrate/017110c1d7886fb02bd6e2b451639a9e.png)
示例 2:列出华东地区大型城市数量、其它地区大型城市数量、非大型城市数量
MySQL8:
with t as (select * from world.city where CountryCode='CHN')
select 'East&Big' class, count(*) cnt
from t
where population>=2000000
and district in ('Shanghai','Jiangshu', 'Shandong','Zhejiang','Anhui','Jiangxi')
union all
select 'Other&Big', count(*)
from t
where population>=2000000
and district not in ('Shanghai','Jiangshu','Shandong','Zhejiang','Anhui','Jiangxi')
union all
select 'Not Big', count(*)
from t
where population<2000000;
集算器 SPL:
![8efdda011334d764e03537ab09ae6e31.png](https://i-blog.csdnimg.cn/blog_migrate/64a7dda73ef0f68d9dbc36969f45e16b.png)
A5: enum@n 将不满足 A4 中所有条件的记录存放到追加的最后一组中
![e7df740dd13221655cfd43aba9630cfd.png](https://i-blog.csdnimg.cn/blog_migrate/cecb8e736e450c5a53a97b8ac8e8a239.png)
示例 3:列出所有地区大型城市数量、华东地区大型城市数量、非大型城市数量
MySQL8:
with t as (select * from world.city where CountryCode='CHN')
select 'Big' class, count(*) cnt
from t
where population>=2000000
union all
select 'East&Big' class, count(*) cnt
from t
where population>=2000000
and district in ('Shanghai','Jiangshu','Shandong','Zhejiang','Anhui','Jiangxi')
union all
select 'Not Big' class, count(*) cnt
from t
where population<2000000;
集算器 SPL:
![bf782cec975258c98bc91a573e2433a6.png](https://i-blog.csdnimg.cn/blog_migrate/ed1a8e06e4dab8bb28396c9fbeb24087.png)
A6: 若 A2 中记录满足 A4 中多个条件时,enum@r 会将其追加到对应的每个组中
![a42a94d83381f21a3f031a53ce17f7d6.png](https://i-blog.csdnimg.cn/blog_migrate/f5de0e63bb320b1661d6342bd19ffada.png)
优势总结
- 有库写 SQL,没库写 SPL
用 Java 程序直接汇总计算数据,还是比较累的,代码很长,并且不可复用,很多情况数据也不在数据库里,有了 SPL,就能像在 Java 中用 SQL 一样了,十分方便。 - 技术文档和社区支持
官方提供的集算器技术文档本身就有很多现成的例子,常规问题从文档里都能找到解决方法。如果获得了入门版,不仅能够使用 SPL 的常规功能,碰到任何问题都可以去迈安科技上去咨询,官方通过该社区对入门版用户提供免费的技术支持。