Skip to main content

dbt Project on Snowflake

Snowflake に dbt プロジェクトを保存できるようになりました。dbt プロジェクトとは、dbt_project.ymlprofiles.yml、そしてモデルなどのアセットファイルを含む関連ファイルのセットです。
Snowflake では、dbt プロジェクトをスキーマオブジェクトとして保存できます。1 つの dbt プロジェクトに属するファイルはグループ化され、1 つのユニット(スキーマレベルオブジェクト)として扱われます。この機能は「dbt Project on Snowflake」と呼ばれます。

Snowflake で dbt プロジェクトを作成する方法

ステップ 1: ソースファイルをアップロードする

dbt プロジェクトのソースファイルは、次のいずれかの場所に配置できます。

  • 内部の名前付きステージ
  • Git リポジトリステージ
  • 既存の dbt プロジェクトオブジェクト
  • Snowflake 上の dbt 用ワークスペース

上記の場所に関する追加情報

Git リポジトリステージ:
Snowflake は、リモート Git リポジトリのクローン作成をサポートしています。Snowflake の Git リポジトリクローンは、ブランチ、タグ、コミットを含むリモートリポジトリの完全なクローンを持つローカル Git リポジトリとして機能します。

Snowflake 上のワークスペース:
Snowflake には、ローカルワークスペースとリモート Git リポジトリ統合ワークスペースの 2 種類のプライベートワークスペースがあります。

  • ローカルワークスペースでは、すべてのファイルは Snowflake のローカルに保存されます。ファイルは Snowflake 内にのみ保存されます。
  • リモート Git リポジトリ統合ワークスペースでは、ワークスペースは基本的にリモートリポジトリのクローンを持つエディターになります。ファイルはリモート Git リポジトリと Snowflake の両方に保存されます。 (Git 統合ワークスペースと Git リポジトリステージの違いは、前者にはエディターがあることです。)Git 統合ワークスペースには、フェッチ、プッシュなどの Git 機能を有効にする UI コメントがあります。

リモート Git リポジトリへの接続

以下のリモート Git リポジトリに接続するには、Snowflake のオブジェクト/設定が必要です。

  • API 統合
  • リポジトリがプライベートの場合は、シークレット
  • ネットワークルール
  • ネットワークルールを参照する外部アクセス統合

ステップ 2: dbt プロジェクトのデプロイ(作成)

ソースファイルを Snowflake に コピー した後、スキーマ内に dbt プロジェクトとしてデプロイできます。「デプロイ」とは、ソースファイルを Snowflake スキーマに(単一のユニットとして)保存することを意味します。dbt プロジェクトを実行することを意味するものではありません。

ソースファイルが Snowflake ワークスペースにある場合の dbt プロジェクトのデプロイ例。

(Snowflake ワークスペースには、デプロイを可能にする UI コンポーネントがあります。)

CREATE DBT PROJECT some_db.xyz_schema.my_dbt_project
FROM 'snow://workspace/user$.public."My DBT Project"/versions/live/'

dbt プロジェクトがデプロイされるたびに、Snowflake は Version$1 から始まるバージョン仕様を作成します。dbt プロジェクトが変更されるたびに、バージョンは 1 ずつ更新されます。

dbt プロジェクトの実行

dbt プロジェクトがデプロイされると(スキーマに保存されると)、実行できるようになります。UI、SQL ステートメントを使用して実行できます。

EXECUTE DBT PROJECT my_database.my_schema.my_dbt_project args='run --target prod';

上記のコマンドを Snowflake のプロシージャとタスクでラップすることもできます。

自動更新なし

ワークスペースまたは Git リポジトリでソースコードを編集しても、変更は dbt プロジェクトには反映されません。(dbt project on Snowflakeは、特定のスキーマにアタッチされたソースファイルのコピーだけです)。変更を dbt プロジェクトに適用するには、再度デプロイする必要があります(これにより、dbt プロジェクトの新しいバージョンが作成されます)。

Snowflake における dbt プロジェクトのメリットとデメリット

Snowflakeにおけるdbtプロジェクトの明確なメリットを見つけるのは困難です。潜在的なメリットは1つだけあります。

