SDK を使用して Azure Data Explorerの Event Grid データ接続を作成する

この記事では、Event Grid データ接続を使用して、ストレージ アカウントから Azure Data Explorer に BLOB を取り込む方法を説明します。 Azure Event Grid サブスクリプションを設定する Event Grid データ接続を作成します。 Event Grid サブスクリプションは、Azure Event Hubs 経由でストレージアカウントから Azure Data Explorer にイベントをルーティングします。

Azure portalまたは ARM テンプレートを使用して接続を作成する方法については、「Event Grid データ接続を作成する」を参照してください。

Event Grid から Azure Data Explorer への取り込みに関する一般的な情報については、Event Grid への接続に関する記事を参照してください。

注意

Event Grid 接続で最高のパフォーマンスを実現するには、BLOB メタデータを rawSizeBytes 使用してインジェスト プロパティを設定します。 詳細については、「 インジェスト プロパティ」を参照してください。

以前の SDK バージョンに基づくコード サンプルについては、 アーカイブされた記事を参照してください。

前提条件

Event Grid データ接続を作成する

このセクションでは、Event Grid と Azure Data Explorer テーブルの間に接続を確立します。

  1. Microsoft.Azure.Management.Kusto NuGet パッケージをインストールします。

  2. 認証に使用するMicrosoft Entra アプリケーション プリンシパルを作成します。 ディレクトリ (テナント) ID、アプリケーション ID、クライアント シークレットが必要です。

  3. 次のコードを実行します。

    var tenantId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; //Directory (tenant) ID
    var clientId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; //Application ID
    var clientSecret = "PlaceholderClientSecret"; //Client Secret
    var subscriptionId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";
    var credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);
    var resourceManagementClient = new ArmClient(credentials, subscriptionId);
    var resourceGroupName = "testrg";
    //The cluster and database that are created as part of the Prerequisites
    var clusterName = "mykustocluster";
    var databaseName = "mykustodatabase";
    var subscription = await resourceManagementClient.GetDefaultSubscriptionAsync();
    var resourceGroup = (await subscription.GetResourceGroupAsync(resourceGroupName)).Value;
    var cluster = (await resourceGroup.GetKustoClusterAsync(clusterName)).Value;
    var database = (await cluster.GetKustoDatabaseAsync(databaseName)).Value;
    var dataConnections = database.GetKustoDataConnections();
    var eventGridConnectionName = "myeventgridconnect";
    //The event hub and storage account that are created as part of the Prerequisites
    var eventHubResourceId = new ResourceIdentifier("/subscriptions/<storageAccountSubscriptionId>/resourceGroups/<storageAccountResourceGroupName>/providers/Microsoft.Storage/storageAccounts/<storageAccountName>");
    var storageAccountResourceId = new ResourceIdentifier("/subscriptions/<eventHubSubscriptionId>/resourceGroups/<eventHubResourceGroupName>/providers/Microsoft.EventHub/namespaces/<eventHubNamespaceName>/eventhubs/<eventHubName>");
    var consumerGroup = "$Default";
    var location = AzureLocation.CentralUS;
    //The table and column mapping are created as part of the Prerequisites
    var tableName = "StormEvents";
    var mappingRuleName = "StormEvents_CSV_Mapping";
    var dataFormat = KustoEventGridDataFormat.Csv;
    var blobStorageEventType = BlobStorageEventType.MicrosoftStorageBlobCreated;
    var databaseRouting = KustoDatabaseRouting.Multi;
    var eventGridConnectionData = new KustoEventGridDataConnection
    {
        StorageAccountResourceId = storageAccountResourceId, EventHubResourceId = eventHubResourceId,
        ConsumerGroup = consumerGroup, TableName = tableName, Location = location, MappingRuleName = mappingRuleName,
        DataFormat = dataFormat, BlobStorageEventType = blobStorageEventType, DatabaseRouting = databaseRouting
    };
    await dataConnections.CreateOrUpdateAsync(WaitUntil.Completed, eventGridConnectionName, eventGridConnectionData);
    
    設定 推奨値 フィールドの説明
    tenantId xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx テナント ID。 ディレクトリ ID とも呼ばれます。
    subscriptionId xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx リソースの作成に使用するサブスクリプション ID。
    clientId xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx ご利用のテナント内のリソースにアクセスできるアプリケーションのクライアント ID。
    clientSecret PlaceholderClientSecret ご利用のテナント内のリソースにアクセスできるアプリケーションのクライアント シークレット。
    resourceGroupName testrg ご利用のクラスターを含むリソース グループの名前。
    clusterName mykustocluster ご利用のクラスターの名前。
    databaseName mykustodatabase ご利用のクラスター内のターゲット データベースの名前。
    eventGridConnectionName myeventgridconnect データ接続の任意の名前。
    tableName StormEvents ターゲット データベース内のターゲット テーブルの名前。
    mappingRuleName StormEvents_CSV_Mapping ターゲット テーブルに関連付けられている列マッピングの名前。
    dataFormat csv メッセージのデータ形式。
    eventHubResourceId リソース ID Event Grid がイベントを送信するように構成されているイベント ハブのリソース ID。
    storageAccountResourceId リソース ID インジェスト用のデータを保持しているストレージ アカウントのリソース ID。
    consumerGroup $Default ご利用のイベント ハブのコンシューマー グループ。
    location 米国中部 データ接続リソースの場所。
    blobStorageEventType Microsoft.Storage.BlobCreated インジェストをトリガーするイベントの種類。 サポートされているイベントは次のとおりです。Microsoft.Storage.BlobCreated または Microsoft.Storage.BlobRenamed。 BLOB の名前変更は、ADLSv2 ストレージに対してのみサポートされています。
    databaseRouting Multi または Single 接続のデータベース ルーティング。 値を Single に設定すると、databaseName 設定で指定したクラスター内の単一のデータベースにデータ接続はルーティングされます。 値を Multi に設定すると、Databaseインジェスト プロパティを使用して、既定のターゲット データベースを上書きできます。 詳細については、「イベント ルーティング」を参照してください。

