如何通过PyApacheAtlas在Azure Purview中创建业务术语模板?
关于PyApacheAtlas创建Azure Purview术语模板的问题解答
你的代码方式不正确
你当前的代码使用AtlasEntity并指定typeName="TermTemplate",这会把对象当成普通资产实体上传,而非业务术语表的术语模板。Azure Purview的术语模板本质是自定义术语类型,需要通过PyApacheAtlas的专属类和方法来创建。
正确创建术语模板的步骤
1. 导入必要的类
from pyapacheatlas.core.glossary import GlossaryTermDef, AtlasAttributeDef from pyapacheatlas.core import PurviewClient
2. 定义模板的自定义属性
先创建模板所需的属性字段(可根据业务需求扩展):
# 定义模板的属性集合 template_attributes = [ AtlasAttributeDef( name="category", typeName="string", isOptional=True, description="术语的分类标签" ), AtlasAttributeDef( name="business_owner", typeName="string", isOptional=True, description="术语的业务负责人" ) ]
3. 创建术语模板实例
关联到目标术语表(这里用默认术语表):
# 获取默认术语表的GUID default_glossary = client.get_glossary(name="Glossary") # 创建术语模板(自定义术语类型) term_template = GlossaryTermDef( name="Term Template", description="用于创建新术语的标准模板", attributeDefs=template_attributes, glossaryGuid=default_glossary["guid"] )
4. 上传模板到Purview
使用术语表专属的上传方法,而非资产上传方法:
# 上传术语模板 uploaded_template = client.glossary.upload_term_def(term_template) print(f"模板创建成功,GUID: {uploaded_template['guid']}")
如何使用模板创建新术语
模板创建完成后,创建新术语时指定模板的GUID即可:
from pyapacheatlas.core.glossary import AtlasGlossaryTerm # 基于模板创建新术语 new_business_term = AtlasGlossaryTerm( name="客户生命周期价值", description="客户在合作周期内为企业贡献的总营收", glossaryGuid=default_glossary["guid"], termDefGuid=uploaded_template["guid"], # 指定模板GUID attributes={ "category": "业务指标", "business_owner": "张三" } ) # 上传新术语 client.glossary.upload_terms([new_business_term])
关键错误点总结
- 错误使用
AtlasEntity类:该类用于创建资产实体,而非术语表的模板/术语 - 错误调用
upload_entities方法:术语模板需要通过client.glossary.upload_term_def上传 - 对术语模板的本质理解偏差:它是自定义的术语类型,不是普通术语或资产
内容的提问来源于stack exchange,提问作者aca1803
相关产品推荐
相关产品推荐

