使用 Azure 数据资源管理器 Python 库查询数据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 article, 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 article 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-data 。Install azure-kusto-data.

pip install azure-kusto-data

添加导入语句和常量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 租户 ID,以对应用程序进行身份验证。To authenticate an application, Azure Data Explorer uses your AAD tenant ID. 要查找租户 ID,请使用以下 URL,并将域替换为 YourDomain 。To find your tenant ID, use the following URL, substituting your domain for YourDomain.

https://login.chinacloudapi.cn/<YourDomain>/.well-known/openid-configuration/

例如,如果域名为 contoso.com,则该 URL 将是:https://login.chinacloudapi.cn/contoso.com/.well-known/openid-configuration/For example, if your domain is contoso.com, the URL is: https://login.chinacloudapi.cn/contoso.com/.well-known/openid-configuration/. 单击此 URL 以查看结果;第一行如下所示。Click this URL to see the results; the first line is as follows.

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

在这种情况下,租户 ID 为 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.chinacloudapi.cn/"
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. 运行此代码时,它会返回如下消息:若要登录,请使用 Web 浏览器打开页面 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