public List <string> Query(string query) { Log.Info($"query:{query}", ConsoleColor.Blue); if (_kustoQueryClient == null) { _kustoQueryClient = KustoClientFactory.CreateCslQueryProvider(ManagementConnection); queryTimer = new Timer(DisposeQueryClient, null, maxKustoClientTimeMs, maxKustoClientTimeMs); } try { queryTimer.Change(maxKustoClientTimeMs, maxKustoClientTimeMs); // unable to parse multiple tables v1 or v2 using kusto so using httpclient and rest string requestBody = "{ \"db\": \"" + DatabaseName + "\", \"csl\": \"" + query + "\" }"; string requestId = new Guid().ToString(); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("accept", "application/json"); headers.Add("host", HostName); headers.Add("x-ms-client-request-id", requestId); Log.Info($"query:", requestBody); _httpClient.DisplayResponse = Config.LogDebug >= LoggingLevel.Verbose; _httpClient.SendRequest(uri: RestQueryUri, authToken: _arm.BearerToken, jsonBody: requestBody, httpMethod: HttpMethod.Post, headers: headers); ResponseDataSet = JsonConvert.DeserializeObject <KustoRestResponseV1>(_httpClient.ResponseStreamString); if (!ResponseDataSet.HasData()) { Log.Info($"no tables:", ResponseDataSet); return(new List <string>()); } KustoRestTableOfContentsV1 toc = SetTableOfContents(ResponseDataSet); if (toc.HasData) { SetExtendedProperties(); long index = toc.Rows.FirstOrDefault(x => x.Kind.Equals("QueryResult")).Ordinal; PrimaryResultTable = new KustoRestTable(ResponseDataSet.Tables[index]); return(PrimaryResultTable.RecordsCsv()); } else { TableOfContents = new KustoRestTableOfContentsV1(); Cursor = "''"; ExtendedPropertiesTable = new KustoRestTable(); PrimaryResultTable = new KustoRestTable(ResponseDataSet.Tables[0]); return(PrimaryResultTable.RecordsCsv()); } } catch (Exception e) { Log.Exception($"exception executing query: {query}\r\n{e}"); return(new List <string>()); } }
private void SetExtendedProperties() { // extended properties stored in single 'Value' column as key value pair in json string string tableName = "@ExtendedProperties"; if (TableOfContents.Rows.Any(x => x.Name.Equals(tableName))) { long index = TableOfContents.Rows.FirstOrDefault(x => x.Name.Equals(tableName)).Ordinal; ExtendedPropertiesTable = new KustoRestTable(ResponseDataSet.Tables[index]); } }
private void SetExtendedProperties() { // extended properties stored in single 'Value' column as key value pair in json string string columnName = "Value"; string extendedProperty = "Cursor"; string tableName = "@ExtendedProperties"; if (TableOfContents.Rows.Any(x => x.Name.Equals(tableName))) { long index = TableOfContents.Rows.FirstOrDefault(x => x.Name.Equals(tableName)).Ordinal; ExtendedPropertiesTable = new KustoRestTable(ResponseDataSet.Tables[index]); Dictionary <string, object> jsonString = ExtendedPropertiesTable.Records().FirstOrDefault(record => record[columnName].ToString().Contains(extendedProperty)); if (jsonString != null) { JObject jObject = (JObject)JsonConvert.DeserializeObject(jsonString[columnName].ToString()); Cursor = $"'{jObject.GetValue(extendedProperty)}'"; } } }
public List <string> Query(string query) { Log.Info($"query:{query}", ConsoleColor.Blue); if (_kustoQueryClient == null) { _kustoQueryClient = KustoClientFactory.CreateCslQueryProvider(ManagementConnection); } if (queryTimer == null) { queryTimer = new Timer(DisposeQueryClient, null, maxKustoClientTimeMs, maxKustoClientTimeMs); } try { PrimaryResultTable = null; QueryResultTables.Clear(); Cursor = null; queryTimer.Change(maxKustoClientTimeMs, maxKustoClientTimeMs); // unable to parse multiple tables v1 or v2 using kusto so using httpclient and rest string requestBody = "{ \"db\": \"" + DatabaseName + "\", \"csl\": \"" + query + ";print Cursor=current_cursor()\" }"; string requestId = new Guid().ToString(); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("accept", "application/json"); headers.Add("host", HostName); headers.Add("x-ms-client-request-id", requestId); Log.Info($"query:", requestBody); _httpClient.SendRequest(uri: RestQueryUri, authToken: _arm.BearerToken, jsonBody: requestBody, httpMethod: HttpMethod.Post, headers: headers); ResponseDataSet = JsonConvert.DeserializeObject <KustoRestResponseV1>(_httpClient.ResponseStreamString); if (!ResponseDataSet.HasData()) { Log.Info($"no tables:", ResponseDataSet); return(new List <string>()); } KustoRestTableOfContentsV1 toc = SetTableOfContents(ResponseDataSet); if (toc.HasData) { SetExtendedProperties(); List <long> indexes = toc.Rows.Where(x => x.Kind.Equals("QueryResult")).Select(x => x.Ordinal).ToList(); foreach (long index in indexes) { KustoRestTable table = new KustoRestTable(ResponseDataSet.Tables[index]); QueryResultTables.Add(table); if (PrimaryResultTable == null) { PrimaryResultTable = table; continue; } if (table.Columns.FirstOrDefault(x => x.ColumnName.Contains("Cursor")) != null) { Cursor = table.Records().FirstOrDefault()["Cursor"].ToString(); } } Log.Debug($"table cursor: {Cursor}"); return(PrimaryResultTable.RecordsCsv()); } else { TableOfContents = new KustoRestTableOfContentsV1(); ExtendedPropertiesTable = new KustoRestTable(); PrimaryResultTable = new KustoRestTable(ResponseDataSet.Tables[0]); ResponseDataSet.Tables.ForEach(x => QueryResultTables.Add(new KustoRestTable(x))); return(PrimaryResultTable.RecordsCsv()); } } catch (Exception e) { Log.Exception($"exception executing query: {query}\r\n{e}"); return(new List <string>()); } }