次の方法で共有


管理コマンドを実行するアプリを作成する

この記事では、次の方法について説明します。

前提条件

Kusto クライアント ライブラリを使用するように開発環境を設定します。

管理コマンドを実行して結果を処理する

任意の IDE またはテキスト エディターで、優先言語に適した規則を使用して 、管理コマンド という名前のプロジェクトまたはファイルを作成します。 次のコードを追加します。

  1. クラスターに接続するクライアント アプリを作成します。 プレースホルダーを <your_cluster_uri> クラスター名に置き換えます。

    Note

    管理コマンドの場合は、クライアント ファクトリ メソッドを使用します 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. コマンドを実行し、前に定義した関数を使用して結果を出力します。

    Note

    メソッドを 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

テーブル レベルのインジェスト バッチ処理ポリシーを変更する

対応するテーブル ポリシーを変更することで、テーブルのインジェスト バッチ処理の動作をカスタマイズできます。 詳細については、「IngestionBatching ポリシー」を参照してください。

注意

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

データベース レベルの保持ポリシーを表示する

管理コマンドを使用して、データベースの アイテム保持ポリシーを表示できます。

たとえば、次のコードを使用して、 データベースのアイテム保持ポリシーを表示 するようにアプリを変更できます。 結果は、結果から 2 つの列を投影するようにキュレーションされます。

// 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"
}

次のステップ