Azure流分析:多个Windows + JOINS

时间:2018-05-22 22:11:46

标签: azure azure-eventhub azure-stream-analytics complex-event-processing

我的架构:

  • 1个带有8个分区的EventHub& 2 TPU
  • 1流式分析作业
  • 6基于相同输入的Windows(从1mn到6mn)

示例数据:

{side: 'BUY', ticker: 'MSFT', qty: 1, price: 123, tradeTimestamp: 10000000000}
{side: 'SELL', ticker: 'MSFT', qty: 1, price: 124, tradeTimestamp:1000000000}

EventHub PartitionKeyticker

我想每秒发出一次以下数据:

(Total quantity bought / Total quantity sold) in the last minute, last 2mn, last 3mn and more

我尝试了什么:

WITH TradesWindow AS (
    SELECT
        windowEnd = System.Timestamp,
        ticker,
        side,
        totalQty = SUM(qty)
    FROM [Trades-Stream] TIMESTAMP BY tradeTimestamp PARTITION BY PartitionId
    GROUP BY ticker, side, PartitionId, HoppingWindow(second, 60, 1)
),
TradesRatio1MN AS (
    SELECT 
        ticker = b.ticker,
        buySellRatio = b.totalQty / s.totalQty
    FROM TradesWindow b /* SHOULD I PARTITION HERE TOO ? */
    JOIN TradesWindow s /* SHOULD I PARTITION HERE TOO ? */
    ON s.ticker = b.ticker AND s.side = 'SELL'
    AND DATEDIFF(second, b, s) BETWEEN 0 AND 1
    WHERE b.side = 'BUY'
)

 /* .... More windows.... */

/* FINAL OUTPUT: Joining all the windows */
SELECT
   buySellRatio1MN = bs1.buySellRatio,
   buySellRatio2MN = bs2.buySellRatio
   /* more windows */
INTO [output]
FROM buySellRatio1MN bs1 /* SHOULD I PARTITION HERE TOO ? */
JOIN buySellRatio2MN bs2 /* SHOULD I PARTITION HERE TOO ? */
ON bs2.ticker = bs1.ticker
AND DATEDIFF(second, bs1, bs2) BETWEEN 0 AND 1

问题:

  • 这需要6个EventHub Consumer组(每个只能有5个读者),为什么?我输入上没有5x6 SELECT语句,为什么呢?
  • 输出似乎不一致(我不知道我的JOIN是否正确)。
  • 有时候作业根本没有输出(可能是一些分区问题?请参阅代码中有关分区的注释)

简而言之,有没有更好的方法来实现这一目标?我在文档中没有找到任何关于有多个窗口并加入它们的例子,然后只从1个输入加入以前连接的结果。

1 个答案:

答案 0 :(得分:0)

对于第一个问题,这取决于横向扩展逻辑的内部实现。详情请见here

对于连接的输出,我没有看到整个查询,但是如果你加入一个带有1分钟窗口的查询和一个带有1s时间“缓冲区”的2分钟窗口的查询,你将只输出每一个2分钟。 UNION运营商将会更好。

从您的示例和目标来看,我认为使用UDA(用户定义聚合)编写此查询有一种更简单的方法。

为此,我将首先定义一个名为“ratio”的UDA函数:

function main() {
this.init = function () {
    this.sumSell = 0.0;
    this.sumBuy  = 0.0;
}

this.accumulate = function (value, timestamp) {
    if (value.side=="BUY") {this.sumBuy+=value.qty};
    if (value.side=="SELL") {this.sumSell+=value.qty};
   }

this.computeResult = function () {
    if(this.sumSell== 0) {
        result = 0;
    }
    else {
        result =  this.sumBuy/this.sumSell;
    }
    return result;

}
}

然后我可以简单地使用这个SQL查询60秒窗口:

SELECT
  windowEnd = System.Timestamp,
  ticker,
  uda.ratio(iothub) as ratio
FROM iothub PARTITION BY PartitionId
GROUP BY ticker, PartitionId, SlidingWindow(second, 60)