Share the love

Here is an example of .NET code that connects to a Kusto cluster, fetches data, and exports the report in Azure Blob Storage:

using System;
using System.IO;
using Microsoft.Azure.Kusto.Data;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

class Program
{
    static void Main(string[] args)
    {
        // Kusto Connection String
        string kustoConnectionString = "Data Source=https://yourcluster.kusto.windows.net;Initial Catalog=yourdatabase;User ID=yourusername;Password=yourpassword;";
        
        // Azure Blob Storage Connection String
        string storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=youraccountname;AccountKey=youraccountkey;EndpointSuffix=core.windows.net";
        
        // Kusto Query
        string query = "your_query";
        
        // File name for the report
        string fileName = "report.csv";
        
        // Container name in Azure Blob Storage
        string containerName = "yourcontainer";

        // Connect to Kusto
        KustoConnection kustoConnection = KustoClientFactory.CreateConnection(kustoConnectionString);
        KustoCommand kustoCommand = kustoConnection.CreateCommand();
        kustoCommand.CommandText = query;
        KustoDataReader kustoDataReader = kustoCommand.ExecuteReader();

        // Convert Kusto data to CSV
        var csv = new StringWriter();
        for (int i = 0; i < kustoDataReader.FieldCount; i++)
        {
            csv.Write(kustoDataReader.GetName(i));
            if (i < kustoDataReader.FieldCount - 1)
            {
                csv.Write(",");
            }
            }
            csv.WriteLine();
            while (kustoDataReader.Read())
            {
                for (int i = 0; i < kustoDataReader.FieldCount; i++)
                {
                    csv.Write(kustoDataReader[i].ToString());
                    if (i < kustoDataReader.FieldCount - 1)
                    {
                        csv.Write(",");
                    }
                }
                csv.WriteLine();
            }

        // Close Kusto connection
        kustoDataReader.Close();
        kustoConnection.Close();

        // Connect to Azure Blob Storage
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
        CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);

        // Upload report to Azure Blob Storage
        using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(csv.ToString())))
        {
            blockBlob.UploadFromStreamAsync(stream);
        }
        Console.WriteLine("Report exported successfully to Azure Blob Storage.");
    }
}

This code connects to a Kusto cluster using the Kusto Connection String, fetch data by executing the provided query, then converts the Kusto data to CSV format. After that, it connects to Azure Blob Storage using the Azure Blob Storage connection string, creates a container and upload the report to the container with the provided file name.

It’s important to note that you need to have the Microsoft.Azure.Kusto.Data and Microsoft.WindowsAzure.Storage NuGet packages installed in your project in order to use the Kusto and Azure Blob Storage classes.

Also you need to replace the placeholders with the appropriate values for your specific scenario. You need to provide the Kusto cluster URL, the database name, your username and password, the query you want to execute, the file name you want to save the report as, the container name on the Azure Blob Storage and the storage account name and storage account key.