如何使用 kusto 查询合并两个字典

发布于 2025-01-09 18:09:43 字数 962 浏览 1 评论 0 原文

我希望我能够正确地表达这个问题,因为我对 kusto 很陌生。基本上我正在尝试编写一个 kusto 查询,该查询返回满足某些要求的字典。我有一个包含几列(名称、外部、父级)的表,目标是返回一个字典,其中键:名称,值:父级。然而,在某些情况下,“Parent”列为空,此时该值需要为:value:External。目前,我有一个查询在不同的行中返回两个字典,但我想知道是否可以将它们全部放在一个字典中。我希望我解释得很好,代码如下:

let Query1 = view () {
cluster('mycluster').database('my_database').Sizes
|distinct Name, Size, External
|where isempty( Size)| extend p = pack(Name, External) 
|summarize dict=make_bag(p)};
let Query2 = view (){
cluster('mycluster').database('my_database').Sizes
|distinct Name, Size, External
|where not(isempty( Size))| extend o = pack(Name, Parent) 
|summarize dict=make_bag(o)};
union withsource="TempTableName" Query1, Query2

代码返回:

TempTableName dict
Query1 {B0:Standard,B12:Standard1,B13:Standard5
Query2 {B1:Basic0,B5:Basic09,B19:Basic12}

I hope that I'm able to phrase this question properly as I am very new to kusto. Basically I am trying to write a kusto query that returns a dictionary where certain requirements are met. I have a table with several columns(Name, External, Parent) the goal is to return a dictioanary where key: Name , value: Parent. However there are some instances where the "Parent" column is blank, when this is true the value instead needs to be: value:External. Currently I have a query that returns two dictionaries in seperate rows, but I wanted to know if it is possible to have them all in one dictionary. I hope I explained this well, code is below:

let Query1 = view () {
cluster('mycluster').database('my_database').Sizes
|distinct Name, Size, External
|where isempty( Size)| extend p = pack(Name, External) 
|summarize dict=make_bag(p)};
let Query2 = view (){
cluster('mycluster').database('my_database').Sizes
|distinct Name, Size, External
|where not(isempty( Size))| extend o = pack(Name, Parent) 
|summarize dict=make_bag(o)};
union withsource="TempTableName" Query1, Query2

code returns:

TempTableName dict
Query1 {B0: Standard, B12: Standard1, B13: Standard5
Query2 {B1:Basic0, B5: Basic09, B19: Basic12}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅 2025-01-16 18:09:43

您可以使用 bag_merge() 函数:https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/bag-merge-function

print d1 = dynamic({"B0": "Standard", "B12": "Standard1", "B13": "Standard5"}),
      d2 = dynamic({"B1":"Basic0", "B5": "Basic09", "B19": "Basic12"})
| project d3 = bag_merge(d1, d2)
d3
{
"B0": "标准",
"B12": "标准1",
"B13": "标准5",
"B1": "Basic0",
"B5": "Basic09 ",
"B19": "Basic12"
}

或者,使用您的查询:

let d1 = toscalar(
    cluster('mycluster').database('my_database').Sizes
    | distinct Name, Size, External
    | where isempty(Size)
    | extend p = pack(Name, External) 
    | summarize dict=make_bag(p)
);
let d2 = toscalar(
    cluster('mycluster').database('my_database').Sizes
    | distinct Name, Size, External
    | where not(isempty(Size))
    | extend o = pack(Name, Parent) 
    | summarize dict=make_bag(o)
);
print d = bag_merge(d1, d2)

you can use the bag_merge() function: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/bag-merge-function

print d1 = dynamic({"B0": "Standard", "B12": "Standard1", "B13": "Standard5"}),
      d2 = dynamic({"B1":"Basic0", "B5": "Basic09", "B19": "Basic12"})
| project d3 = bag_merge(d1, d2)
d3
{
"B0": "Standard",
"B12": "Standard1",
"B13": "Standard5",
"B1": "Basic0",
"B5": "Basic09",
"B19": "Basic12"
}

or, with your query:

let d1 = toscalar(
    cluster('mycluster').database('my_database').Sizes
    | distinct Name, Size, External
    | where isempty(Size)
    | extend p = pack(Name, External) 
    | summarize dict=make_bag(p)
);
let d2 = toscalar(
    cluster('mycluster').database('my_database').Sizes
    | distinct Name, Size, External
    | where not(isempty(Size))
    | extend o = pack(Name, Parent) 
    | summarize dict=make_bag(o)
);
print d = bag_merge(d1, d2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文