Event Grid データ接続を使用する

このセクションでは、BLOB の作成または BLOB の名前変更に従って、Azure Blob Storageまたは Azure Data Lake Gen 2 からクラスターへのインジェストをトリガーする方法について説明します。

BLOB のアップロードに使用されるストレージ SDK の種類に基づいて、関連するタブを選択します。

次のコード サンプルでは、Azure Blob Storage SDK を使用してファイルをAzure Blob Storageにアップロードします。 アップロードによって Event Grid データ接続がトリガーされ、Azure Data Explorerにデータが取り込まれます。

var azureStorageAccountConnectionString=<storage_account_connection_string>;
var containerName = <container_name>;
var blobName = <blob_name>;
var localFileName = <file_to_upload>;
var uncompressedSizeInBytes = <uncompressed_size_in_bytes>;
var mapping = <mappingReference>;
// Create a new container in your storage account.
var azureStorageAccount = CloudStorageAccount.Parse(azureStorageAccountConnectionString);
var blobClient = azureStorageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
// Set metadata and upload a file to the blob.
var blob = container.GetBlockBlobReference(blobName);
blob.Metadata.Add("rawSizeBytes", uncompressedSizeInBytes);
blob.Metadata.Add("kustoIngestionMappingReference", mapping);
blob.UploadFromFile(localFileName);
// Confirm success of the upload by listing the blobs in your container.
var blobs = container.ListBlobs();

注意

Azure Data Explorer では、BLOB 投稿の取り込みは削除されません。 BLOB の削除を管理する Azure Blob Storage のライフサイクルを使用して、BLOB を 3 から 5 日間保持します。

注意

操作に続くインジェストの CopyBlob トリガーは、階層型名前空間機能が有効になっているストレージ アカウントではサポートされていません。

Event Grid データ接続を削除する

Event Grid 接続を削除するには、次のコマンドを実行します。

kustoManagementClient.DataConnections.Delete(resourceGroupName, clusterName, databaseName, dataConnectionName);