我有 2 个数据流,它们是从 2 个表创建的,例如:
Table orderRes1 = ste.sqlQuery(
"SELECT orderId, userId, SUM(bidPrice) as q FROM " + tble +
" Group by orderId, userId");
Table orderRes2 = ste.sqlQuery(
"SELECT orderId, userId, SUM(askPrice) as q FROM " + tble +
" Group by orderId, userId");
DataStream<Tuple2<Boolean, Row>> ds1 = ste.toRetractStream(orderRes1 , Row.class).
filter(order-> order.f0);
DataStream<Tuple2<Boolean, Row>> ds2 = ste.toRetractStream(orderRes2 , Row.class).
filter(order-> order.f0);
我想对这两个流执行完全外连接,我使用了这两个流orderRes1.fullOuterJoin(orderRes2 ,$(exp))
和一个包含完全外连接的 sql 查询,如下所示:
Table bidOrdr = ste.fromDataStream(bidTuple, $("orderId"),
$("userId"), $("price"));
Table askOrdr = ste.fromDataStream(askTuple, $("orderId"),
$("userId"), $("price"));
Table result = ste.sqlQuery(
"SELECT COALESCE(bidTbl.orderId,askTbl.orderId) , " +
" COALESCE(bidTbl.userId,askTbl.orderId)," +
" COALESCE(bidTbl.bidTotalPrice,0) as bidTotalPrice, " +
" COALESCE(askTbl.askTotalPrice,0) as askTotalPrice, " +
" FROM " +
" (SELECT orderId, userId," +
" SUM(price) AS bidTotalPrice " +
" FROM " + bidOrdr +
" Group by orderId, userId) bidTbl full outer JOIN " +
" (SELECT orderId, userId," +
" SUM(price) AS askTotalPrice" +
" FROM " + askOrdr +
" Group by orderId, userId) askTbl " +
" ON (bidTbl.orderId = askTbl.orderId" +
" AND bidTbl.userId= askTbl.userId) ") ;
DataStream<Tuple2<Boolean, Row>> = ste.toRetractStream(result, Row.class).filter(order -> order.f0);
但是,在某些情况下,结果并不正确:假设用户 A 向 B 卖出 3 次价格,然后用户 B 向 A 卖出 2 次,第二次结果是:
7> (true,123,a,300.0,0.0)
7> (true,123,a,300.0,200.0)
10> (true,123,b,0.0,300.0)
10> (true,123,b,200.0,300.0)
第二行和第四行是流的预期结果,但它也会生成第一行和第三行。值得一提的是 coGroup 是另一种解决方案,但我不想在这种情况下使用窗口化,并且非窗口化解决方案只能在有界流(DataSet)中访问。
提示:orderId 和 userId 将在两个流中重复,我想在每个操作中生成 2 行,包含:orderId、userId1、bidTotalPrice、askTotalPrice 和 orderId、userId2、bidTotalPrice、askTotalPrice