共用方式為


快速入門:使用 Python 建立 Microsoft Purview (先前稱為 Azure Purview) 帳戶

注意事項

Microsoft Purview 資料目錄 (傳統) 和 Data Health Insights (傳統) 不再接受新客戶,而且這些服務 (先前為 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 資料對應會建立資訊的最新地圖。 它識別和分類敏感資料,並提供端對端譜系。 資料取用者能夠探索整個組織的資料,而資料管理員能夠稽核、保護和確保正確使用您的資料。

如需 Microsoft Purview 傳統治理功能的詳細資訊, 請參閱我們的治理解決方案概觀頁面

必要條件

  • 如果您沒有 Azure 訂用帳戶,請先建立 免費訂用帳戶 ,再開始。

  • 與您的訂用帳戶相關聯的 Microsoft Entra 租用戶

  • 您用來登入 Azure 的使用者帳戶必須是 參與者擁有者 角色的成員,或 Azure 訂用帳戶的 系統管理員 。 若要檢視您在訂用帳戶中擁有的權限,請遵循下列步驟:

    1. 移至 Azure 入口網站
    2. 在右上角選擇您的用戶名。
    3. 選取省略符號按鈕 (“...”) 更多選擇。
    4. 然後選取 [我的許可權]。
    5. 如果您有權存取多個訂用帳戶,請選取適當的訂用帳戶。

登入 Azure

使用您的 Azure 入口網站 登入您的 Azure 帳戶。

安裝 Python 套件

  1. 以管理員權限開啟終端機或命令提示字元。

  2. 首先,安裝 Azure 管理資源的 Python 套件:

    pip install azure-mgmt-resource
    
  3. 若要安裝適用於 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。

  4. 若要安裝 Azure 身分識別驗證的 Python 套件,請執行下列命令:

    pip install azure-identity
    

    注意事項

    「azure-identity」 套件可能與某些常見相依性上的「azure-cli」 發生衝突。 如果您遇到任何驗證問題,請移除 「azure-cli」 及其相依性,或使用乾淨的電腦而不安裝 「azure-cli」 套件。

建立 Purview 用戶端

  1. 建立名為 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
    
  2. 將下列程式碼新增至建立 PurviewManagementClient 類別實例的 Main 方法。 您將使用此物件來建立 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 帳戶

  1. 將下列程式碼新增至建立 Purview 帳戶Main 方法。 如果您的資源群組已存在,請註解掉第一個 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)      
    
  2. 現在,新增以下語句,以便在程式執行時呼叫 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 治理入口網站的存取權。