如何通过SDK从Azure Purview导出实体到Excel并迁移资产属性?
实现Microsoft Purview实体导出至Excel及属性迁移自动化
可以通过pyapacheatlas SDK实现Purview实体导出至Excel,同时结合SDK的查询、更新、删除能力,能自动化完成你提到的属性迁移及旧资产清理需求,具体操作如下:
一、将Purview实体导出至Excel的操作步骤
1. 配置pyapacheatlas客户端
使用服务主体认证初始化客户端,需提前准备好Purview账户信息、租户ID、客户端ID、客户端密钥:
from pyapacheatlas.auth import ServicePrincipalAuthentication from pyapacheatlas.core import PurviewClient # 服务主体认证配置 auth = ServicePrincipalAuthentication( tenant_id="你的租户ID", client_id="你的客户端ID", client_secret="你的客户端密钥" ) # 初始化Purview客户端 client = PurviewClient( account_name="你的Purview账户名", authentication=auth )
2. 查询目标实体
针对你的场景,查询旧SQL实例myAzureSQLMI_1下的所有表资产:
# 搜索旧实例的SQL表资产 search_results = client.search_entities( query="qualifiedName:mssql://myAzureSQLMI_1* AND entityType:azure_sql_table" )
3. 整理实体属性数据
提取需要迁移的属性(如名称、描述、数据所有者等),用pandas整理成结构化数据:
import pandas as pd entity_data = [] for entity in search_results: # 按需提取属性,可根据实际自定义字段调整 entity_info = { "表名": entity.get("name"), "旧限定路径": entity.get("qualifiedName"), "描述": entity.get("description"), "数据所有者": entity.get("data_owner"), "旧实体GUID": entity.get("guid") } entity_data.append(entity_info) # 转换为DataFrame df = pd.DataFrame(entity_data)
4. 导出至Excel
将整理好的数据导出到本地Excel文件:
df.to_excel("Purview旧SQL实例资产属性.xlsx", index=False)
二、自动化迁移旧实体属性至新实体并删除旧资产
针对SQL实例更名的场景,无需导出Excel也能直接在代码中完成迁移(导出Excel可作为备份),具体流程:
1. 匹配新旧实体
通过表名、数据库名,关联旧实例与新实例的对应资产:
entity_mappings = [] for old_entity in search_results: # 从旧限定路径拆分出数据库名和表名 qualified_parts = old_entity["qualifiedName"].split("/") db_name = qualified_parts[-3] table_name = qualified_parts[-1] # 搜索新实例中对应的表资产 new_entity_search = client.search_entities( query=f"qualifiedName:mssql://myAzureSQLMI_2*{db_name}*{table_name} AND entityType:azure_sql_table" ) if new_entity_search: entity_mappings.append({ "旧GUID": old_entity["guid"], "新GUID": new_entity_search[0]["guid"], "描述": old_entity.get("description"), "数据所有者": old_entity.get("data_owner") # 添加其他需要迁移的属性 })
2. 批量更新新实体属性
构造更新请求,批量将旧属性赋值给新实体:
update_entities = [] for mapping in entity_mappings: update_entity = { "guid": mapping["新GUID"], "attributes": { "description": mapping["描述"], "data_owner": mapping["数据所有者"] }, "typeName": "azure_sql_table" } update_entities.append(update_entity) # 提交更新请求 update_result = client.upload_entities(update_entities) print(f"成功更新 {len(update_result['guidAssignments'])} 个实体")
3. 批量删除旧实体
收集旧实体的GUID,批量清理旧资产:
old_guids = [mapping["旧GUID"] for mapping in entity_mappings] delete_result = client.delete_entities(guid=old_guids) print(f"成功删除 {len(delete_result['deletedEntities'])} 个旧实体")
注意事项
- 确保服务主体拥有Purview的数据管理员或足够权限(读取、更新、删除实体);
- 测试阶段可先选择1-2个表验证流程,避免误操作;
- 若有大量自定义属性,可通过
entity["attributes"]动态提取所有属性进行迁移; - 导出的Excel可作为属性备份,方便后续校验。
内容的提问来源于stack exchange,提问作者nam
相关产品推荐
相关产品推荐

