注意
Microsoft Purview 数据目录 (经典) 和数据运行状况见解 (经典) 不再接受新客户,这些服务(以前为 Azure Purview)现在处于客户支持模式。
重要
每个租户只能创建一个 Microsoft Purview 帐户。 如果你的组织已有一个 Microsoft Purview 帐户,则你将无法创建新的 Microsoft Purview 帐户,除非你的组织已经拥有多个帐户并且仍低于 预先存在的配额。 有关详细信息,请参阅 常见问题解答。
在本快速入门中,你将使用 Python 以编程方式创建一个 Microsoft Purview (以前是 Azure Purview) 帐户。 提供了适用于 Microsoft Purview 的 Python 参考,但本文将指导你完成使用 Python 创建帐户所需的所有步骤。
Microsoft Purview 治理门户会显示Microsoft Purview 数据映射和Microsoft Purview 数据目录等工具,可帮助你管理和治理数据环境。 通过跨本地、多云和软件即服务 (SaaS) 源连接到数据,Microsoft Purview 数据映射可创建信息的最新地图。 它标识和分类敏感数据,并提供端到端 linage。 数据使用者能够发现整个组织的数据,数据管理员能够审核、保护并确保正确使用数据。
有关 Microsoft Purview 的经典治理功能 的详细信息,请参阅我们的治理解决方案概述页。
先决条件
如果没有 Azure 订阅,请在开始之前创建一个 免费订阅 。
与订阅关联的Microsoft Entra租户。
用于登录到 Azure 的用户帐户必须是参与者或所有者角色的成员,或者是 Azure 订阅的管理员。 若要查看订阅中拥有的权限,请执行以下步骤:
- 转到Azure 门户
- 选择右上角的用户名。
- 选择省略号按钮 (“...”) 以获取更多选项。
- 然后选择“ 我的权限”。
- 如果有权访问多个订阅,请选择相应的订阅。
登录到 Azure
使用 Azure 帐户登录到Azure 门户。
安装 Python 包
使用管理员权限打开终端或命令提示符。
首先,为 Azure 管理资源安装 Python 包:
pip install azure-mgmt-resource若要安装适用于 Microsoft Purview 的 Python 包,请运行以下命令:
pip install azure-mgmt-purview适用于 Microsoft Purview 的 Python SDK 支持 Python 2.7、3.3、3.4、3.5、3.6 和 3.7。
若要安装用于 Azure 标识身份验证的 Python 包,请运行以下命令:
pip install azure-identity注意
“azure-identity”包在某些常见依赖项上可能与“azure-cli”冲突。 如果遇到任何身份验证问题,请删除“azure-cli”及其依赖项,或使用干净的计算机而不安装“azure-cli”包。
创建 purview 客户端
创建名为 purview.py 的文件。 添加以下语句以添加对命名空间的引用。
from azure.identity import ClientSecretCredential from azure.mgmt.resource import ResourceManagementClient from azure.mgmt.purview import PurviewManagementClient from azure.mgmt.purview.models import * from datetime import datetime, timedelta import time将以下代码添加到 Main 方法,以创建 PurviewManagementClient 类的实例。 你将使用此对象创建 purview 帐户、删除 purview 帐户、检查名称可用性和其他资源提供程序作。
def main(): # Azure subscription ID subscription_id = '<subscription ID>' # This program creates this resource group. If it's an existing resource group, comment out the code that creates the resource group rg_name = '<resource group>' # The purview name. It must be globally unique. purview_name = '<purview account name>' # Location name, where Microsoft Purview account must be created. location = '<location name>' # Specify your Active Directory client ID, client secret, and tenant ID credentials = ClientSecretCredential(client_id='<service principal ID>', client_secret='<service principal key>', tenant_id='<tenant ID>') # resource_client = ResourceManagementClient(credentials, subscription_id) purview_client = PurviewManagementClient(credentials, subscription_id)
创建 purview 帐户
将以下代码添加到 Main 方法,以创建 purview 帐户。 如果资源组已存在,请注释掉第一个
create_or_update语句。# create the resource group # comment out if the resource group already exits resource_client.resource_groups.create_or_update(rg_name, rg_params) #Create a purview identity = Identity(type= "SystemAssigned") sku = AccountSku(name= 'Standard', capacity= 4) purview_resource = Account(identity=identity,sku=sku,location =location ) try: pa = (purview_client.accounts.begin_create_or_update(rg_name, purview_name, purview_resource)).result() print("location:", pa.location, " Microsoft Purview Account Name: ", pa.name, " Id: " , pa.id ," tags: " , pa.tags) except: print("Error") print_item(pa) while (getattr(pa,'provisioning_state')) != "Succeeded" : pa = (purview_client.accounts.get(rg_name, purview_name)) print(getattr(pa,'provisioning_state')) if getattr(pa,'provisioning_state') == "Failed" : print("Error in creating Microsoft Purview account") break time.sleep(30)现在,添加以下语句以在程序运行时调用 main 方法:
# Start the main method main()
完整脚本
下面是完整的 Python 代码:
from azure.identity import ClientSecretCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.purview import PurviewManagementClient
from azure.mgmt.purview.models import *
from datetime import datetime, timedelta
import time
def main():
# Azure subscription ID
subscription_id = '<subscription ID>'
# This program creates this resource group. If it's an existing resource group, comment out the code that creates the resource group
rg_name = '<resource group>'
# The purview name. It must be globally unique.
purview_name = '<purview account name>'
# Location for your resource group and your Microsoft Purview account.
location ="<location>"
# Specify your Active Directory client ID, client secret, and tenant ID
credentials = ClientSecretCredential(client_id='<service principal ID>', client_secret='<service principal key>', tenant_id='<tenant ID>')
resource_client = ResourceManagementClient(credentials, subscription_id)
purview_client = PurviewManagementClient(credentials, subscription_id)
# create the resource group
# comment out if the resource group already exits
resource_client.resource_groups.create_or_update(rg_name, {"location": location})
#Create a purview
identity = Identity(type= "SystemAssigned")
sku = AccountSku(name= 'Standard', capacity= 4)
purview_resource = Account(identity=identity,sku=sku,location =location)
try:
pa = (purview_client.accounts.begin_create_or_update(rg_name, purview_name, purview_resource)).result()
print("location:", pa.location, " Microsoft Purview Account Name: ", purview_name, " Id: " , pa.id ," tags: " , pa.tags)
except:
print("Error in submitting job to create account")
print_item(pa)
while (getattr(pa,'provisioning_state')) != "Succeeded" :
pa = (purview_client.accounts.get(rg_name, purview_name))
print(getattr(pa,'provisioning_state'))
if getattr(pa,'provisioning_state') == "Failed" :
print("Error in creating Microsoft Purview account")
break
time.sleep(30)
# Start the main method
main()
运行代码
生成并启动应用程序。 控制台将打印Microsoft Purview 帐户的创建进度。 等到完成。 下面是示例输出:
location: southcentralus Microsoft Purview Account Name: purviewpython7 Id: /subscriptions/8c2c7b23-848d-40fe-b817-690d79ad9dfd/resourceGroups/Demo_Catalog/providers/Microsoft.Purview/accounts/purviewpython7 tags: None
Creating
Creating
Succeeded
验证输出
转到Azure 门户中的“Microsoft Purview 帐户”页,并验证使用上述代码创建的帐户。
删除Microsoft Purview 帐户
若要删除 purview 帐户,请将以下代码添加到程序,然后运行:
pa = purview_client.accounts.begin_delete(rg_name, purview_name).result()
后续步骤
在本快速入门中,你了解了如何创建Microsoft Purview (以前为 Azure Purview) 帐户、删除该帐户以及检查以获取名称可用性。 现在可以下载 Python SDK 并了解可为 Microsoft Purview 帐户执行的其他资源提供程序作。
按照以下后续文章作,了解如何导航 Microsoft Purview 治理门户、创建集合以及授予对 Microsoft Purview 治理门户的访问权限。