Java 结构化数据处理开源库 SPL
程序员书单
共 4417字,需浏览 9分钟
·
2022-01-19 15:49
Orders=T("d:/Orders.csv")
Orders.new(OrderID, Amount, OrderDate)
Orders.new(OrderID, Amount, year(OrderDate))
Orders.new(OrderID:ID, SellerId, year(OrderDate):y)
Orders.groups(year(_5),_2; sum(_4))
join@1(Orders:o,SellerId ; Employees:e,EId).groups(e.Dept; sum(o.Amount))
Orders.select(Amount>1000 && Amount<=3000 && like(Client,"*bro*"))
Orders.sort(-Client,Amount)
Orders.groups(year(OrderDate),Client; sum(Amount))
join(Orders:o,SellerId ; Employees:e,EId).groups(e.Dept; sum(o.Amount))
Orders.select(Amount>1000)
Orders.select(Amount>1000 && Amount<2000)
Orders.sum(Amount*Amount)
Orders.sort(-Client, Amount)
Orders.groups(Client;sum(Amount):amt).select(amt>1000 && like(Client,"*S*"))
join(Orders:o,SellerId ; Employees:e,Eid).groups(e.Dept; sum(o.Amount))
Sales2021.group(month(sellDate)).(~.groups(Client;sum(Amount):sumValue)).(~.sort(-sumValue)) .(~.select(#<=10)).(~.(Client)).isect()
$select * from d:/Orders.csv where (OrderDate<date('2020-01-01') and Amount<=100)or (OrderDate>=date('2020-12-31') and Amount>100)
$select year(OrderDate),Client ,sum(Amount),count(1) from d:/Orders.csv
group by year(OrderDate),Client
having sum(Amount)<=100
$select o.OrderId,o.Client,e.Name e.Dept from d:/Orders.csv o
join d:/Employees.csv e on o.SellerId=e.Eid
$with t as (select Client ,sum(amount) s from d:/Orders.csv group by Client)
select t.Client, t.s, ct.Name, ct.address from t
left join ClientTable ct on t.Client=ct.Client
A | |
1 | =T("score.csv").group(subject) |
2 | =A2.(~.rank(score).pselect@a(~<=10)) |
3 | =A1.(~(A3(#)).(name)).isect() |
SPL序表的字段可以存储记录或记录集合,这样可以用对象引用的方式,直观地表达关联关系,即使关系再多,也能直观地表达。比如,根据员工表找到女经理下属的男员工:
Orders.select@1(Amount>1000)
Orders.select@m(Amount>1000)
Orders.select@b(Amount>1000)
Orders.select@1b(Amount>1000)
join(Orders:o,SellerId ; Employees:e,EId)
A.select(f(x):{x%2==0} )
A | B | |
1 | =T("Orders.txt") | /订单序表 |
2 | =A1.groups(year(Date):y,month(Date):m; sum(Amount):amt) | /按年月分组汇总 |
3 | =A2.derive(amt/amt[-1]:lrr, amt[-1:1].avg():ma) | /计算比上期和移动平均 |
…
Class.forName("com.esproc.jdbc.InternalDriver");
Connection conn =DriverManager.getConnection("jdbc:esproc:local://");
PrepareStatement st = conn.prepareStatement("=T(\"D:/Orders.txt\").select(Amount>1000 && Amount<=3000 && like(Client,\"*S*\"))");
ResultSet result=st.execute();
...
复杂计算可以存成脚本文件,以存储过程方式调用
…
Class.forName("com.esproc.jdbc.InternalDriver");
Connection conn =DriverManager.getConnection("jdbc:esproc:local://");
Statement st = connection.();
CallableStatement st = conn.prepareCall("{call splscript1(?, ?)}");
3000);
5000);
ResultSet result=st.execute();
...
将脚本外置于Java程序,一方面可以降低代码耦合性,另一方面利用解释执行的特点还可以支持热切换,业务逻辑变动时只要修改脚本即可立即生效,不像使用Java时常常要重启整个应用。这种机制特别适合编写微服务架构中的业务处理逻辑。
评论