Java 嵌入 SPL 輕松實現數據分組

問題介紹

要在 Java 代碼中實現類似 SQL 中的 GroupBy 分組聚合運算,是比較繁瑣的,通常先要聲明數據結構(Java 實體類),然后用 Java 集合進行循環遍歷,最后根據分組條件添加到某個子集合中。Java 8 有了 Lambda(stream)代碼簡潔了許多,分組后往往還要跟著聚合操作,仍然需要單寫聚合函數 sum(),count(*),topN()等。這些還都是最常規的分組和聚合運算,遇到對位分組、枚舉分組、多重分組等非常規分組加上其他聚集函數 (FIRST,LAST…),代碼就變得非常冗長且不通用。如果能有一個中間件專門負責這類計算,采用類似 SQL 腳本做算法描述,在 Java 中直接調用腳本并返回結果集就好了。Java 版集算器和 SPL 腳本,就是這樣的機制,下面舉例說明如何使用。

SPL 實現

常規分組

duty.xlsx 文件中保存著每個人的加班記錄:

匯總每個人的值班天數:

保存腳本文件CountName.dfx(嵌入 Java 會用到)

每組 TopN

取每個月、每個人、頭三天的加班記錄

保存腳本文件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();

...

...

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;

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:

A1: 連接數據庫

A2: 查詢出所有官方語言的記錄

A3: 需要列出的語言

A4: 將所有記錄按 Language 對位到 A3 相應位置

A5: 構造以語言和使用此語言為官方語言的國家數量的序表

示例 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;

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:

A4: 將所有記錄按 Language 對位到 A3.to(3) 相應位置,并追加一組用于存放不能對位的記錄

A5: 第 4 組計算不同 CountryCode 的數量

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;

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:

A3: ${…} 宏替換,以大括號內表達式的結果作為新表達式進行計算,結果為序列 [“?<200000”,“?<1000000”,“?<2000000”,“?<100000000”]

A5: 針對 A2 中每條記錄,尋找 A3 中第 1 個成立的條件,并追加到對應的組中

示例 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;

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:

A5: enum@n 將不滿足 A4 中所有條件的記錄存放到追加的最后一組中

示例 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;

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:

A6: 若 A2 中記錄滿足 A4 中多個條件時,enum@r 會將其追加到對應的每個組中

優勢總結

有庫寫 SQL,沒庫寫 SPL

用 Java 程序直接匯總計算數據,還是比較累的,代碼很長,并且不可復用,很多情況數據也不在數據庫里,有了 SPL,就能像在 Java 中用 SQL 一樣了,十分方便。

常用無憂,不花錢就能取得終身使用權的入門版

如果要分析的數據是一次性或臨時性的,潤乾集算器每個月都提供免費試用授權,可以循環免費使用。但要和 Java 應用程序集成起來部署到服務器上長期使用,定期更換試用授權還是比較麻煩,潤乾提供了有終身使用權的入門版,解決了這個后顧之憂,獲得方式參考 如何免費使用潤乾集算器?

技術文檔和社區支持

官方提供的集算器技術文檔本身就有很多現成的例子,常規問題從文檔里都能找到解決方法。如果獲得了入門版,不僅能夠使用 SPL 的常規功能,碰到任何問題都可以去乾學院上去咨詢,官方通過該社區對入門版用戶提供免費的技術支持。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,786評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,656評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,697評論 0 379
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,098評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,855評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,254評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,322評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,473評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,014評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,833評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,016評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,568評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,273評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,680評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,946評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,730評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,006評論 2 374

推薦閱讀更多精彩內容

  • 轉 # https://www.cnblogs.com/easypass/archive/2010/12/ 08/...
    呂品?閱讀 9,759評論 0 44
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,392評論 0 10
  • 告別了2016年,見證了詹姆斯為自己的城市帶來了總冠軍,不僅僅他在這充滿奇跡的一年收獲,結果。 你我都是。 第一次...
    呵呵樂邦閱讀 358評論 0 0
  • #微寫作# Day4 最近公司里變動有點大,金三銀四身邊陸陸續續走了幾個朋友,其中就有兩個飯友,哎,其實自己有時候...
    芙筱筱Elaine閱讀 117評論 2 0
  • 能幫阿姨拿著拍照嗎?”當然可以,這是玫瑰”,她用普通話回答我,然后用小麥苗一樣纖細的手指著旁邊一個齊額劉海...
    Linda_4403閱讀 134評論 0 1