簡體   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