共用方式為


建立應用程式以執行管理命令

適用於:✅Microsoft FabricAzure 數據總管

在本文中,您將瞭解如何:

先決條件

設定開發環境 以使用 Kusto 客戶端程式庫。

執行管理命令並處理結果

在慣用的 IDE 或文字編輯器中,使用適合您慣用語言的慣例,建立名為 的管理命令 項目或檔案。 然後新增下列程式代碼:

  1. 建立連線叢集的用戶端應用程式。 將 <your_cluster_uri> 佔位元取代為您的叢集名稱。

    注意

    針對管理命令,您將使用 CreateCslAdminProvider 用戶端 Factory 方法。

    using Kusto.Data;
    using Kusto.Data.Net.Client;
    
    namespace ManagementCommands {
      class ManagementCommands {
        static void Main(string[] args) {
          var clusterUri = "<your_cluster_uri>";
          var kcsb = new KustoConnectionStringBuilder(clusterUri)
              .WithAadUserPromptAuthentication();
    
          using (var kustoClient = KustoClientFactory.CreateCslAdminProvider(kcsb)) {
          }
        }
      }
    }
    
  2. 定義會列印所執行命令及其結果數據表的函式。 此函式會將結果數據表中的數據行名稱解壓縮,並在新行上列印每個名稱/值組。

    static void PrintResultsAsValueList(string command, IDataReader response) {
      while (response.Read()) {
        Console.WriteLine("\n{0}\n", new String('-', 20));
        Console.WriteLine("Command: {0}", command);
        Console.WriteLine("Result:");
        for (int i = 0; i < response.FieldCount; i++) {
          Console.WriteLine("\t{0} - {1}", response.GetName(i), response.IsDBNull(i) ? "None" : response.GetString(i));
        }
      }
    }
    
  3. 定義要執行的命令。 命令 會建立名為 MyStormEvents 的數據表,並將數據表架構定義為數據行名稱和類型清單。 請將 <your_database> 佔位符替換為您的資料庫名稱。

    string database = "<your_database>";
    string table = "MyStormEvents";
    
    // Create a table named MyStormEvents
    // The brackets contain a list of column Name:Type pairs that defines the table schema
    string command = @$".create table {table}
                      (StartTime:datetime,
                       EndTime:datetime,
                       State:string,
                       DamageProperty:int,
                       DamageCrops:int,
                       Source:string,
                       StormSummary:dynamic)";
    
  4. 執行 命令,並使用先前定義的函式列印結果。

    注意

    您將使用 ExecuteControlCommand 方法來執行 命令。

    using (var response = kustoClient.ExecuteControlCommand(database, command, null)) {
      PrintResultsAsValueList(command, response);
    }
    

完整的程式代碼看起來應該像這樣:

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

namespace ManagementCommands {
  class ManagementCommands {
    static void Main(string[] args) {
      string clusterUri = "https://<your_cluster_uri>";
      var kcsb = new KustoConnectionStringBuilder(clusterUri)
          .WithAadUserPromptAuthentication();

      using (var kustoClient = KustoClientFactory.CreateCslAdminProvider(kcsb)) {
        string database = "<your_database>";
        string table = "MyStormEvents";

        // Create a table named MyStormEvents
        // The brackets contain a list of column Name:Type pairs that defines the table schema
        string command = @$".create table {table} 
                          (StartTime:datetime,
                           EndTime:datetime,
                           State:string,
                           DamageProperty:int,
                           DamageCrops:int,
                           Source:string,
                           StormSummary:dynamic)";

        using (var response = kustoClient.ExecuteControlCommand(database, command, null)) {
          PrintResultsAsValueList(command, response);
        }
      }
    }

    static void PrintResultsAsValueList(string command, IDataReader response) {
      while (response.Read()) {
        Console.WriteLine("\n{0}\n", new String('-', 20));
        Console.WriteLine("Command: {0}", command);
        Console.WriteLine("Result:");
        for (int i = 0; i < response.FieldCount; i++) {
          Console.WriteLine("\t{0} - {1}", response.GetName(i), response.IsDBNull(i) ? "None" : response.GetString(i));
        }
      }
    }
  }
}

執行您的應用程式

在命令提示字元中,使用下列命令來執行應用程式:

# Change directory to the folder that contains the management commands project
dotnet run .

您應該會看到類似下列的結果:

--------------------

Command: .create table MyStormEvents 
                 (StartTime:datetime,
                  EndTime:datetime,
                  State:string,
                  DamageProperty:int,
                  Source:string,
                  StormSummary:dynamic)
Result:
   TableName - MyStormEvents
   Schema - {"Name":"MyStormEvents","OrderedColumns":[{"Name":"StartTime","Type":"System.DateTime","CslType":"datetime"},{"Name":"EndTime","Type":"System.DateTime","CslType":"datetime"},{"Name":"State","Type":"System.String","CslType":"string"},{"Name":"DamageProperty","Type":"System.Int32","CslType":"int"},{"Name":"Source","Type":"System.String","CslType":"string"},{"Name":"StormSummary","Type":"System.Object","CslType":"dynamic"}]}
   DatabaseName - MyDatabaseName
   Folder - None
   DocString - None

變更數據表層級匯入批處理原則

您可以修改相關資料表的策略來自訂資料表的匯入批次處理行為。 如需詳細資訊,請參閱 匯入批次處理原則

注意

如果您未指定 PolicyObject的所有參數,則未指定的參數將會設定為 預設值。 例如,只指定 「MaximumBatchingTimeSpan」 會導致 「MaximumNumberOfItems」 和 「MaximumRawDataSizeMB」 設定為預設值。

例如,您可以使用下列命令,修改應用程式,將 擷取批處理原則 逾時值 MyStormEventsingestionBatching 變更為 30 秒:

// Reduce the default batching timeout to 30 seconds
command = @$".alter-merge table {table} policy ingestionbatching '{{ ""MaximumBatchingTimeSpan"":""00:00:30"" }}'";

using (var response = kustoClient.ExecuteControlCommand(database, command, null))
{
  PrintResultsAsValueList(command, response);
}

當您將程式代碼新增至應用程式並加以執行時,應該會看到類似下列的結果:

--------------------

Command: .alter-merge table MyStormEvents policy ingestionbatching '{ "MaximumBatchingTimeSpan":"00:00:30" }'
Result:
   PolicyName - IngestionBatchingPolicy
   EntityName - [YourDatabase].[MyStormEvents]
   Policy - {
  "MaximumBatchingTimeSpan": "00:00:30",
  "MaximumNumberOfItems": 500,
  "MaximumRawDataSizeMB": 1024
}
   ChildEntities - None
   EntityType - Table

顯示資料庫層級保留原則

您可以使用管理命令來顯示資料庫的 保留原則

例如,您可以使用下列程式代碼,將應用程式修改為 顯示資料庫的保留原則。 結果被調整為從結果中刪除兩個欄位:

// Show the database retention policy (drop some columns from the result)
command = @$".show database {database} policy retention | project-away ChildEntities, EntityType";

using (var response = kustoClient.ExecuteControlCommand(database, command, null)) {
  PrintResultsAsValueList(command, response);
}

當您將程式代碼新增至應用程式並加以執行時,應該會看到類似下列的結果:

--------------------

Command: .show database YourDatabase policy retention | project-away ChildEntities, EntityType
Result:
   PolicyName - RetentionPolicy
   EntityName - [YourDatabase]
   Policy - {
  "SoftDeletePeriod": "365.00:00:00",
  "Recoverability": "Enabled"
}

下一步