我想替换我当前使用 AAD 和应用程序密钥的 KustoClient 以使用 Synapse 托管身份,我知道有一个选项使用链接服务从 Kusto 集群读取,但我想重复使用尽可能多的代码我已经使用了用于执行“导出”命令的 KustoClient,是否有任何可用的选项使用来自 Azure Identity 的 msparkutils 凭据或 DefaultAzureCredential??
这是我现在创建 Kusto 客户端的方法:
kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication(kusto_cluster
, service_principal_id, service_principal_secret, tenant_id)
如何使用 Synapse Managed Identity 创建 Kusto 客户端
要使用
KustoClient
使用托管身份进行身份验证,您可以按照此 文档进行操作。
注意: 托管标识仅在它们在 Azure 中运行时关联的主机上有效。
您可以使用下面的代码使用托管身份向 Kusto 客户端进行身份验证。
代码:
from azure.kusto.data import KustoClient, KustoConnectionStringBuilder
from azure.kusto.data.helpers import dataframe_from_result_table
cluster = "https://xxxx.eastus.kusto.windows.net"
user_assigned_managed_identity="068768d7-cxxxxx-dd4f6a0c3189"
kcsb = KustoConnectionStringBuilder.with_aad_managed_service_identity_authentication(cluster, client_id=user_assigned_managed_identity)
client = KustoClient(kcsb)
db = "database456"
query = """
StormEvents
| summarize count() by EventType
| top 5 by count_
"""
response = client.execute(db, query)
df = dataframe_from_result_table(response.primary_results[0])
print(df)
输出:
EventType count_
0 Thunderstorm Wind 13015
1 Hail 12711
2 Flash Flood 3688
3 Drought 3616
4 Winter Weather 3349
确保您的托管身份已分配给具有适当权限的数据资源管理器。
传送门:
参考: azure - 使用 Python 中的托管身份对 Kusto 进行身份验证 - Stack Overflow,作者:Yochai Gilad。