【Snowflake】Snowflake ~ TAG ~

■ はじめに

https://dk521123.hatenablog.com/entry/2023/08/03/000817

で紹介した、Snowflakeのタグ付けについて、
掘り下げてみる

目次

【0】Snowflake のタグ
 1)公式ドキュメント
 2)用途
【1】タグ作成
【2】使用上の注意
【3】タグを付加させる方法
 例1:クエリに付加させる場合
 例2:Pythonの場合
 例3:Javaの場合
 例4:Kafka connectの場合
【4】タグ参照方法

【0】Snowflake のタグ

* AWS のタグ付けとほぼ同じイメージ
 => 必須ではないが、タグ付けしておくと運用などで便利って感じ。

1)公式ドキュメント

https://docs.snowflake.com/ja/user-guide/object-tagging

2)用途

[1] クエリなどにタグ付けしておくと、絞り込みに便利
 => 例えば、パフォーマンステストなどでタグ付けしておければ
  タグによりクエリが絞り込め、終了日時などの算出に役に立つ
[2] タグを介してマスキングポリシーを付与
 => 以下のサイトより、用途に追加

https://dev.classmethod.jp/articles/snowflake-tag-based-masking-policy/

などなど、利用は多岐に渡るかと、、、

【1】タグ作成

https://docs.snowflake.com/ja/sql-reference/sql/create-tag

CREATE TAG <tag_name>;
CREATE TAG <tag_name> COMMENT = 'comment';

CREATE TAG test_tag COMMENT = 'For demo';

【2】使用上の注意

https://docs.snowflake.com/ja/sql-reference/sql/create-tag

この機能には、Enterprise Edition以上が必要です。

https://docs.snowflake.com/ja/sql-reference/sql/create-tag#usage-notes

Snowflakeは、アカウント内のタグの数を10,000に制限します。

【3】タグを付加させる方法

例1:クエリに付加させる場合

-- セッション単位で、Tagを付加させる
alter session set query_tag='test_tag';

insert into users
from users_tmp
;

例2:Pythonの場合

con = snowflake.connector.connect(
    user='XXXX',
    password='XXXX',
    account='XXXX',
    session_parameters={
        'QUERY_TAG': 'test_tag', # ★ここ
    }
)

例3:Javaの場合

  private static Connection getConnection() throws SQLException {

    // build connection properties
    Properties properties = new Properties();
    properties.put("user", "user");
    properties.put("password", "password");
    properties.put("warehouse", "test_wh");
    properties.put("db", "test_db");
    properties.put("schema", "test_schema");
    properties.put("query_tag", "test_tag"); // ★

    String connectStr = "jdbc:snowflake://<account_identifier>.snowflakecomputing.com";
    return DriverManager.getConnection(connectStr, properties);
  }

例4:Kafka connectの場合

https://dk521123.hatenablog.com/entry/2023/06/04/230737

  # Connector Conguration
  connector_configuration = {
    "name" = "demo-msk-connect"
    "connector.class" = "com.github.jcustenborder.kafka.connect.simulator.SimulatorSinkConnector"
    "tasks.max"       = "3"
    "topics"          = "demo-topic"
    "connection.query_tag" = "test_tag", # ★
    "connection.url" = "$${secretManager:demo-msk-connect-credentials:db_url}",

【4】タグ参照方法

* タグを設定した場合、QUERY_TAG ビューで絞り込むことができる

https://docs.snowflake.com/ja/sql-reference/account-usage/query_history

SELECT
  QUERY_ID,
  QUERY_TEXT,
  START_TIME,
  END_TIME,
  QUERY_TAG
FROM
  QUERY_HISTORY
WHERE
  QUERY_TAG = 'test_tag'
;

関連記事

Snowflake ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/02/130111
Snowflake ~ 入門編 / Hello world
https://dk521123.hatenablog.com/entry/2021/11/22/212520
Snowflake ~ 統計情報 ~
https://dk521123.hatenablog.com/entry/2023/08/03/000817