azure - 来自 C# 的 Kusto 查询

标签 azure azure-data-explorer

我想从 c# 应用程序中检索 Kusto DB 的数据,任何人都可以帮助我。 我了解编写 Kusto 查询的知识,但需要一些帮助来从 Azure 托管的 Azure Kusto DB 中提取数据。

我尝试了以下代码,但它不起作用:

var client = Kusto.Data.Net.Client.KustoClientFactory.CreateCslQueryProvider("https://help.kusto.windows.net/Samples;Fed=true");
var reader = client.ExecuteQuery("MyTable | count");
// Read the first row from reader -- it's 0'th column is the count of records in MyTable
// Don't forget to dispose of reader when done.

最佳答案

您能否详细说明上面的代码不起作用(您收到的错误消息是什么)?

此外,下面还提供了一个完整(虽然简单)的示例:

// This sample illustrates how to query Kusto using the Kusto.Data .NET library.
//
// For the purpose of demonstration, the query being sent retrieves multiple result sets.
//
// The program should execute in an interactive context (so that on first run the user
// will get asked to sign in to Azure AD to access the Kusto service).
class Program
{
    const string Cluster = "https://help.kusto.windows.net";
    const string Database = "Samples";
    static void Main()
    {
        // The query provider is the main interface to use when querying Kusto.
        // It is recommended that the provider be created once for a specific target database,
        // and then be reused many times (potentially across threads) until it is disposed-of.
        var kcsb = new KustoConnectionStringBuilder(Cluster, Database)
        .WithAadUserPromptAuthentication();
        using (var queryProvider = KustoClientFactory.CreateCslQueryProvider(kcsb))
        {
            // The query -- Note that for demonstration purposes, we send a query that asks for two different
            // result sets (HowManyRecords and SampleRecords).
            var query = "StormEvents | count | as HowManyRecords; StormEvents | limit 10 | project StartTime, EventType, State | as SampleRecords";
            // It is strongly recommended that each request has its own unique
            // request identifier. This is mandatory for some scenarios (such as cancelling queries)
            // and will make troubleshooting easier in others.
            var clientRequestProperties = new ClientRequestProperties() { ClientRequestId = Guid.NewGuid().ToString() };
            using (var reader = queryProvider.ExecuteQuery(query, clientRequestProperties))
            {
                // Read HowManyRecords
                while (reader.Read())
                {
                    var howManyRecords = reader.GetInt64(0);
                    Console.WriteLine($"There are {howManyRecords} records in the table");
                }

                // Move on to the next result set, SampleRecords
                reader.NextResult();
                Console.WriteLine();
                while (reader.Read())
                {
                    // Important note: For demonstration purposes we show how to read the data
                    // using the "bare bones" IDataReader interface. In a production environment
                    // one would normally use some ORM library to automatically map the data from
                    // IDataReader into a strongly-typed record type (e.g. Dapper.Net, AutoMapper, etc.)
                    DateTime time = reader.GetDateTime(0);
                    string type = reader.GetString(1);
                    string state = reader.GetString(2);
                    Console.WriteLine("{0}\t{1,-20}\t{2}", time, type, state);
                }
            }
        }
    }
}

关于azure - 来自 C# 的 Kusto 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53397728/

相关文章:

python - 使用 Azure Functions 中的 Python 根据 Azure Blob 存储中的模式匹配检查 Blob 是否存在

ASP.NET Core 应用程序中的 Azure SignalR 服务

c# - 尝试在 Azure B2C 上更新用户时出现 "Insufficient Privileges"错误

azure - 计算库斯托百分比

azure-data-explorer - bag_unpack 在 Kusto 中的性能如何?

Azure 数据浏览器摄取批处理策略未按预期工作

带有 WHERE 子句的 Azure Log Analytics 查询不会产生任何结果

azure - 来自事件中心的数据未填充到 ADX 数据库中

azure - 我可以通过 Databricks 将数据提取到 azure 数据资源管理器中的表中吗?

azure - 仅限制对同一订阅中的角色的 SQL Azure 访问