本文說明如何從 IoT 中樞、巨量數據串流平臺和IoT擷取服務將數據內嵌至 Azure 資料總管。
若要瞭解如何使用 Kusto SDK 建立連線,請參閱使用 SDK 建立 IoT 中樞 數據連線。
如需從 IoT 中樞 擷取至 Azure 數據總管的一般資訊,請參閱連線到 IoT 中樞。
注意
只有在您建立數據連線之後才會加入佇列的事件才會內嵌。
如需以舊版 SDK 為基礎的程式代碼範例,請參閱 封存一文。
必要條件
- Azure 訂用帳戶。 建立免費的 Azure 帳戶。
- Azure 資料總管叢集和資料庫。 建立叢集和資料庫。
- 目的地數據表。 建立數據表 或使用現有的數據表。
- 數據表的擷取對應。
- 具有擷取數據的 IoT 中樞。
建立 IoT 中樞 數據連線
在本節中,您將建立 IoT 中樞 與 Azure 數據總管數據表之間的連線。 只要此連線已就緒,數據就會從 IoT 中樞 傳輸至目標數據表。
建立要用於驗證的Microsoft Entra 應用程式主體 。 您將需要目錄(租使用者)識別碼、應用程式識別碼和客戶端密碼。
執行下列程式碼。
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 iotHubConnectionName = "myiothubconnect"; //The IoT hub that is created as part of the Prerequisites var iotHubResourceId = new ResourceIdentifier("/subscriptions/<iotHubSubscriptionId>/resourceGroups/<iotHubResourceGroupName>/providers/Microsoft.Devices/IotHubs/<iotHubName>"); var sharedAccessPolicyName = "iothubforread"; 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 = KustoIotHubDataFormat.Csv; var databaseRouting = KustoDatabaseRouting.Multi; var iotHubConnectionData = new KustoIotHubDataConnection { IotHubResourceId = iotHubResourceId, ConsumerGroup = consumerGroup, SharedAccessPolicyName = sharedAccessPolicyName, Location = location, TableName = tableName, MappingRuleName = mappingRuleName, DataFormat = dataFormat, DatabaseRouting = databaseRouting }; await dataConnections.CreateOrUpdateAsync(WaitUntil.Completed, iotHubConnectionName, iotHubConnectionData);
設定 建議的值 欄位描述 tenantId xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx 您的租用戶識別碼。 也稱為目錄標識碼。 subscriptionId xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx 您用來建立資源的訂用帳戶標識碼。 clientId xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx 應用程式可存取租用戶中資源的用戶端標識碼。 clientSecret PlaceholderClientSecret 應用程式可存取租用戶中資源的客戶端密碼。 resourceGroupName testrg 包含叢集的資源群組名稱。 clusterName mykustocluster 您叢集的名稱。 databaseName mykustodatabase 叢集中目標資料庫的名稱。 iotHubConnectionName myiothubconnect 數據連線所需的名稱。 tableName StormEvents 目標資料庫中的目標數據表名稱。 mappingRuleName StormEvents_CSV_Mapping 與目標數據表相關的數據行對應名稱。 dataFormat csv 訊息的數據格式。 iotHubResourceId 資源識別碼 IoT 中樞的資源標識碼,其中包含要擷取的數據。 sharedAccessPolicyName iothubforread 共用存取原則的名稱,定義要連線到 IoT 中樞 之裝置和服務的許可權。 consumerGroup $Default 事件中樞的取用者群組。 location Central US 數據連線資源的位置。 databaseRouting 多重 或 單一 線上的資料庫路由。 如果您將值設定為Single,數據聯機會路由傳送至叢集中的單一資料庫,如 databaseName 設定中所指定。 如果您將值設定為 Multi,您可以使用資料庫擷取屬性覆寫預設目標資料庫。 如需詳細資訊,請參閱 事件路由。
拿掉 IoT 中樞 資料連線
若要移除 IoT 中樞 連線,請執行下列命令:
kustoManagementClient.DataConnections.Delete(resourceGroupName, clusterName, databaseName, dataConnectionName);