在 C# 中获取未知 Kusto 查询的结果
Fetch results of unknown Kusto query in C#
我正在尝试从未知的 Kusto 查询中获取结果。
示例:
var query = "StormEvents | count | as HowManyRecords; StormEvents | limit 10 | project StartTime, EventType, State | as SampleRecords";
假设我对这个查询一无所知,我的 C# 客户端是否有某种方法可以分解 Kusto 返回给我的内容(这里它将返回 2 个不同的东西)并将响应封装在适当的C# 对象 ?
我们可以遍历结果集,对每个 table 使用 GetSchemaTable()
这是一个简单的例子。
请注意,除了查询结果外,我们还得到描述渲染选项的 tables、执行统计信息和所有 tables 的名称。
using System.Data;
string query = "PopulationData | take 3; StormEvents | take 5";
string cluster = "https://help.kusto.windows.net/Samples";
using (var client = Kusto.Data.Net.Client.KustoClientFactory.CreateCslQueryProvider($"{cluster};Fed=true"))
{
using IDataReader reader = client.ExecuteQuery(query);
do
{
Console.WriteLine(new string('*', 40));
DataTable? schemaTable = reader.GetSchemaTable();
if (schemaTable != null)
{
foreach (DataRow row in schemaTable.Rows)
{
Console.WriteLine($"{row["ColumnOrdinal"],3} {row["ColumnName"],-20} {row["ColumnType"],-20}");
}
}
}
while (reader.NextResult());
}
****************************************
0 State string
1 Population long
****************************************
0 StartTime datetime
1 EndTime datetime
2 EpisodeId int
3 EventId int
4 State string
5 EventType string
6 InjuriesDirect int
7 InjuriesIndirect int
8 DeathsDirect int
9 DeathsIndirect int
10 DamageProperty int
11 DamageCrops int
12 Source string
13 BeginLocation string
14 EndLocation string
15 BeginLat real
16 BeginLon real
17 EndLat real
18 EndLon real
19 EpisodeNarrative string
20 EventNarrative string
21 StormSummary dynamic
****************************************
0 Value string
****************************************
0 Timestamp datetime
1 Severity int
2 SeverityName string
3 StatusCode int
4 StatusDescription string
5 Count int
6 RequestId guid
7 ActivityId guid
8 SubActivityId guid
9 ClientActivityId string
****************************************
0 Ordinal long
1 Kind string
2 Name string
3 Id string
4 PrettyName string
我正在尝试从未知的 Kusto 查询中获取结果。
示例:
var query = "StormEvents | count | as HowManyRecords; StormEvents | limit 10 | project StartTime, EventType, State | as SampleRecords";
假设我对这个查询一无所知,我的 C# 客户端是否有某种方法可以分解 Kusto 返回给我的内容(这里它将返回 2 个不同的东西)并将响应封装在适当的C# 对象 ?
我们可以遍历结果集,对每个 table 使用 GetSchemaTable()
这是一个简单的例子。
请注意,除了查询结果外,我们还得到描述渲染选项的 tables、执行统计信息和所有 tables 的名称。
using System.Data;
string query = "PopulationData | take 3; StormEvents | take 5";
string cluster = "https://help.kusto.windows.net/Samples";
using (var client = Kusto.Data.Net.Client.KustoClientFactory.CreateCslQueryProvider($"{cluster};Fed=true"))
{
using IDataReader reader = client.ExecuteQuery(query);
do
{
Console.WriteLine(new string('*', 40));
DataTable? schemaTable = reader.GetSchemaTable();
if (schemaTable != null)
{
foreach (DataRow row in schemaTable.Rows)
{
Console.WriteLine($"{row["ColumnOrdinal"],3} {row["ColumnName"],-20} {row["ColumnType"],-20}");
}
}
}
while (reader.NextResult());
}
****************************************
0 State string
1 Population long
****************************************
0 StartTime datetime
1 EndTime datetime
2 EpisodeId int
3 EventId int
4 State string
5 EventType string
6 InjuriesDirect int
7 InjuriesIndirect int
8 DeathsDirect int
9 DeathsIndirect int
10 DamageProperty int
11 DamageCrops int
12 Source string
13 BeginLocation string
14 EndLocation string
15 BeginLat real
16 BeginLon real
17 EndLat real
18 EndLon real
19 EpisodeNarrative string
20 EventNarrative string
21 StormSummary dynamic
****************************************
0 Value string
****************************************
0 Timestamp datetime
1 Severity int
2 SeverityName string
3 StatusCode int
4 StatusDescription string
5 Count int
6 RequestId guid
7 ActivityId guid
8 SubActivityId guid
9 ClientActivityId string
****************************************
0 Ordinal long
1 Kind string
2 Name string
3 Id string
4 PrettyName string