1

我有一些数据存储在我想对其执行聚合和分析的 customEvents 属性包中。我不熟悉可以使用 Azure 数据资源管理器查询语言对 AppInsights 运行的分析查询,但我遇到了问题。

我有一个将属性包的键值对之一的内容转换为数字数组的句柄(在下面的示例中,该输出由items.

let items = parse_json('{"operation_Id": "12345Z12", "days":[43, 21, 65]}');
print items.operation_Id, items.days;

但是,当我需要为每个 operation_Id 计算数组中项目的平均值时,我遇到了文档墙。我看过mvexpand, let(使用 lambda 表达式), datatable, 使用动态数据类型等。我遇到的阻塞问题mvexpand是我想将输出中的每一行与其操作相关联operation_Id,而且mvexpand似乎只是在第一行保持这种关系。使用 a datatable,该类型不支持管道输入。

我遇到的另一个常见错误(包括下面的代码示例是Operator Source Expression should be table or column)。

let items = parse_json('{"days":[43, 21, 65]}');

let arraySum = (T:(x: long))
{
   T 
   | summarize sum(x) 
};
items
| project days | invoke arraySum()

如有必要,我可以在 JavaScript 中执行聚合代码,只将计算出的平均值传递到属性包中,但丢弃原始数据值感觉很浪费。是否有一些明显的计算或聚合函数可以解决这个问题?

4

1 回答 1

2

以下两个选项都应该允许您计算您感兴趣的平均值:

警告:这是基于您展示的示例,可能会被“简化”以不反映您的现实生活场景,因此请澄清以防万一这没有帮助):

let items = dynamic({"operation_Id": "12345Z12", "days":[43, 21, 65]});
print operationId = tostring(items.operation_Id), days = items.days
| mvexpand days to typeof(int)
| summarize avg(days) by operationId

// or

let items = dynamic({"operation_Id": "12345Z12", "days":[43, 21, 65]});
print operationId = tostring(items.operation_Id), days = items.days
| project operationId, series_stats_dynamic(days)['avg']

您的第二个示例确实无效(标量和表格参数不相等),但可以重写如下:

与上述相同的警告

let items = dynamic({"days":[43, 21, 65]});
let arraySum = (T:(x: long))
{
   T 
   | summarize sum(x) 
};
print items 
| mvexpand x = items.days to typeof(long)
| invoke arraySum()

// or

let items = dynamic({"days":[43, 21, 65]});
print items
| project sum = series_stats_dynamic(items.days)["avg"] * array_length(items.days)

更新了后续提供的评论的示例:

datatable (Operation_id:string, customDimensions:dynamic) 
[ 
    "MTFfq", dynamic({"siteId": "1", "fileCount": "3", "pendingDays":[15,10,11]}), 
    "LXVjk", dynamic({"siteId": "2", "fileCount": "1", "pendingDays":[3]}),
    "jnySt", dynamic({"siteId": "3", "fileCount": "2", "pendingDays":[7,11]}), 
    "NoxoX", dynamic({"siteId": "4", "fileCount": "4", "pendingDays":[1,4,3,11]})
]
| mvexpand days = customDimensions.pendingDays to typeof(int)
| summarize avg(days) by Operation_id

// or

datatable (Operation_id:string, customDimensions:dynamic) 
[ 
    "MTFfq", dynamic({"siteId": "1", "fileCount": "3", "pendingDays":[15,10,11]}), 
    "LXVjk", dynamic({"siteId": "2", "fileCount": "1", "pendingDays":[3]}),
    "jnySt", dynamic({"siteId": "3", "fileCount": "2", "pendingDays":[7,11]}), 
    "NoxoX", dynamic({"siteId": "4", "fileCount": "4", "pendingDays":[1,4,3,11]})
]
| project Operation_id, series_stats_dynamic(customDimensions.pendingDays)['avg']

和:

let arraySum = (T:(x: long))
{
   T 
   | summarize sum(x) 
};
datatable (Operation_id:string, customDimensions:dynamic) 
[ 
    "MTFfq", dynamic({"siteId": "1", "fileCount": "3", "pendingDays":[15,10,11]}), 
    "LXVjk", dynamic({"siteId": "2", "fileCount": "1", "pendingDays":[3]}),
    "jnySt", dynamic({"siteId": "3", "fileCount": "2", "pendingDays":[7,11]}), 
    "NoxoX", dynamic({"siteId": "4", "fileCount": "4", "pendingDays":[1,4,3,11]})
]
| mvexpand x = customDimensions.pendingDays to typeof(long)
| invoke arraySum()

// or

datatable (Operation_id:string, customDimensions:dynamic) 
[ 
    "MTFfq", dynamic({"siteId": "1", "fileCount": "3", "pendingDays":[15,10,11]}), 
    "LXVjk", dynamic({"siteId": "2", "fileCount": "1", "pendingDays":[3]}),
    "jnySt", dynamic({"siteId": "3", "fileCount": "2", "pendingDays":[7,11]}), 
    "NoxoX", dynamic({"siteId": "4", "fileCount": "4", "pendingDays":[1,4,3,11]})
]
| project Operation_id, sum = series_stats_dynamic(customDimensions.pendingDays)["avg"] * array_length(customDimensions.pendingDays)

上面使用的运算符/函数的一些参考:

于 2018-11-30T05:47:17.070 回答