【问题标题】:Error Python API GCP Data Catalog - Google Cloud Platform错误 Python API GCP 数据目录 - Google Cloud Platform
【发布时间】:2021-05-21 21:09:31
【问题描述】:

我在尝试使用此链接的代码时出错:Data Catalog Example。在第 4 步,只需复制粘贴提供的所有代码,在我的 GCP 项目中进行身份验证并对其进行测试。

一切正常,直到它开始创建标签模板字段...

tag_template = datacatalog_v1.types.TagTemplate()
tag_template.display_name = 'On-premises Tag Template'

tag_template.fields['source'].display_name = 'Source of the data asset'
tag_template.fields['source'].type.primitive_type = \
    datacatalog_v1.FieldType.PrimitiveType.STRING.value

它总是因同样的错误而崩溃。

Error Image

tag_template <proto.marshal.collections.maps.MapComposite object at 0x10fe23310>
Traceback (most recent call last):
  File "/Users/ac/Documents/DataCatalog/python_datacatalog/application/sample.py", line 149, in <module>
    tag_template.fields['source'].display_name = 'Source of the data asset'
  File "/Users/ac/Documents/DataCatalog/python_datacatalog/venv/lib/python3.8/site-packages/proto/marshal/collections/maps.py", line 56, in __getitem__
    raise KeyError(key)
KeyError: 'source'

有人可以帮我分享替代方法吗?

【问题讨论】:

  • 发布您的错误的屏幕截图会使任何有相同错误的人很难找到此帖子。请在此问题中发布您的错误文本。
  • @blindguy 感谢您的观察。

标签: python api google-cloud-platform google-data-catalog


【解决方案1】:

Data Catalog Example 上的示例代码已过时。从第 4 步开始对代码进行了一些更改(您当前卡住的地方)。我在原始类型的下一行遇到了另一个错误。

# -------------------------------
# 4. Create a Tag Template.
# -------------------------------
tag_template = datacatalog_v1.types.TagTemplate()
tag_template.display_name = 'On-premises Tag Template'

tag_template.fields['source'] = datacatalog_v1.types.TagTemplateField() #creates key 'source'
tag_template.fields['source'].display_name = 'Source of the data asset'
tag_template.fields['source'].type_.primitive_type = datacatalog_v1.types.FieldType.PrimitiveType.STRING #from type -> type_, syntax for primitive type string
  • 修复是通过添加tag_template.fields['source'] = datacatalog_v1.types.TagTemplateField() 为 TagTemplateField 创建键“源”
  • 更新了"tag_template.type" 的语法并分配了primitive string value

如果继续第 5 步,则会弹出错误KeyError: 'source'。如果您没有遇到这种情况,那一切都很好。但是,如果您在此处遇到它,则可以通过代码来解决此问题。

# -------------------------------
# 5. Attach a Tag to the custom Entry.
# -------------------------------
tag = datacatalog_v1.types.Tag()
tag.template = tag_template.name
tag.fields['source'] = datacatalog_v1.types.TagField() #creates key 'source'
tag.fields['source'].string_value = 'On-premises system name'

tag = datacatalog.create_tag(parent=entry.name, tag=tag)
print('Created tag: {}'.format(tag.name))
  • 修复类似于第 4 步,这次是通过添加 tag.fields['source'] = datacatalog_v1.types.TagField() #creates key 'source' 为 TagField 创建密钥“源”

我从第 1 步到第 5 步运行了整个脚本。

脚本的输出:

创建的标签模板:

创建的标签:

【讨论】:

  • 这非常有效。谢谢,你的第 5 步是正确的,你的解决方案是正确的。
猜你喜欢
  • 2016-02-25
  • 2021-12-04
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 2016-04-08
  • 1970-01-01
相关资源
最近更新 更多