2017-09-18 15 views
0

ストリームアナリティクスでarray_aggまたはstring_aggに相当するpostgresを実行する方法はありますか?私は数秒ごとに来るデータを持っており、時間枠内で値のカウントを取得したいと考えています。 2秒のスライディングウィンドウでAzureストリーム分析array_agg相当?

{time:12:01:01,name:A,location:X,value:10} 
{time:12:01:01,name:B,location:X,value:9} 
{time:12:01:02,name:C,location:Y,value:5} 
{time:12:01:02,name:B,location:Y,value:4} 
{time:12:01:03,name:B,location:Z,value:2} 
{time:12:01:03,name:A,location:Z,value:3} 
{time:12:01:06,name:B,location:Z,value:4} 
{time:12:01:06,name:C,location:Z,value:7} 
{time:12:01:08,name:B,location:Y,value:1} 
{time:12:01:13,name:B,location:X,value:8} 

、私はデータは、以下を参照してくださいグループにしたい:

データ

12:01:01, 2 events, 9.5 avg, 2 distinct names, 1 distinct location, nameA:1, nameB:1, locationX:1 
12:01:02, 4 events, 7 avg, 3 distinct names, 2 distinct location, nameA:1, nameB:2,nameC:1,locationX:1,locationY:1 
12:01:03... 
12:01:06... 
... 

私はイベントの数、平均値を得ることができ、かつ問題のない別個のカウント。私はウインドウとwithステートメントを使ってタイムスタンプに参加し、そのタイムスタンプの集計カウントを取得します。 Azureで文字列を集計する方法がわからないため、名前と場所で合計数を取得する方法を調べるのに問題があります。

with agg1 as (
select system.timestamp as start, 
avg(value) as avg, 
count(1) as events, 
count(distinct name) as distinct names, 
count(distinct location) as distinct location 
from input timestamp by created 
group by slidingwindow(second,2) 
), 
agg2 as (
select agg2_inner.start, 
array_agg(name,'|',ct_name) as countbyname (????) 
from (
    select system.timestamp as start, 
    name, count(1) as ct_name 
    from input timestamp by created 
    group by slidingwindow(second,2), name 
) as agg2_inner 
group by agg2_inner.start, slidingwindow(seconds,2) 
) 

select * from agg1 join agg2 on (datediff(second,agg1,agg2) between 0 and 2 
and agg1.start = agg2.start) 

クエリがビット動的である必要があるように、名前、場所のリストが設定されていません。カウントが単一のクエリ内のオブジェクト内にある場合、後で個々のカウントを取得するために解析することができます。

答えて

1

私が知る限り、紺碧のストリーム分析はarray_aggメソッドを提供しません。しかし、ウィンドウからすべてのレコード値を返すことができるCollectメソッドを提供します。

私はCollectメソッドを使用することをお勧めします最初に時間とウィンドウでグループ化された配列を返します。

次に、Azure Stream Analytics JavaScript user-defined functionsを使用して、配列を結果に変換する独自のロジックを書き込むことができます。

詳細は、サンプル下記を参照できます。

このようなクエリ:

SELECT 
    time, udf.yourunfname(COLLECT()) as Result 
INTO 
    [YourOutputAlias] 
FROM 
    [YourInputAlias] 
Group by time, TumblingWindow(minute, 10) 

UDFは、このようなものです:

私はちょうど平均とイベントの長さを返します。

function main(InputJSON) { 
     var sum = 0; 
     for (i = 0; i < InputJSON.length; i++) { 
      sum += InputJSON[i].value; 

     } 
    var result = {events:InputJSON.length,avg:sum/InputJSON.length }; 

    return result; 
} 

データ:

{"name": "A", "time":"12:01:01","value":10} 

{"name": "B", "time":"12:01:01","value":9} 

{"name": "C", "time":"12:01:02","value":10} 

結果:

enter image description here

関連する問題