【问题标题】:Kusto cannot project a value into a user defined functionKusto 无法将值投影到用户定义的函数中
【发布时间】:2019-04-16 07:56:15
【问题描述】:

我在我们的域中有一个查询,我无法让它工作。我使用数据表来模仿我的问题。 我正在尝试在用户定义的函数中使用投影值。

// this works
let f = (a:int) {
    datatable (b:string, d:int) ["2015-12-31", 1, "2016-12-30", 2, "2014-01-05", 3]
    | as dataset
    | where d == a
    | project b;
};
datatable (d:int) [1, 2, 3]
| as dataset
| project toscalar(f(2))

// this doesnt work, why is the 'd' not used (projected) in function q. 
// if I add toscalar to the project it also doesnt work
let f = (a:int) {
    datatable (b:string, d:int) ["2015-12-31", 1, "2016-12-30", 2, "2014-01-05", 3]
    | as dataset
    | where d == a
    | project b;
};
datatable (d:int) [1, 2, 3]
| as dataset
| project toscalar(f(d))

我在这里缺少什么,我期待'| project' 对每个结果使用函数 (f)。

这里有 2 个要修改的查询。

first query

second query

谢谢

【问题讨论】:

  • 这是自定义函数的限制,该列不能作为自定义函数的参数。你可以看看限制here
  • 评论不正确。列值可以发送到用户定义函数。该示例不起作用的原因是使用带有行上下文的“toscalar()”(这意味着不能为每个行值调用 toscalar())。
  • @AlexanderSloutsky,你是对的。我只是想把它解释得更简单一点,但给出了一个错误的解释,我的错。

标签: azure azure-data-explorer kql


【解决方案1】:

有一种方法可以实现这一点(无需连接)是使用 toscalar() 创建一个动态映射(属性包),然后将其用作查找字典。

let f = (a:int) {
    let _lookup = toscalar 
    (
        datatable (b:string, d:int) ["2015-12-31", 1, "2016-12-30", 2, "2014-01-05", 3]
        | extend p = pack(tostring(d), b)
        | summarize make_bag(p)
     );
    _lookup[tostring(a)]
};
datatable (d:int) [1, 2, 3]
| project result=f(d)

【讨论】:

  • 聪明的方法 :) 与JOIN相比,它在大型数据集上的表现如何?
  • 当您拥有包含数百个项目的静态数据集时,此方法效果很好。如果数据集增长超过该值并且仍低于 1M 值 - (性能方面)最好使用带有“广播”提示或“查找”运算符的连接。如果数据集超出此范围 - 洗牌连接方法来拯救。 (谷歌:“kusto 查找”、“kusto 广播加入”、“kusto shuffle 加入” - 了解更多详情)
【解决方案2】:

这是用户定义函数的限制,不能为每个行值调用 toscalar()。你可以看看限制here

这里有一个变通方法,可以达到你的目的(你也可以用这个query link直接运行):

let f = (T:(d:int)) {
    let table1 = datatable (b:string, d:int) ["2015-12-31", 1, "2016-12-30", 2, "2014-01-05", 3]
    | as dataset;
    T
    | join (
       table1 
    ) on d
    | project b  
};

datatable (d:int) [1, 2, 3]
| as dataset
| invoke f()

测试结果如下:

【讨论】:

  • 您好,如果答案有效,请帮忙标记为答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 2023-01-20
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 2014-11-27
相关资源
最近更新 更多