繁体   English   中英

使用 Kusto 查询返回的字典

[英]Use Dictionary returned by Kusto Query

kusto 相当新,所以我在浏览它时遇到了很多困难。 我决定使用 kusto 将 map 的某些值传递给其他人,下面是查询:

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)

查询返回一个包含我需要的映射的字典。 我遇到问题的地方实际上是在我的 c# 代码中使用这本字典。 目前我可以使用以下代码调用查询:

 string GetFabricToNameMappingCRPQuery = "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, Size) " +
                                                        "| summarize dict=make_bag(o));print d = bag_merge(d1, d2)";

            using (var queryProvider = KustoClientFactory.CreateCslQueryProvider(builder))
            {
                using (var reader = queryProvider.ExecuteQuery(query: GetFabricToNameMappingCRPQuery))
                    while (reader.Read())
                    {
                          return (reader.GetString(0));
                    }
            }

我以为稍后可以在代码中将其反序列化为字典,但出现错误:无法转换类型为 object;Newtonsoft.Json.Linq.JObject; 键入;System.String;。 在 System.Data.DataTableReader2.GetString(Int32 ordinal)这是有道理的,但我不确定如何解决它。 是否可以在我的代码中使用包含我的映射的字典?

谢谢@Yoni L。您的命令帮助修复了SQL Reader JSON Response的反序列化。

请找到以下解决方法。

  • reader.GetString(0)替换为reader.GetValue(0).ToString()
  • 读取所有查询结果并将其保存在StringBuilder中。
jsonResultSB.Append(reader.GetValue(0).ToString());
  • 在一步反序列化所有查询结果之后。
JsonResultLists = JsonConvert.DeserializeObject<List<JsonResultList>>(jsonResultSB.ToString());

使用以下代码解决您的问题。

# using String Builder and List to get the all values while processing the query result.
List<JsonResultList> JsonResultLists=  new  List<JsonResultList>();
StringBuilder jsonResultSB =  new  StringBuilder();
using (var queryProvider = KustoClientFactory.CreateCslQueryProvider(builder))
            {
                using (var reader = queryProvider.ExecuteQuery(query: GetFabricToNameMappingCRPQuery))
                    while (reader.Read())
                    {
                         #Replaceing below line and added the Deserialization 
                         // return (reader.GetString(0));
                         
                        #Adding the reader result line by line in String Builder
                        jsonResultSB.Append(reader.GetValue(0).ToString());
                         
                    }
                    #Deserializing the Result of Query
                    JsonResultLists = JsonConvert.DeserializeObject<List<JsonResultList>>(jsonResultSB.ToString());
            }
     return JsonResultLists;

有关阅读 SQL JSON 结果为 .NET的更多信息,请参阅此处

暂无
暂无

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

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