简体   繁体   English

将 KQL 查询使用的所有表名放入 C# 中的列表中

[英]Putting all table names that a KQL query uses into a List in C#

Let's say I have a KQL query that uses several tables to retrieve the data.假设我有一个使用多个表来检索数据的 KQL 查询。 I need to write some code in C#, that will take all the tables used by a given KQL query, and put all those table names into a list.我需要在 C# 中编写一些代码,它将获取给定 KQL 查询使用的所有表,并将所有这些表名放入一个列表中。

Simply put: I need to analyze each KQL query to know from which tables it gets the data.简单地说:我需要分析每个 KQL 查询以了解它从哪些表中获取数据。

I already tried doing so by writing this code:我已经尝试通过编写以下代码来做到这一点:

var query = "Table1 | project a ,b,c";
       var code = KustoCode.Parse(query);‏
var parseCode = code.Analyze();
Console.WriteLine(parseCode.ResultType.Display.ToString());

But this doesn't return the tables names, but instead it returns the columns names that this query used, which is not what I want.但这不会返回名,而是返回此查询使用的名,这不是我想要的。

If you could help me solve this I would greatly appreciate it!如果您能帮我解决这个问题,我将不胜感激!

I don't believe this is knowable a priori as some table names can be resolved at query time and some queries (for example using find) can cover all/multiple tables that are not enumerated until query time.我不相信这是先验的,因为某些表名可以在查询时解析,并且某些查询(例如使用 find)可以涵盖直到查询时才枚举的所有/多个表。

You can find this information shortly after the fact if you turn on Table Usage Statistics in the 'Diagnostic settings' tab in the azure portal view of your ADX cluster.如果您在 ADX 集群的 azure 门户视图中的“诊断设置”选项卡中打开表使用统计信息,您可以在事后不久找到此信息。 This will pump detailed information into an app insights resource that will show what table/extents get used.这会将详细信息输入到应用程序洞察资源中,该资源将显示使用了哪些表/范围。

This would seem to do the trick (based on .show queryplan )这似乎可以解决问题(基于.show queryplan

using System.Data;
using Newtonsoft.Json.Linq;

string query = @".show queryplan <| search in (database('*').*) * | summarize count() by $table";
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);

    reader.Read();
    reader.Read();
    string relop_tree = reader.GetString(2);
    JObject content = JObject.Parse(relop_tree);

    IEnumerable<JToken> sources = content.SelectTokens("$.source.operands..[?(@.source.type == 'TableShardReference')].source");

    foreach (JToken source in sources)
    {
        Console.WriteLine($"{source.Value<string>("database"), -50}{source.Value<string>("table")}");
    }
}

ContosoSales                                      SalesFact
ContosoSales                                      Products
ContosoSales                                      Customers
ContosoSales                                      SalesTable
KustoMonitoringPersistentDatabase                 KustoMonitoringTable
SampleIoTData                                     RawSensorsData
SampleIoTData                                     TransformedSensorsData
SampleIoTData                                     _MV_LatestSensorValue
SampleLogs                                        RawSysLogs
SampleLogs                                        TransformedSysLogs
SampleLogs                                        TraceLogs
SampleLogs                                        TargetTable
SampleMetrics                                     RawServerMetrics
SampleMetrics                                     TransformedServerMetrics
SampleMetrics                                     _MV_TransformedMetricsDedup
SampleMetrics                                     SQLServerLocation
SampleMetrics                                     _MV_AvgPerfWithServersHierarchy
Samples                                           StormEvents
Samples                                           demo_make_series1
Samples                                           demo_series2
Samples                                           demo_series3
Samples                                           demo_many_series1
Samples                                           ConferenceSessions
Samples                                           demo_make_series2
Samples                                           demo_clustering1
Samples                                           Covid19_Bing
Samples                                           Covid19_map2
Samples                                           US_States
Samples                                           US_Counties
Samples                                           irregular_ts
Samples                                           _MV_DailyCovid19
Samples                                           demo_prometheus
Samples                                           PopulationData
Samples                                           OccupancyDetection
Samples                                           SamplePowerRequirementHistorizedData

Thanks for the help: I finally was able to find a solution for this so here is my code:感谢您的帮助:我终于找到了解决方案,所以这是我的代码:

var code = KustoCode.Parse(query).Analyze();

SyntaxElement.WalkNodes(code.Syntax,
       Operator =>
       {
           if (Operator is Expression e && e.RawResultType is TableSymbol && Operator.Kind.ToString() == "NameReference")
               tables.Add(e.ToString());
       })

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

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