編輯

共用方式為


Hello Kusto: Create your first app

Applies to: ✅ Microsoft FabricAzure Data Explorer

In this article, you learn how to:

  • Create your first client app
  • Use interactive authentication
  • Run a basic query that prints Hello Kusto!

Prerequisites

Set up your development environment to use the Kusto client library.

Create your app

In your preferred IDE or text editor, create a project or file named hello kusto using the convention appropriate for your preferred language. Then add the following code:

  1. Add the Kusto client and string builder classes.

    using Kusto.Data;
    using Kusto.Data.Net.Client;
    
  2. Define an empty function named main and call it.

    namespace HelloKusto {
      class HelloKusto {
        static void Main(string[] args) {
        }
      }
    }
    
  3. Create a connection string builder object that defines the cluster URI and sets the authentication mode to interactive. For more information about the cluster URI, see Kusto connection strings.

    var clusterUri = "https://help.kusto.windows.net/";
    var kcsb = new KustoConnectionStringBuilder(clusterUri).WithAadUserPromptAuthentication();
    

    Note

    For interactive authentication, you need a Microsoft account or a Microsoft Entra user identity. An Azure subscription isn't required.

    In C#, the interactive authentication process may not prompt the user if:

    • The user is already authenticated on the device
    • There is an existing Kusto.Explorer or Azure Date Explorer web UI authentication on the device
  4. Create a client object that uses the connection string builder object to connect to the cluster.

    Note

    We highly recommended that you cache and reuse the Kusto client instance. Frequently recreating Kusto clients may lead to performance degradation in your application and increased load on your cluster.

    using (var kustoClient = KustoClientFactory.CreateCslQueryProvider(kcsb)) {
    }
    
  5. Define the database and query to run. The query prints Hello Kusto! in a column named Welcome.

    var database = "Samples";
    var query = "print Welcome='Hello Kusto!'";
    
  6. Run the query and print the result.

    using (var response = kustoClient.ExecuteQuery(database, query, null)) {
      response.Read();
      int columnNo = response.GetOrdinal("Welcome");
      Console.WriteLine(response.GetString(columnNo));
    }
    

    Note

    The query output is returned in the response as an object that contains one or more tables, comprised of one more more rows and columns. The format of the object depends on the client library language.

    The print kusto query returns a single table with one row and column.

    The response is a DataReader object. You can reference the result, as follows:

    • Use the Read() method to read the first row
    • Use the GetString() method to get the value of the first column

The complete code should look like this:

using Kusto.Data;
using Kusto.Data.Net.Client;

namespace HelloKusto {
  class HelloKusto {
    static void Main(string[] args) {
      string clusterUri = "https://help.kusto.windows.net/";
      var kcsb = new KustoConnectionStringBuilder(clusterUri).WithAadUserPromptAuthentication();
    
      using (var kustoClient = KustoClientFactory.CreateCslQueryProvider(kcsb)) {
        string database = "Samples";
        string query = "print Welcome='Hello Kusto!'";

        using (var response = kustoClient.ExecuteQuery(database, query, null)) {
          response.Read();
          int columnNo = response.GetOrdinal("Welcome");
          Console.WriteLine(response.GetString(columnNo));
        }
      }
    }
  }
}

Run your app

In a command shell, use the following command to run your app:

# Change directory to the folder that contains the hello world project
dotnet run .

You should see a result similar to the following:

Hello Kusto!

Next step