セキュリティ:dbtプロジェクトのソースコードを、実際のデータが保存されているSnowflake内に保存する方が安全だと言えるでしょう。 この場合、ソースファイルはローカルワークスペースで編集する必要があります。共同作業が必要な場合は、共有ワークスペースを使用する必要があります。

しかし、(Microsoftの) GitHubはソースコードの保存において同等の高いセキュリティを提供します。さらに、Snowflakeのローカルワークスペースを使用すると、エディター拡張機能やsqlfluffなどのコマンドラインツールを使用できなくなります。

タスクを使用してdbtプロジェクトの実行をSnowflake上の他のアクションとスケジュールして組み合わせることはメリットと考えられるかもしれませんが、必ずしも他に類を見ないものです。ほGitHub Actions等は、スケジュール実行をサポートする CI/CD 機能が備わっています。そのため、これは明確な利点とは言えません。ストアド プロシージャなどの他のアクションの実行と dbt プロジェクトの実行を組み合わせるには、dbt の on-run-start フックと on-run-end フックを使用することもできます。
さらに、Snowflake 上の dbt プロジェクトでは(ローカル ワークスペース、Git 統合ワークスペース、Git リポジトリ ステージのいずれを使用していても)、ソース ファイルが変更されると、dbt プロジェクトを(再)デプロイする必要があります。

結論

データが存在する場所にソースコードを保存するなど、ニーズがあれば、dbt Project on Snowflakeが唯一の選択肢と思います。その他の場合は、Snowflake 上の dbt プロジェクトは、既存のソリューションに対して明確な利点を提供しません。

Comments

Popular posts from this blog

Introduction to SQLFluff: How to make your SQL code clean and error-free

Image by Jake Aldridge from Pixabay You know oftentimes, the cause of runtime or compile errors and hours of debugging agony is all due to simply a missing semicolon. Have you ever had such experience? If you had, you are not alone. There are two ways to avoid these unfortunate situations: either become a perfect developer who never makes mistakes, or use helpful tools such as linters that can catch these errors early on. I am nowhere near being a perfect developer who never makes a mistake. In fact, I'm probably the opposite of a perfect developer, so even if I wanted to, I wouldn’t be able to teach you how to become a perfect developer. But what I can teach you is using linters. A Wikipedia defines a linter as a "static code analysis tool used to flag programming errors, bugs, stylistic errors and suspicious constructs." If you're not convinced yet on using linters, consider this scenario: in a large project with multiple members, different people tend to ...

Git squash merge explained

There are many ways to integrate changes in git: regular / normal git merge, git squash merge, git rebase etc. This article explains git squash merge by comparing it to regular merge. Let’s use below example: In the repository with default main branch, after two commits, a new feature branch is created. Some work happened in feature branch. feature branch now has 2 commits that it shares with main branch, and three exclusive commits (exists only in feature branch). In the meantime, others worked on main branch and added two new commits (exists only in main branch). git log output of the main branch: c72d4a9 ( HEAD - > main ) fourth commit on main 2c3dd61 third commit on main 0c2eec3 second commit on main 9b968e8 first commit on main git log output of the feature branch: 786650f ( HEAD - > feature ) third commit on feature 21cbaf1 second commit on feature 677bc7f first commit on feature 0c2eec3 second commit on main 9b968e8 first commit on mai...

How To Use KeePassXC Cli

There are similarly named programs: KeePass, KeePassX and KeePassXC (many of which are each others’ forks). Program Condition KeePass primarily for Windows. KeePassX no longer actively maintained. KeePassXC actively maintained and runs natively on Linux, macOS and Windows . Note: GUI version of the KeePassXC has more features than cli version. GUI version has variety of shortcuts as well. Regarding how to use GUI version of the KeePassXC, visit Getting Started Guide . Below features are available only in GUI version. Setting “Name” and “Description” fields of passwords database. Nesting Groups. Creating entry attributes ( open issue ). Adding Timed One-Time Passwords (TOTP). Adding entry with the same title as existing entry. KeePassXC stores all the passwords in passwords database. A passwords database (hereafter referred to as database) is an (encrypted) binary file. It can have any or no extension, but the .kdbx extension is commonly used. The ...