Python으로 태그 관리하기

Python을 사용하여 Snowflake에서 태그를 관리할 수 있습니다. 태그는 다른 Snowflake 오브젝트에 할당할 수 있는 스키마 수준 오브젝트입니다. 사용자는 태그를 Snowflake 오브젝트에 할당할 때 태그를 임의의 문자열 값과 연결합니다. 태그를 할당할 때 태그를 임의의 문자열 값과 연결하면 Snowflake가 태그와 해당 문자열 값을 키-값 페어로 저장합니다. 태그를 정의하고 할당하면 태그를 쿼리하여 오브젝트의 사용량을 모니터링하고 감사 및 보고와 같은 데이터 거버넌스 작업을 용이하게 할 수 있습니다.

태그에 대한 자세한 내용은 오브젝트 태그 소개 섹션을 참조하세요.

Snowflake Python APIs 은 다음 유형의 태그를 나타냅니다.

  • Tag: 해당 이름, 저장된 데이터베이스 및 스키마, 생성 시점과 같은 속성이 있는 태그 오브젝트 모델을 나타냅니다.

  • TagValue: 태그의 값을 나타냅니다.

  • TagResource: 태그 오브젝트에 대한 정보를 가져오고 태그 오브젝트에 대한 작업(예: 태그 이름 바꾸기 및 태그 삭제)을 수행하는 데 사용할 수 있는 태그 오브젝트에 대한 참조를 나타냅니다.

전제 조건

이 항목의 예제에서는 Snowflake와 연결하고 Snowflake Python APIs 을 사용할 수 있는 Root 오브젝트를 생성하는 코드를 추가했다고 가정합니다.

예를 들어, 다음 코드는 구성 파일에 정의된 연결 매개 변수를 사용하여 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_tagcustom_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