简体   繁体   English

如何在 Azure 流分析中聚合多个数组

[英]How to aggregate multiple arrays in Azure Stream Analytics

I have a table that looks like this:我有一张看起来像这样的表:

"A" : [1,2,3],
"A" : [4,5,6],
"A" : [7,8,9],
"B" : [10,11,12]
"B" : [13,14,15]
"B" : [16,17,18]

and i need it to look likes this:我需要它看起来像这样:

"A" : [1,2,3,4,5,6,7,8,9],
"B" : [10,11,12,13,14,15,16,17,18]

How can i merge the arrays based on their letter in stream analytics?如何根据流分析中的字母合并数组? All the aggregate functions i've tried do not help.我尝试过的所有聚合函数都无济于事。

Thanks -谢谢 -

Something like this I think fits像这样的东西我认为合适

drop table if exists dbo.test_table;
go
create table dbo.test_table(
  letter        char(3) not null,
  string        varchar(20) not null);

insert dbo.test_table(letter, string) values
('"A"', '[1,2,3]'),
('"A"', '[4,5,6]'),
('"A"', '[7,8,9]'),
('"B"', '[10,11,12]'),
('"B"', '[12,14,15]'),
('"B"', '[16,17,18]');

select letter, concat('[', string_agg(cast(intg.value as varchar(9)),',') within group (order by intg.value), ']') colName
from dbo.test_table t
     cross apply
     string_split(substring(string, 2, len(string)-2),',') intg
group by letter;

Results结果

letter  colName
"A"     [1,2,3,4,5,6,7,8,9]
"B"     [10,11,12,12,14,15,16,17,18]

The best approach would be to GROUP BY <column with the "A", "B"> and use COLLECT() as an aggregate.最好的方法是 GROUP BY <column with the "A", "B"> 并使用 COLLECT() 作为聚合。 This will give you {name : "A", val : [ [1,2,3], [4,5,6], [7,8,9] ] }, ...这会给你 {name : "A", val : [ [1,2,3], [4,5,6], [7,8,9] ] }, ...

And then use a JavaScript UDF to flatten the array of arrays.然后使用 JavaScript UDF 来展平数组数组。

Here are some links to documentation:以下是一些文档链接:

https://docs.microsoft.com/en-us/stream-analytics-query/collect-azure-stream-analytics https://docs.microsoft.com/en-us/stream-analytics-query/collect-azure-stream-analytics

https://docs.microsoft.com/en-us/azure/stream-analytics/stream-analytics-javascript-user-defined-functions https://docs.microsoft.com/en-us/azure/stream-analytics/stream-analytics-javascript-user-defined-functions

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM