Pythonでのタグの管理

Pythonを使用して、Snowflakeでタグを管理できます。タグは、別のSnowflakeオブジェクトに割り当てることができるスキーマレベルのオブジェクトです。タグを割り当てるときに、タグを任意の文字列値に関連付け、Snowflakeはタグとその文字列値をキーと値のペアとして保存します。タグを定義して割り当てた後、タグをクエリしてオブジェクトの使用状況をモニターし、監査やレポートなどのデータガバナンス操作を容易にすることができます。

タグの詳細については、:doc:`/user-guide/object-tagging/introduction`をご参照ください。

|sf-python|は、次のタイプでタグを表します。

  • Tag:タグオブジェクトモデルを表し、名前、格納されているデータベース、スキーマ、作成日などのプロパティを持ちます。

  • TagValue:タグの値を表します。

  • TagResource:タグオブジェクトに関する情報を取得し、タグの名前変更やタグのドロップなど、タグオブジェクトに対して操作を実行するために使用できるタグオブジェクトへの参照を表します。

前提条件

このトピックの例では、Snowflakeと接続するコードを追加して Root オブジェクトを作成し、そこからSnowflake Python Snowflake Python APIs を使用することを想定しています。

たとえば、以下のコードでは、構成ファイルで定義された接続パラメーターを使用してSnowflakeへの接続を作成します。

from snowflake.core import Root
from snowflake.snowpark import Session

session = Session.builder.config("connection_name", "myconnection").create()
root = Root(session)
Copy

出来上がった Session オブジェクトを使って、コードは API のタイプとメソッドを使う Root オブジェクトを作成します。詳細については、 Snowflake Python APIs によるSnowflakeへの接続 をご参照ください。

テーブル用のタグの作成と管理

次のコード例では、``environment_tag``および``custom_tag``という名前のタグを作成して``my_tagged_table``という名前のテーブルに割り当て、テーブルのタグ割り当てを取得します。その後、テーブルから``environment_tag``タグの設定を解除し、タグの割り当てを再度取得します。

from snowflake.core.tag import Tag, TagValue

table = root.databases["my_database"].schemas["my_schema"].tables["my_tagged_table"]
tags = root.databases["tag_database"].schemas["tag_schema"].tags

# Create tags
environment_tag = Tag(
    name="environment_tag",
    allowed_values=["prod", "dev", "staging"],
    comment="Tag to classify environment",
)
custom_tag = Tag(
    name="custom_tag",
)
environment_tag_resource = tags.create(environment_tag, mode="ifNotExists")
custom_tag_resource = tags.create(custom_tag, mode="ifNotExists")

# Set tags on a table
table.set_tags({environment_tag_resource: TagValue(value="prod"), custom_tag_resource: TagValue(value="custom value")})

# Fetch tag assignments for the table
fetched_tags = table.get_tags()
print(f"Tags on table: {fetched_tags}")
# Tags on table: {<TagResource: 'TAG_DATABASE.TAG_SCHEMA.CUSTOM_TAG'>: TagValue(value='custom value', level='TABLE'), <TagResource: 'TAG_DATABASE.TAG_SCHEMA.ENVIRONMENT_TAG'>: TagValue(value='prod', level='TABLE')}

# Unset one of the tags from the table
table.unset_tags({environment_tag_resource})

# Fetch tag assignments again
fetched_tags_after_unset = table.get_tags()
print(f"Tags after unset: {fetched_tags_after_unset}")
# Tags after unset: {<TagResource: 'TAG_DATABASE.TAG_SCHEMA.CUSTOM_TAG'>: TagValue(value='custom value', level='TABLE')}
Copy

継承によるスキーマとテーブルのタグの管理

次のコード例では、``environment_tag``という名前の既存のタグを``my_schema``という名前のスキーマへ割り当て、そのスキーマの``another_tagged_table``という名前のテーブルに対するタグ割り当てを取得します。次に、継承関係を含めて(with_lineage=True)、そのテーブルに対するタグ割り当てを取得します。

from snowflake.core.tag import TagValue

schema = root.databases["my_database"].schemas["my_schema"]
table = schema.tables["another_tagged_table"]
environment_tag = root.databases["tag_database"].schemas["tag_schema"].tags["environment_tag"]

# Set tag on a schema
schema.set_tags({environment_tag: TagValue(value="prod")})

# Fetch tag assignments for the table
fetched_tags = table.get_tags()
print(f"Tags on table: {fetched_tags}")
# Tags on table: {}

# Fetch tag assignments for the table with inheritance
fetched_tags_with_lineage = table.get_tags(with_lineage=True)
print(f"Tags including inheritance: {fetched_tags_with_lineage}")
# Tags including inheritance: {<TagResource: 'TAG_DATABASE.TAG_SCHEMA.ENVIRONMENT_TAG'>: TagValue(value='prod', level='SCHEMA')}
Copy