创建应用以运行管理命令

适用于:✅Azure 数据资源管理器

在本文中,学习如何:

先决条件

设置开发环境以使用 Kusto 客户端库。

运行管理命令并处理结果

在首选 IDE 或文本编辑器中,使用适合首选语言的约定创建名为“管理命令”的项目或文件。 然后,添加以下代码:

  1. 创建连接到群集的客户端应用。 将 <your_cluster_uri> 占位符值替换为你的群集名。

    注意

    对于管理命令,你将使用 CreateCslAdminProvider 客户端工厂方法。

    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”设置为默认值。

例如,可使用以下命令更改 MyStormEvents 表的 ingestionBatching 策略,从而修改应用以将引入批处理策略超时值更改为 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"
}

下一步