快速入門:使用 Azure 資料總管 Python 程式庫查詢資料Quickstart: Query data using the Azure Data Explorer Python library

Azure 資料總管是一項快速又可高度調整的資料探索服務,可用於處理記錄和遙測資料。Azure Data Explorer is a fast and highly scalable data exploration service for log and telemetry data. Azure 資料總管提供一個適用於 Python 的資料用戶端程式庫 (英文)。Azure Data Explorer provides a data client library for Python. 此程式庫可讓您從程式碼中查詢資料。This library enables you to query data from your code. 在本快速入門中,您要連線到「說明叢集」上的資料表,我們已設定此叢集來協助學習。In this quickstart, you connect to a table on the help cluster that we have set up to aid learning. 您接著要查詢該叢集上的資料表,並傳回結果。You then query a table on that cluster and return the results.

本快速入門也可用來作為 Azure Notebook (英文)。This quickstart is also available as an Azure Notebook.

必要條件Prerequisites

  • 屬於 Azure Active Directory (AAD) 成員的組織電子郵件帳戶An organizational email account that is a member of Azure Active Directory (AAD)

  • 安裝在您開發電腦上的 PythonPython installed on your development computer

安裝資料程式庫Install the data library

安裝 azure-kusto-dataInstall azure-kusto-data.

pip install azure-kusto-data

新增 Import 陳述式和常數Add import statements and constants

從程式庫匯入類別,以及 pandas (資料分析程式庫)。Import classes from the library, as well as pandas, a data analysis library.

from azure.kusto.data.request import KustoClient, KustoConnectionStringBuilder
from azure.kusto.data.exceptions import KustoServiceError
from azure.kusto.data.helpers import dataframe_from_result_table
import pandas as pd

為了驗證應用程式,Azure 資料總管會使用您的 AAD 租用戶識別碼。To authenticate an application, Azure Data Explorer uses your AAD tenant ID. 若要尋找您的租用戶識別碼,請使用下列 URL,並以您的網域取代 YourDomainTo find your tenant ID, use the following URL, substituting your domain for YourDomain.

https://login.windows.net/<YourDomain>/.well-known/openid-configuration/

例如,如果您的網域為 contoso.com,則 URL 會是:https://login.windows.net/contoso.com/.well-known/openid-configuration/For example, if your domain is contoso.com, the URL is: https://login.windows.net/contoso.com/.well-known/openid-configuration/. 按一下此 URL 來查看結果;第一行如下所示。Click this URL to see the results; the first line is as follows.

"authorization_endpoint":"https://login.windows.net/6babcaad-604b-40ac-a9d7-9fd97c0b779f/oauth2/authorize"

在此案例中,租用戶識別碼為 6babcaad-604b-40ac-a9d7-9fd97c0b779fThe tenant ID in this case is 6babcaad-604b-40ac-a9d7-9fd97c0b779f. 先設定 AAD_TENANT_ID 的值,然後再執行此程式碼。Set the value for AAD_TENANT_ID before running this code.

AAD_TENANT_ID = "<TenantId>"
KUSTO_CLUSTER = "https://help.kusto.windows.net/"
KUSTO_DATABASE  = "Samples"

現在來建構連接字串。Now construct the connection string. 此範例使用服務驗證來存取叢集。This example uses device authentication to access the cluster. 您也可以使用 AAD 應用程式憑證AAD 應用程式金鑰,以及 AAD 使用者和密碼You can also use AAD application certificate, AAD application key, and AAD user and password.

KCSB = KustoConnectionStringBuilder.with_aad_device_authentication(KUSTO_CLUSTER)
KCSB.authority_id = AAD_TENANT_ID

連線到 Azure 資料總管並執行查詢Connect to Azure Data Explorer and execute a query

針對叢集執行查詢,然後將輸出儲存於資料框架中。Execute a query against the cluster and store the output in a data frame. 此程式碼執行時,它會傳回類似以下的訊息:若要登入,請使用網頁瀏覽器開啟頁面 https://microsoft.com/devicelogin,並輸入代碼 F3W4VWZDM 以進行驗證When this code runs, it returns a message like the following: To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code F3W4VWZDM to authenticate. 請遵循下列步驟來登入,然後返回以執行下一個程式碼區塊。Follow the steps to sign-in, then return to run the next code block.

KUSTO_CLIENT  = KustoClient(KCSB)
KUSTO_QUERY  = "StormEvents | sort by StartTime desc | take 10"

RESPONSE = KUSTO_CLIENT.execute(KUSTO_DATABASE, KUSTO_QUERY)

在 DataFrame 中探索資料Explore data in DataFrame

當您進入登入之後,查詢會傳回結果,並將其儲存於資料框架中。After you enter a sign in, the query returns results, and they are stored in a data frame. 您可以像處理任何其他資料框架一樣來處理結果。You can work with the results like you do any other data frame.

df = dataframe_from_result_table(RESPONSE.primary_results[0])
df

您應該會看到來自 StormEvents 資料表的前十個結果。You should see the top ten results from the StormEvents table.

後續步驟Next steps