Create an Azure Data Explorer cluster and database by using C#

Azure Data Explorer is a fast, fully managed data analytics service for real-time analysis on large volumes of data streaming from applications, websites, IoT devices, and more. To use Azure Data Explorer, you first create a cluster, and create one or more databases in that cluster. Then you ingest (load) data into a database so that you can run queries against it. In this article, you create a cluster and a database by using C#.

Prerequisites

  • If you don't have Visual Studio 2019 installed, you can download and use the free Visual Studio 2019 Community Edition. Make sure that you enable Azure development during the Visual Studio setup.
  • If you don't have an Azure subscription, create a free Azure account before you begin.

Install C# NuGet

Authentication

For running the examples in this article, we need an Azure AD Application and service principal that can access resources. Check create an Azure AD application to create a free Azure AD Application and add role assignment at the subscription scope. It also shows how to get the Directory (tenant) ID, Application ID, and Client Secret.

Create the Azure Data Explorer cluster

  1. Create your cluster by using the following code:

    var tenantId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";//Directory (tenant) ID
    var clientId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";//Application ID
    var clientSecret = "xxxxxxxxxxxxxx";//Client Secret
    var subscriptionId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";
    var authenticationContext = new AuthenticationContext($"https://login.windows.net/{tenantId}");
    var credential = new ClientCredential(clientId, clientSecret);
    var result = await authenticationContext.AcquireTokenAsync(resource: "https://management.core.windows.net/", clientCredential: credential);
    
    var credentials = new TokenCredentials(result.AccessToken, result.AccessTokenType);
    
    var kustoManagementClient = new KustoManagementClient(credentials)
    {
        SubscriptionId = subscriptionId
    };
    
    var resourceGroupName = "testrg";
    var clusterName = "mykustocluster";
    var location = "Central US";
    var skuName = "Standard_D13_v2";
    var tier = "Standard";
    var capacity = 5;
    var sku = new AzureSku(skuName, tier, capacity);
    var cluster = new Cluster(location, sku);
    await kustoManagementClient.Clusters.CreateOrUpdateAsync(resourceGroupName, clusterName, cluster);
    
    Setting Suggested value Field description
    clusterName mykustocluster The desired name of your cluster.
    skuName Standard_D13_v2 The SKU that will be used for your cluster.
    tier Standard The SKU tier.
    capacity number The number of instances of the cluster.
    resourceGroupName testrg The resource group name where the cluster will be created.

    Note

    Create a cluster is a long running operation, so it's highly recommended to use CreateOrUpdateAsync, instead of CreateOrUpdate.

  2. Run the following command to check whether your cluster was successfully created:

    kustoManagementClient.Clusters.Get(resourceGroupName, clusterName);
    

If the result contains ProvisioningState with the Succeeded value, then the cluster was successfully created.

Create the database in the Azure Data Explorer cluster

  1. Create your database by using the following code:

    var hotCachePeriod = new TimeSpan(3650, 0, 0, 0);
    var softDeletePeriod = new TimeSpan(3650, 0, 0, 0);
    var databaseName = "mykustodatabase";
    var database = new ReadWriteDatabase(location: location, softDeletePeriod: softDeletePeriod, hotCachePeriod: hotCachePeriod);
    
    await kustoManagementClient.Databases.CreateOrUpdateAsync(resourceGroupName, clusterName, databaseName, database);
    
     [!NOTE]
     If you are using C# version 2.0.0 or below, use Database instead of ReadWriteDatabase.
    
    Setting Suggested value Field description
    clusterName mykustocluster The name of your cluster where the database will be created.
    databaseName mykustodatabase The name of your database.
    resourceGroupName testrg The resource group name where the cluster will be created.
    softDeletePeriod 3650:00:00:00 The amount of time that data will be kept available to query.
    hotCachePeriod 3650:00:00:00 The amount of time that data will be kept in cache.
  2. Run the following command to see the database that you created:

    kustoManagementClient.Databases.Get(resourceGroupName, clusterName, databaseName) as ReadWriteDatabase;
    

You now have a cluster and a database.

Clean up resources

  • If you plan to follow our other articles, keep the resources you created.

  • To clean up resources, delete the cluster. When you delete a cluster, it also deletes all the databases in it. Use the following command to delete your cluster:

    kustoManagementClient.Clusters.Delete(resourceGroupName, clusterName);
    

Next steps