「Azure 串流分析」支援以 JavaScript 撰寫的使用者定義彙總 (UDA),可讓您實作複雜的具狀態商務邏輯。 在UDA中,您可以完全控制狀態數據結構、狀態累積、狀態解算和匯總結果計算。 本文介紹了兩種不同的 JavaScript UDA 介面、創建 UDA 的步驟,以及在流分析查詢中如何使用 UDA 進行基於視窗的操作。
JavaScript 使用者定義聚合
使用者定義的聚合是用在時間窗口規範之上,旨在對該窗口中的事件進行聚合,並產生單一的結果值。 目前「串流分析」支援兩種類型的 UDA 介面:AccumulateOnly 和 AccumulateDeaccumulate。 這兩種類型的 UDA 都可搭配 Tumbling、Hopping、Sliding 和 Session Window 使用。 AccumulateDeaccumulate UDA 與 Hopping、Sliding 和 Session Window 一起使用時,效能優於 AccumulateOnly UDA。 您需根據所使用的演算法來選擇這兩種類型其中之一。
AccumulateOnly 彙總
AccumulateOnly 集合只會將新事件累積至其狀態,此演算法不允許取消彙總。 無法實作將事件資訊從狀態值中解除累積時,請選擇此彙總類型。 以下是 AccumulateOnly 匯總的 JavaScript 範本:
// Sample UDA which state can only be accumulated.
function main() {
this.init = function () {
this.state = 0;
}
this.accumulate = function (value, timestamp) {
this.state += value;
}
this.computeResult = function () {
return this.state;
}
}
AccumulateDeaccumulate 彙總
AccumulateDeaccumulate 彙總功能允許從狀態中移除先前累積的值,例如,從事件值列表中移除鍵值組,或從總和彙總的狀態中扣除某個值。 以下是 AccumulateDeaccumulate 彙總的 JavaScript 範本:
// Sample UDA which state can be accumulated and deaccumulated.
function main() {
this.init = function () {
this.state = 0;
}
this.accumulate = function (value, timestamp) {
this.state += value;
}
this.deaccumulate = function (value, timestamp) {
this.state -= value;
}
this.deaccumulateState = function (otherState){
this.state -= otherState.state;
}
this.computeResult = function () {
return this.state;
}
}
UDA - JavaScript 函式宣告
每個 JavaScript UDA 都由一個「函式」物件宣告來定義。 以下是 UDA 定義中的主要元素。
函式別名
函式別名是 UDA 識別碼。 在「串流分析」查詢中呼叫時,請一律將 UDA 別名與 "uda" 前置詞搭配使用。
函式類型
就 UDA 而言,函式類型應該是 Javascript UDA。
輸出類型
這是 Stream Analytics 作業支援的特定類型;如果您想在查詢中處理該類型,則可使用「Any」。
函式名稱
此函式物件的名稱。 函式名稱應符合 UDA 別名。
方法 - init()
init() 方法會將彙總狀態初始化。 視窗開始時呼叫此方法。
方法 – accumulate()
accumulate() 方法會根據先前的狀態和目前的事件值來計算 UDA 狀態。 呼叫此方法的時機是在事件進入某個時間範圍 (TUMBLINGWINDOW、HOPPINGWINDOW、SLIDINGWINDOW 或 SESSIONWINDOW) 時。
方法 – deaccumulate()
deaccumulate() 方法會根據先前的狀態和目前的事件值來重新計算狀態。 呼叫此方法的時機是在事件離開 SLIDINGWINDOW 或 SESSIONWINDOW 時。
方法 – deaccumulateState()
deaccumulateState() 方法會根據先前的狀態,以及躍點的狀態重新計算狀態。 此方法會在一組事件離開 HOPPINGWINDOW 時被呼叫。
方法 – computeResult()
computeResult() 方法會根據目前的狀態傳回彙總結果。 此方法會在時間窗口 (例如 TUMBLINGWINDOW、HOPPINGWINDOW、SLIDINGWINDOW 或 SESSIONWINDOW) 結束時被呼叫。
JavaScript UDA 支援的輸入和輸出資料類型
針對 JavaScript UDA 資料類型,請參閱整合 JavaScript UDF 的串流分析與 JavaScript 類型轉換一節。
從 Azure 入口網站新增 JavaScript UDA
以下我們將會說明如何從「Portal」建立 UDA 的程序。 我們在這裡使用的範例是計算時間加權平均值。
現在,我們將依循步驟,在現有的 ASA 作業底下建立一個 JavaScript UDA。
登入 Azure 入口網站並找出您現有的「串流分析」作業。
然後選取 [作業拓撲] 中的函式連結。
選取 [新增] 以新增函式。
在 [新增函式] 檢視上,選取 [JavaScript UDA] 作為 [函式類型],然後您就會在編輯器中看見預設的 UDA 範本。
填入 "TWA" 作為 UDA 別名,然後依下列方式變更函式實作:
// Sample UDA which calculate Time-Weighted Average of incoming values. function main() { this.init = function () { this.totalValue = 0.0; this.totalWeight = 0.0; } this.accumulate = function (value, timestamp) { this.totalValue += value.level * value.weight; this.totalWeight += value.weight; } // Uncomment below for AccumulateDeaccumulate implementation /* this.deaccumulate = function (value, timestamp) { this.totalValue -= value.level * value.weight; this.totalWeight -= value.weight; } this.deaccumulateState = function (otherState){ this.state -= otherState.state; this.totalValue -= otherState.totalValue; this.totalWeight -= otherState.totalWeight; } */ this.computeResult = function () { if(this.totalValue == 0) { result = 0; } else { result = this.totalValue/this.totalWeight; } return result; } }選取 [儲存] 按鈕之後,您的 UDA 就會顯示在函式清單上。
選取新函式 "TWA" 即可查看函式定義。
在 ASA 查詢中呼叫 JavaScript UDA
在 Azure 入口網站中,開啟您的作業、編輯查詢,然後使用授權前置詞 "uda" 來呼叫 TWA() 函式。 例如:
WITH value AS
(
SELECT
NoiseLevelDB as level,
DurationSecond as weight
FROM
[YourInputAlias] TIMESTAMP BY EntryTime
)
SELECT
System.Timestamp as ts,
uda.TWA(value) as NoseDoseTWA
FROM value
GROUP BY TumblingWindow(minute, 5)
使用 UDA 來測試查詢
使用下列內容來建立一個本機 JSON 檔案,將此檔案上傳到「串流分析」作業中,然後測試上述查詢。
[
{"EntryTime": "2017-06-10T05:01:00-07:00", "NoiseLevelDB": 80, "DurationSecond": 22.0},
{"EntryTime": "2017-06-10T05:02:00-07:00", "NoiseLevelDB": 81, "DurationSecond": 37.8},
{"EntryTime": "2017-06-10T05:02:00-07:00", "NoiseLevelDB": 85, "DurationSecond": 26.3},
{"EntryTime": "2017-06-10T05:03:00-07:00", "NoiseLevelDB": 95, "DurationSecond": 13.7},
{"EntryTime": "2017-06-10T05:03:00-07:00", "NoiseLevelDB": 88, "DurationSecond": 10.3},
{"EntryTime": "2017-06-10T05:05:00-07:00", "NoiseLevelDB": 103, "DurationSecond": 5.5},
{"EntryTime": "2017-06-10T05:06:00-07:00", "NoiseLevelDB": 99, "DurationSecond": 23.0},
{"EntryTime": "2017-06-10T05:07:00-07:00", "NoiseLevelDB": 108, "DurationSecond": 1.76},
{"EntryTime": "2017-06-10T05:07:00-07:00", "NoiseLevelDB": 79, "DurationSecond": 17.9},
{"EntryTime": "2017-06-10T05:08:00-07:00", "NoiseLevelDB": 83, "DurationSecond": 27.1},
{"EntryTime": "2017-06-10T05:09:00-07:00", "NoiseLevelDB": 91, "DurationSecond": 17.1},
{"EntryTime": "2017-06-10T05:09:00-07:00", "NoiseLevelDB": 115, "DurationSecond": 7.9},
{"EntryTime": "2017-06-10T05:09:00-07:00", "NoiseLevelDB": 80, "DurationSecond": 28.3},
{"EntryTime": "2017-06-10T05:10:00-07:00", "NoiseLevelDB": 55, "DurationSecond": 18.2},
{"EntryTime": "2017-06-10T05:10:00-07:00", "NoiseLevelDB": 93, "DurationSecond": 25.8},
{"EntryTime": "2017-06-10T05:11:00-07:00", "NoiseLevelDB": 83, "DurationSecond": 11.4},
{"EntryTime": "2017-06-10T05:12:00-07:00", "NoiseLevelDB": 89, "DurationSecond": 7.9},
{"EntryTime": "2017-06-10T05:15:00-07:00", "NoiseLevelDB": 112, "DurationSecond": 3.7},
{"EntryTime": "2017-06-10T05:15:00-07:00", "NoiseLevelDB": 93, "DurationSecond": 9.7},
{"EntryTime": "2017-06-10T05:18:00-07:00", "NoiseLevelDB": 96, "DurationSecond": 3.7},
{"EntryTime": "2017-06-10T05:20:00-07:00", "NoiseLevelDB": 108, "DurationSecond": 0.99},
{"EntryTime": "2017-06-10T05:20:00-07:00", "NoiseLevelDB": 113, "DurationSecond": 25.1},
{"EntryTime": "2017-06-10T05:22:00-07:00", "NoiseLevelDB": 110, "DurationSecond": 5.3}
]
取得協助
如需其他協助,請嘗試 Azure 串流分析的 Microsoft 問與答頁面。