如何简单计算来自 Restful 的多层 json 数据
接收 Restful 的 json 数据后经常还要计算,用 Java 处理会比较麻烦。用 esProc 会简单很多,不仅能对接 Restful 接口,擅长处理多层数据,还能嵌入 Java 应用使用,可以作为应用内计算引擎使用。
先到乾学院下载免费的 esProc 标准版。
安装完成后,先尝试一下访问 Restful 数据。
Restful 访问
直接访问
Rest 服务提供各种数据访问接口,比如访问:http://192.168.2.52:8503/orders可以获得订单数据:
Orders 采用多层 json 存储了订单产品等相关信息,结构如下:
代码语言:txt复制[
{
"order_id": "ORD001",
"order_date": "2025-03-01",
"customer": "Alice Johnson",
"order_details": [
{
"detail_id": "D001",
"quantity": 2,
"price": 50.0,
"product": {
"product_id": "P001",
"name": "Wireless Mouse",
"category": "Electronics"
}
},
{
"detail_id": "D002",
"quantity": 1,
"price": 120.0,
"product": {
"product_id": "P002",
"name": "Mechanical Keyboard",
"category": "Electronics"
}
}
]
},
{
"order_id": "ORD002",
"order_date": "2025-03-02",
"customer": "Bob Smith",
"order_details": [
{
"detail_id": "D003",
"quantity": 3,
"price": 25.0,
"product": {
"product_id": "P003",
"name": "Bluetooth Headphones",
"category": "Electronics"
}
},
{
"detail_id": "D004",
"quantity": 1,
"price": 200.0,
"product": {
"product_id": "P004",
"name": "Smart Watch",
"category": "Wearables"
}
}
]
},
…
]
编写 esProc 脚本访问 orders 数据:
这里使用 httpfile 函数访问 Restful 服务读取 order 数据,A2 使用 json 函数将 json 串转成二维序表。
按 ctrl+F9 运行脚本:
可以正常访问。这里使用 httpfile 函数访问 Restful 服务读取 order 数据,然后 A2 使用 json 函数将 json 串转成二维序表。
下面做一个计算:筛选包含某个分类(如 Electronics),且订单金额不低于 200 的订单。
设置脚本参数:
编写脚本:
A3 进行条件过滤,这里直接用点(.)操作符引用下一层级的数据,多层就直接点下去就可以,表达很清晰。
执行可以看到各步骤运行结果:
还可以写成一句:
代码语言:txt复制=json(httpfile("http://172.20.10.7:8503/orders").read()).select(order_details.select@1(product.category==categ) && order_details.sum(price*quantity)>200). new(order_id,order_date,customer,order_details.sum(price*quantity):amount)
值得一提的是,这种任务即使用支持 json 较好的 DuckDB SQL 也不好算(先不考虑访问 Restful 服务的复杂度):
代码语言:txt复制SELECT
o.order_id,
LIST_FILTER(o.order_details, x -> x.product.category = 'Electronics') AS order_details
FROM read_json_auto(orders.json') AS o
WHERE
ARRAY_LENGTH(LIST_FILTER(o.order_details, x -> x.product.category = 'Electronics')) > 0
AND SUM(
LIST_FILTER(o.order_details, x -> x.product.category = 'Electronics') ->
(x -> x.price * x.quantity)
) > 200;
相比之下 esProc 就简单多了,而且还能非常方便地访问 Restful 服务。
安全控制
为了数据的安全性,有些 REST 服务器会对访问数据者的身份进行认证,只有通过认证的访问才能读取到数据。常用的身份认证分为两大类,一类是用户访问认证页面后,服务器将认证后的信息记录在 Session 并将 Session 号发回客户端的 Cookie 中,或者将认证信息也发回客户端的 Cookie 中。当要访问有权限控制的页面数据时,需要将 Cookie 中保存的内容放在申请头中,服务器就能判断出访问者的身份,从而决定是否允许访问此页数据。另一类是用户访问认证页面后,服务器返回一个访问令牌 Token,在令牌有效期内,访问有权限控制页面数据时将 Token 作为参数传回就可以了。
这里给出两个示例。
服务器以 Session 或 Cookie 保存身份认证
服务器返回 Token
应用集成
下面把 esProc 集成到 Java 应用中。
从 [esProc 安装目录]\esProc\lib 目录下找到 esProc JDBC 相关的 jar 包:esproc-bin-xxxx.jar、icu4j_60.3.jar。
将这两个 jar 包部署到 Java 开发环境的类路径下,同时将 MySQL 的驱动包也放到应用中。
再从目录 [esProc 安装目录]\esProc\config 下找到 esProc 配置文件 raqsoftConfig.xml,同样部署到 Java 开发环境的类路径下。
编写 Java 代码调用 SPL 脚本 getRestData.splx:
代码语言:txt复制public class RestDataQuery {
public static void main(String[] args) {
String driver = "com.esproc.jdbc.InternalDriver";
String url = "jdbc:esproc:local://";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url);
PreparedStatement st =conn.prepareCall("call getRestData(?)");
st.setObject(1, "Electronics");
st.execute();
ResultSet rs = st.getResultSet();
System.out.println("order_id\torder_date\t\tcustomer\t\tamount");
System.out.println("----------------------------------------------");
while(rs.next()) {
String orderId = rs.getString("order_id");
String orderDate = rs.getString("order_date");
String customer = rs.getString("customer");
double amount = rs.getDouble("amount");
System.out.printf("%-15s%-15s%-15s%.2f%n",orderId, orderDate, customer, amount);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
JDBC 的使用跟数据库完全一致,调用 SPL 脚本也与访问存储过程类似,这里是 call getRestData (?) 并传参。
下面是运行结果:
总结一下,用 esProc 处理 Restful 接口的 json 数据,既能简化多层结构的解析和计算,又能轻松嵌入 Java 程序,可作为应用内计算引擎使用.
发布者:admin,转转请注明出处:http://www.yc00.com/web/1747533361a4648619.html
评论列表(0条)