1

我有 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

4

1 回答 1

2

流式查询(或者换句话说,在动态表上执行的查询)会出现这样的情况。与传统数据库不同,查询的输入关系在查询执行期间保持静态,流式查询的输入不断更新——因此结果也必须不断更新。

如果我了解此处的设置,则在处理相关行之前,第 1 行和第 3 行的“不正确”结果是正确的orderRes2。如果这些行从未到达,则第 1 行和第 3 行将保持正确。

您应该期望的是最终正确的结果,包括必要的撤回。您可以通过打开小批量聚合来减少中间结果的数量。

这个邮件列表线程提供了更多的洞察力。如果我误解了您的情况,请提供一个可重现的示例来说明问题。

于 2021-05-10T08:43:02.857 回答