前言
使用聚合查询中间件,多个http、t3、fdw服务的结果聚合,同时利用SQL引擎的能力对结果进行连接、分组、排序、聚合
安装聚合查询
试用版本安装见 lightdb周边-聚合查询试用包指引_星辰bitone的博客-CSDN博客
场景
假设业务应用分为用户域微服务、订单域微服务、产品域微服务,每个微服务拥有单独的数据库。 如下图所示:
在场景用户认购理财产品后,会在订单域里面产生下单记录,产品域中会有用户购买理财产品中的每日收益情况。
用户库
产品库:
订单库
聚合查询开发配置
服务配置
聚合查询配置fdw,规则见:lightdb周边-聚合查询参考手册_星辰bitone的博客-CSDN博客 中外部表聚合章节
用户外部表
lightdb@postgres=# CREATE foreign table f_users (
lightdb@postgres(# user_id VARCHAR(100),
lightdb@postgres(# user_name VARCHAR(100)
lightdb@postgres(# ) server oradb OPTIONS (schema 'ZHANGLIANG', table 'USERS' );
订单外部表
lightdb@postgres=# create foreign table f_order (
lightdb@postgres(# order_id VARCHAR(100),
lightdb@postgres(# user_id VARCHAR(100),
lightdb@postgres(# product_id VARCHAR(100),
lightdb@postgres(# status VARCHAR(1)
lightdb@postgres(# ) server oradb OPTIONS (schema 'ZHANGLIANG', table 'USER_ORDERS' );
产品外部表
lightdb@postgres=# create foreign table f_products (
lightdb@postgres(# user_id VARCHAR(100),
lightdb@postgres(# product_id VARCHAR(100),
lightdb@postgres(# product_name VARCHAR(100),
lightdb@postgres(# amount_date VARCHAR(100),
lightdb@postgres(# amount numeric(16,2)
lightdb@postgres(# ) server oradb OPTIONS (schema 'ZHANGLIANG', table 'USER_PRODUCTS' );
mybatiis xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hundsun.easysearch.TestMapper">
<select id="getUserTradeTrendFDW" resultType="map">
SELECT * FROM
(
SELECT u.user_id, u.user_name, o.product_id, p.product_name, p.amount_date, sum(p.amount)
FROM
f_products p ,
f_order o ,
f_users u
WHERE
u.user_id = o.user_id and o.status = 'S' and o.product_id = p.product_id
GROUP BY u.user_id, u.user_name, o.product_id, p.product_name, p.amount_date
) t
ORDER BY t.amount_date DESC, t.product_name DESC limit #{pageSize} offset #{pagePos};
</select>
</mapper>
上述mybatis xml配置内容放到TestMaper.xml中,并放到聚合查询接入层easysearch.mapper-locations指定的目录下。
[fcperf@hs-10-20-30-193 easysearch]$ cat config/application.properties | grep locations easysearch.mapper-locations=./mapper
[fcperf@hs-10-20-30-193 easysearch]$ ls
mapper/TestMapper.xml
启动聚合查询接入层
[fcperf@hs-10-20-30-193 easysearch]$ ls arthas-output config easy-search.jar logs mapper start.sh stdout.log stop.sh
[fcperf@hs-10-20-30-193 easysearch]$ ./start.sh
[fcperf@hs-10-20-30-193 easysearch]$ tail -f logs/easy-search-app.log
可以直接请求接口getUserTradeTrendFDW