Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kusto Query from c#

I want to retrieve data from Kusto DB from c# app can any one help me on this. I have knowledge on writing the Kusto queries but I need some help on pulling data from Azure Kusto DB hosted in Azure.

I tried the following code but it's not working:

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.
like image 500
R Kumar Avatar asked Nov 20 '18 16:11

R Kumar


People also ask

What is a kusto query?

A Kusto query is a read-only request to process data and return results. The request is stated in plain text, using a data-flow model that is easy to read, author, and automate. Kusto queries are made of one or more query statements.

What language does kusto use?

KQL stands for Kusto Query Language. It's the language used to query the Azure log databases: Azure Monitor Logs, Azure Monitor Application Insights and others.

Is SQL like kusto?

Kusto supports a subset of the SQL language. See the list of SQL known issues for the full list of unsupported features. The primary language to interact with Kusto is KQL (Kusto Query Language). To make the transition and learning experience easier, you can use Kusto to translate SQL queries to KQL.


1 Answers

Could you please elaborate what's not working (what is the error message you're getting) with the code above?

In addition, a full (though simple) example can be found below:

// 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);
                }
            }
        }
    }
}
like image 167
Yoni L. Avatar answered Nov 15 '22 02:11

Yoni L.