Hello Kusto:建立您的第一個應用程式
本文內容
適用於:✅Microsoft Fabric ✅Azure 數據總管
在本文中,您將瞭解如何:
建立您的第一個用戶端應用程式
使用互動式驗證
執行一個基本查詢來列印 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";
注意
針對 Node.js 應用程式,請使用 InteractiveBrowserCredentialNodeOptions
,而不是使用 InteractiveBrowserCredentialInBrowserOptions
。
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)
clientId
和 redirectUri
來自您在 設定開發環境 必要條件一節中建立的 Microsoft Entra 應用程式註冊。
const clusterUri = "https://help.kusto.windows.net";
const authOptions = {
clientId: "00001111-aaaa-2222-bbbb-3333cccc4444",
redirectUri: "http://localhost:5173",
} as InteractiveBrowserCredentialInBrowserOptions;
const kcsb = KustoConnectionStringBuilder.withUserPrompt(clusterUri, authOptions);
注意
針對 Node.js 應用程式,請使用 InteractiveBrowserCredentialNodeOptions
,而不是使用 InteractiveBrowserCredentialInBrowserOptions
。
String clusterUri = "https://help.kusto.windows.net/";
ConnectionStringBuilder kcsb = ConnectionStringBuilder.createWithUserPrompt(clusterUri);
注意
若要進行互動式驗證,您需要Microsoft帳戶或Microsoft Entra 使用者身分識別。 不需要 Azure 訂用帳戶。
在 C# 中,如果下列狀況,互動式驗證程式可能不會提示使用者:
使用者已在裝置上驗證
裝置上有現有的 Kusto.Explorer 或 Azure 數據總管 Web UI 驗證
建立客戶端物件,該物件會使用連接字串產生器對象來連線到叢集。
附註
強烈建議您快取並重複使用 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)) {
}
定義要執行的資料庫和查詢。 查詢會在名為 Welcome 的欄中列印 Hello Kusto! 。
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"));
備註
查詢輸出會在回應中傳回,作為包含一個或多個數據表的物件,其中包含一個或多個數據列和數據行。
物件的格式取決於客戶端連結庫語言。
列印 kusto 查詢會傳回具有一個數據列和數據行的單一數據表。
主要結果 JSON 物件中的回應。 物件包含表格陣列,而每個表格又包含行陣列。 每一行包含一個由欄位組成的字典形式的數據。 您可以參考結果,如下所示:
第一個陣列索引 [0]
參考第一個數據表
第二個陣列索引 [0]
參考第一個數據列
字典索引鍵 ["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: "00001111-aaaa-2222-bbbb-3333cccc4444",
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();
注意
針對 Node.js 應用程式,請使用 InteractiveBrowserCredentialNodeOptions
,而不是使用 InteractiveBrowserCredentialInBrowserOptions
。
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!
下一步