Hello Kusto: 最初のアプリを作成する
[アーティクル] 12/08/2023
3 人の共同作成者
フィードバック
この記事の内容
この記事では、次の方法について説明します。
最初のクライアント アプリを作成する
対話型認証の使用
Hello Kusto を 出力する基本的なクエリを実行します。
前提条件
Kusto クライアント ライブラリを使用するように開発環境を設定 します。
アプリを作成する
任意の IDE またはテキスト エディターで、任意の言語に適した規則を使用して 、hello kusto という名前のプロジェクトまたはファイルを作成します。 次のコードを追加します。
Kusto クライアント クラスと文字列ビルダー クラスを追加します。
using Kusto.Data;
using Kusto.Data.Net.Client;
from azure.kusto.data import KustoClient, KustoConnectionStringBuilder
import { Client as KustoClient, KustoConnectionStringBuilder } from "azure-kusto-data";
import { InteractiveBrowserCredentialInBrowserOptions } from "@azure/identity";
Note
Node.js アプリの場合は、 のInteractiveBrowserCredentialInBrowserOptions
代わりに を使用InteractiveBrowserCredentialNodeOptions
します。
import com.microsoft.azure.kusto.data.Client;
import com.microsoft.azure.kusto.data.ClientFactory;
import com.microsoft.azure.kusto.data.KustoOperationResult;
import com.microsoft.azure.kusto.data.KustoResultSetTable;
import com.microsoft.azure.kusto.data.auth.ConnectionStringBuilder;
という名前 main
の空の関数を定義し、それを呼び出します。
namespace HelloKusto {
class HelloKusto {
static void Main(string[] args) {
}
}
}
def main():
if __name__ == "__main__":
main()
async function main()
{
}
main();
public class HelloKusto
{
public static void main(String[] args) throws Exception {
try {
}
}
}
クラスター URI を定義し、認証モードを対話型に設定する接続文字列 ビルダー オブジェクトを作成します。 クラスター URI の詳細については、「 Kusto 接続文字列 」を参照してください。
var clusterUri = "https://help.kusto.windows.net/";
var kcsb = new KustoConnectionStringBuilder(clusterUri).WithAadUserPromptAuthentication();
cluster_uri = "https://help.kusto.windows.net"
kcsb = KustoConnectionStringBuilder.with_interactive_login(cluster_uri)
と redirectUri
はclientId
、「開発環境のセットアップ 」の「前提条件 」セクションで作成したMicrosoft Entraアプリの登録から取得したものです。
const clusterUri = "https://help.kusto.windows.net";
const authOptions = {
clientId: "5e39af3b-ba50-4255-b547-81abfb507c58",
redirectUri: "http://localhost:5173",
} as InteractiveBrowserCredentialInBrowserOptions;
const kcsb = KustoConnectionStringBuilder.withUserPrompt(clusterUri, authOptions);
注意
Node.js アプリの場合は、 のInteractiveBrowserCredentialInBrowserOptions
代わりに を使用InteractiveBrowserCredentialNodeOptions
します。
String clusterUri = "https://help.kusto.windows.net/";
ConnectionStringBuilder kcsb = ConnectionStringBuilder.createWithUserPrompt(clusterUri);
注意
対話型認証には、Microsoft アカウントまたはMicrosoft Entraユーザー ID が必要です。 Azure サブスクリプションは不要です。
C# では、次の場合、対話型認証プロセスでユーザーにプロンプトが表示されないことがあります。
ユーザーはデバイスで既に認証されています
既存の Kusto があります。デバイスでの Web UI 認証エクスプローラーエクスプローラーまたは Azure Date
接続文字列 ビルダー オブジェクトを使用してクラスターに接続するクライアント オブジェクトを作成します。
注意
Kusto クライアント インスタンスをキャッシュして再利用することを強くお勧めします。 Kusto クライアントを頻繁に再作成すると、アプリケーションのパフォーマンスが低下し、クラスターの負荷が増加する可能性があります。
using (var kustoClient = KustoClientFactory.CreateCslQueryProvider(kcsb)) {
}
with KustoClient(kcsb) as kusto_client:
const kustoClient = new KustoClient(kcsb);
try (Client kustoClient = ClientFactory.createClient(kcsb)) {
}
実行するデータベースとクエリを定義します。 このクエリは、 Hello Kusto! を Welcome という名前の列に出力します。
var database = "Samples";
var query = "print Welcome='Hello Kusto!'";
database = "Samples"
query = "print Welcome='Hello Kusto!'"
const database = "Samples";
const query = "print Welcome='Hello Kusto!'";
String database = "Samples";
String query = "print Welcome='Hello Kusto!'";
クエリを実行し、結果を出力します。
using (var response = kustoClient.ExecuteQuery(database, query, null)) {
response.Read();
int columnNo = response.GetOrdinal("Welcome");
Console.WriteLine(response.GetString(columnNo));
}
response = kusto_client.execute(database, query)
print(response.primary_results[0][0]["Welcome"])
const response = await kustoClient.execute(database, query);
console.log(response.primaryResults[0][0]["Welcome"].toString());
KustoOperationResult response = kustoClient.execute(database, query);
KustoResultSetTable primary_results = response.getPrimaryResults();
primary_results.next();
System.out.println(primary_results.getString("Welcome"));
Note
クエリ出力は、1 つ以上の行と列で構成される 1 つ以上のテーブルを含むオブジェクトとして応答で返されます。
オブジェクトの形式は、クライアント ライブラリの言語によって異なります。
印刷 kusto クエリは、1 つの行と列を持つ 1 つのテーブルを返します。
プライマリ結果の JSON オブジェクト内の応答。 オブジェクトにはテーブルの配列が含まれており、その配列には行の配列が含まれます。 各行には、列のディクショナリに編成されたデータが含まれています。 結果は次のように参照できます。
最初の配列インデックス [0]
は、最初のテーブルを参照します
2 番目の配列インデックス [0]
は、最初の行を参照します
辞書キー ["Welcome"]
は Welcome 列を参照します
応答は KustoOperationResult オブジェクトです。 結果は次のように参照できます。
getPrimaryResults() メソッドを使用して主な結果テーブルを取得する
最初の行を読み取る next() メソッド
最初の列の値を取得する getString() メソッド
完全なコードは次のようになります。
using Kusto.Data;
using Kusto.Data.Net.Client;
namespace HelloKusto {
class HelloKusto {
static void Main(string[] args) {
string clusterUri = "https://help.kusto.windows.net/";
var kcsb = new KustoConnectionStringBuilder(clusterUri).WithAadUserPromptAuthentication();
using (var kustoClient = KustoClientFactory.CreateCslQueryProvider(kcsb)) {
string database = "Samples";
string query = "print Welcome='Hello Kusto!'";
using (var response = kustoClient.ExecuteQuery(database, query, null)) {
response.Read();
int columnNo = response.GetOrdinal("Welcome");
Console.WriteLine(response.GetString(columnNo));
}
}
}
}
}
from azure.kusto.data import KustoClient, KustoConnectionStringBuilder
def main():
cluster_uri = "https://help.kusto.windows.net"
kcsb = KustoConnectionStringBuilder.with_interactive_login(cluster_uri)
with KustoClient(kcsb) as kusto_client:
database = "Samples"
query = "print Welcome='Hello Kusto!'"
response = kusto_client.execute(database, query)
print(response.primary_results[0][0]["Welcome"])
if __name__ == "__main__":
main()
import { Client as KustoClient, KustoConnectionStringBuilder } from "azure-kusto-data/";
import { InteractiveBrowserCredentialInBrowserOptions } from "@azure/identity";
async function main()
{
const clusterUri = "https://help.kusto.windows.net";
const authOptions = {
clientId: "5e39af3b-ba50-4255-b547-81abfb507c58",
redirectUri: "http://localhost:5173",
} as InteractiveBrowserCredentialInBrowserOptions;
const kcsb = KustoConnectionStringBuilder.withUserPrompt(clusterUri, authOptions);
const kustoClient = new KustoClient(kcsb);
const database = "Samples";
const query = "print Welcome='Hello Kusto!'";
const response = await kustoClient.execute(database, query);
console.log(response.primaryResults[0][0]["Welcome"].toString());
}
main();
Note
Node.js アプリの場合は、 のInteractiveBrowserCredentialInBrowserOptions
代わりに を使用InteractiveBrowserCredentialNodeOptions
します。
import com.microsoft.azure.kusto.data.Client;
import com.microsoft.azure.kusto.data.ClientFactory;
import com.microsoft.azure.kusto.data.KustoOperationResult;
import com.microsoft.azure.kusto.data.KustoResultSetTable;
import com.microsoft.azure.kusto.data.auth.ConnectionStringBuilder;
public class HelloKusto {
public static void main(String[] args) throws Exception {
try {
String clusterUri = "https://help.kusto.windows.net/";
ConnectionStringBuilder kcsb = ConnectionStringBuilder.createWithUserPrompt(clusterUri);
try (Client kustoClient = ClientFactory.createClient(kcsb)) {
String database = "Samples";
String query = "print Welcome='Hello Kusto!'";
KustoOperationResult response = kustoClient.execute(database, query);
KustoResultSetTable primaryResults = response.getPrimaryResults();
primaryResults.next();
System.out.println(primaryResults.getString("Welcome"));
}
}
}
}
アプリの実行
コマンド シェルで、次のコマンドを使用してアプリを実行します。
# Change directory to the folder that contains the hello world project
dotnet run .
Node.js 環境では、次の手順を実行します。
node hello-kusto.js
ブラウザー環境で、適切なコマンドを使用してアプリを実行します。 たとえば、Vite-Reactの場合:
npm run dev
mvn install exec:java -Dexec.mainClass="<groupId>.HelloKusto"
次のような結果が表示されます。
Hello Kusto!
次のステップ