简体   繁体   中英

Use Dictionary returned by Kusto Query

fairly new to kusto so I'm having a lot of difficulty navigating it. I decided to use kusto to map certain values to others, here is the query below:

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)

the query returns a dictionary that contains the mapping I need. Where I'm running into a problem is actually using this dictionary in my c# code. Currently I'm able to call the query with the code below:

 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));
                    }
            }

I thought that I would be able to deserialze it to a dictionary later on in the code, but I'm getting the error: Unable to cast object of type;Newtonsoft.Json.Linq.JObject; to type;System.String;. at System.Data.DataTableReader2.GetString(Int32 ordinal) which makes sense, but I'm not sure how to work around it. Is it possible to use the dictionary that contains my mapping in my code??

Thanks @ Yoni L. Your commands helped to fix the Deserialization of SQL Reader JSON Response .

Please find the following Workaround.

  • Replace reader.GetString(0) with reader.GetValue(0).ToString()
  • Read all the Query result and save it in a StringBuilder .
jsonResultSB.Append(reader.GetValue(0).ToString());
  • After Deserialize all the Query Result in a single step.
JsonResultLists = JsonConvert.DeserializeObject<List<JsonResultList>>(jsonResultSB.ToString());

Use the below code to fix your issue.

# 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;

Refer here for more information about Reading SQL JSON result in .NET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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