Verwalten von dbt Projects on Snowflake mit Snowflake CLI

Bemerkung

The dbt Projects on Snowflake features in Snowflake CLI are available only in version 3.13.0 or later.

You can use Snowflake CLI to manage dbt projects with the following operations:

Deploying a dbt project object

The snow dbt deploy command uploads local files to a temporary stage and creates a new dbt project object, updates it by making a new version, or completely recreates it. A valid dbt project must contain two files:

  • dbt_project.yml: A standard dbt configuration file that specifies the profile to use.

  • profiles.yml: A dbt connection profile definition referenced in dbt_project.yml. profiles.yaml must define the database, role, schema, and type.

    • By default, dbt Projects on Snowflake uses your target schema (target.schema) specified from your dbt environment or profile. Unlike dbt Core behavior, the target schema specified in the profiles.yml file must exist before you create your dbt Project in order for it to compile or execute successfully.

    <profile_name>:
    target: dev
    outputs:
      dev:
        database: <database_name>
        role: <role_name>
        schema: <schema_name>
        type: snowflake
    
    Copy

The following examples illustrate how to use the snow dbt deploy command:

  • Stellen Sie das dbt-Projektobjekt jaffle_shop bereit:

    snow dbt deploy jaffle_shop
    
    Copy
  • Deploy a project named jaffle_shop from a specified directory and create or add a new version depending on whether the dbt project object already exists:

    snow dbt deploy jaffle_shop --source /path/to/dbt/directory --profiles-dir ~/.dbt/ --force
    
    Copy
  • Deploy a project named jaffle_shop from a specified directory using a custom profiles directory and enabling external access integrations:

    snow dbt deploy jaffle_shop --source /path/to/dbt/directory
    --profiles-dir ~/.dbt/ --default-target dev
    --external-access-integration dbthub-integration
    --external-access-integration github-integration
    --force
    
    Copy

Listing all available dbt project objects

The snow dbt list command lists all available dbt project objects on Snowflake.

The following examples illustrate how to use the snow dbt list command:

  • So listen Sie alle verfügbaren dbt-Projektobjekte auf:

    snow dbt list
    
    Copy
  • So listen Sie dbt-Projektobjekte in der product-Datenbank auf, deren Namen mit JAFFLE beginnen:

    snow dbt list --like JAFFLE% --in database product
    
    Copy

Executing a dbt project object command

The snow dbt execute command executes one of the following dbt commands on a Snowflake dbt project object:

Weitere Informationen zur Verwendung von dbt-Befehlen finden Sie in der dbt-Befehlsreferenz.

The following examples illustrate how to use the snow dbt execute command:

  • Ausführen des dbt-Befehls test:

    snow dbt execute jaffle_shop test
    
    Copy
  • Asynchrones Ausführen des dbt-Befehls run:

    snow dbt execute --run-async jaffle_shop run --select @source:snowplow,tag:nightly models/export
    
    Copy

Describing a dbt project object

The snow dbt describe command describes a dbt project object on Snowflake.

The following example describes the dbt project object named my_dbt_project on Snowflake:

snow dbt describe my_dbt_project
Copy

Dropping a dbt project object

The snow dbt drop command deletes a dbt project object on Snowflake.

The following example deletes the dbt project object named my_dbt_project on Snowflake:

snow dbt drop my_dbt_project
Copy

Use snow dbt commands in a CI/CD workflow

Bemerkung

Beim Erstellen von CI/CD-Workflows benötigen Sie nur Ihren Git-Server, wie z. B. Github, und Snowflake CLI. Ein Git-Repository-Objekt ist nicht erforderlich.

Sie können dbt-Befehle mit Snowflake CLI zum Erstellen von CI/CD-Pipelines ausführen. Diese Pipelines werden häufig verwendet, um neuen Code zu testen, z. B. neue Pull-Anforderungen, oder um Produktionsanwendungen bei einer Zusammenführung mit dem Hauptzweig zu aktualisieren.

Zur Erstellung eines CI/CD-Workflows mit snow dbt-Befehlen gehen Sie wie folgt vor:

  1. Bereiten Sie Ihr dbt-Projekt vor:

    1. Laden Sie Ihr dbt-Projekt herunter, oder starten Sie ein neues.

      • Stellen Sie sicher, dass das Hauptverzeichnis des Projekts die Dateien dbt_project.yml und profiles.yml enthält.

      • Überprüfen Sie, ob der in dbt_project.yml angegebene Profilname in profiles.yml definiert ist.

        Bemerkung

        Die dbt-Projektobjekte von Snowflake benötigen keine Kennwörter. Wenn profiles.yml Kennwörter enthält, wird die Bereitstellung ausgesetzt, bis sie entfernt werden.

  2. Richten Sie eine Snowflake-CLI-GitHub-Aktion ein.

    Befolgen Sie die Richtlinien für die -Einrichtung einer GitHub-Aktion für die Snowflake-CLI und überprüfen Sie Ihre Verbindung zu Snowflake.

  3. Definieren Sie Ihren Workflow.

    Bestimmen Sie, welche Befehle Ihr Workflow ausführen muss, basierend auf den Anforderungen Ihrer Organisation. Im folgenden Beispiel wird ein CI-Workflow gezeigt, der die Version des dbt-Projektobjekts namens product_pipeline mit neuen Dateien aktualisiert und die Transformationen sowie schließlich die Tests durchführt:

    - name: Execute Snowflake CLI command
      run: |
        snow dbt deploy product_pipeline
        snow dbt execute product_pipeline run
        snow dbt execute product_pipeline test
    
    Copy