Kusto无法将值投影到用户定义的函数中

时间:2019-04-16 07:56:15

标签: kusto kql

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

// 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))

我在这里想念的是什么,我希望'|项目”为每个结果使用功能(f)。

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

first query

second query

谢谢

2 个答案:

答案 0 :(得分:2)

有一种方法可以实现此目的(不进行连接):使用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)

答案 1 :(得分:0)

这是用户定义函数的限制,不能为每个行值调用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()

测试结果如下:

enter image description here