dbt + Metabase integration.
Project description
dbt-metabase
Integration between dbt and Metabase.
If dbt is your source of truth for database schemas and you use Metabase as your analytics tool, dbt-metabase can propagate table relationships, model and column descriptions and semantic types (e.g. currency, category, URL) to your Metabase data model, and extract questions and dashboards as exposures in your dbt project.
Requirements
Requires Python 3.8 or above.
For development, you will need uv installed.
Usage
You can install dbt-metabase from PyPI:
pip install dbt-metabase
Sections below demonstrate basic usage examples, for all CLI options:
dbt-metabase --help
Manifest
Before running dbt-metabase, you need a compiled manifest.json
file to parse. These are part of the dbt artifact generated during compilation.
Once dbt compile
finishes, manifest.json
can be found in the target/
directory of your dbt project.
See dbt documentation for more information.
Metabase API
All commands require authentication against the Metabase API using one of these methods:
- API key (
--metabase-api-key
)- Strongly recommended for automation, see documentation (Metabase 49 or later).
- Username and password (
--metabase-username
/--metabase-password
)- Fallback for older versions of Metabase and smaller instances.
Exporting Models
Let's start by defining a short sample schema.yml
as below.
models:
- name: stg_users
description: User records.
columns:
- name: id
description: Primary key.
data_tests:
- not_null
- unique
- name: email
description: User's email address.
- name: group_id
description: Foreign key to user group.
data_tests:
- not_null
- relationships:
to: ref('groups')
field: id
- name: stg_groups
description: User groups.
columns:
- name: id
description: Primary key.
data_tests:
- not_null
- unique
- name: name
description: Group name.
This is already enough to propagate the primary keys, foreign keys and descriptions to Metabase:
dbt-metabase models \
--manifest-path target/manifest.json \
--metabase-url https://metabase.example.com \
--metabase-api-key mb_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX= \
--metabase-database business \
--include-schemas public
Open Metabase and go to Settings > Admin Settings > Table Metadata, you will notice that id
column in stg_users
is now marked as "Entity Key" and group_id
is a "Foreign Key" pointing to id
in stg_groups
.
Try running dbt-metabase models --help
to see all the options available for fine tuning.
Foreign Keys
Native relationship tests and column-level constraints are the recommended ways of defining foreign keys, however you can override them with fk_target_table
and fk_target_field
meta fields. If both are set for a column, meta fields take precedence.
- name: country_id
description: FK to User's country in the dim_countries table.
meta:
metabase.fk_target_table: analytics_dims.dim_countries
metabase.fk_target_field: id
You can provide fk_target_table
as schema_name.table_name
or just table_name
to use the current schema. If your model has an alias, provide that alias rather than the original name.
Semantic Types
Now that we have foreign keys configured, let's tell Metabase that email
column contains email addresses:
- name: email
description: User's email address.
meta:
metabase.semantic_type: type/Email
Once you run dbt-metabase models
again, you will notice that email
column is now marked as "Email".
Below are common semantic types (formerly known as special types) accepted by Metabase:
type/PK
type/FK
type/Number
type/Currency
type/Category
type/Title
type/Description
type/City
type/State
type/ZipCode
type/Country
type/Latitude
type/Longitude
type/Email
type/URL
type/ImageURL
type/SerializedJSON
type/CreationTimestamp
See Metabase documentation for a more complete list.
Visibility Types
You can optionally specify visibility for tables and columns, this controls whether they are displayed in Metabase.
Here is how you would hide that email column:
- name: email
description: User's email address.
meta:
metabase.semantic_type: type/Email
metabase.visibility_type: sensitive
Below are the visibility types supported for columns:
normal
(default) - This field will be displayed normally in tables and charts.details-only
- This field will only be displayed when viewing the details of a single record.sensitive
- This field won't be visible or selectable in questions created with the GUI interfaces.
Tables support the following:
- No value for visible (default)
hidden
technical
cruft
If you notice any changes to these, please submit a pull request with an update.
Other Meta Fields
In addition to foreign keys, semantic types and visibility types, Metabase also accepts the following meta fields:
- name: model_name
meta:
metabase.display_name: another_model_name
metabase.visibility_type: normal
metabase.points_of_interest: Relevant records.
metabase.caveats: Sensitive information about users.
columns:
- name: column_name
meta:
metabase.display_name: another_column_name
metabase.visibility_type: sensitive
metabase.semantic_type: type/Number
metabase.has_field_values: list
metabase.coercion_strategy: keyword
metabase.number_style: decimal
See Metabase documentation for details and accepted values.
Synchronization
By default, dbt-metabase waits for tables and columns to be synchronized between your dbt project and Metabase database, otherwise the export fails when the sync timeout expires.
If you have known discrepancies between dbt and Metabase and wish to proceed without synchronization, set the sync timeout to zero (e.g. --sync-timeout 0
). This is discouraged, because you will still encounter errors if you have a table or column in your dbt project that is missing from Metabase and dbt-metabase attempts to export it.
Exposure Extraction
dbt-metabase allows you to extract questions and dashboards from Metabase as dbt exposures in your project:
dbt-metabase exposures \
--manifest-path ./target/manifest.json \
--metabase-url https://metabase.example.com \
--metabase-api-key mb_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX= \
--output-path models/ \
--exclude-collections "temp*"
Once the execution completes, check your output path for exposures files containing descriptions, creator details and links for Metabase questions and dashboards:
exposures:
- name: number_of_orders_over_time
description: '
### Visualization: Line
A line chart depicting how order volume changes over time
#### Metadata
Metabase Id: __8__
Created On: __2021-07-21T08:01:38.016244Z__'
type: analysis
url: https://metabase.example.com/card/8
maturity: medium
owner:
name: Indiana Jones
email: indiana@example.com
depends_on:
- ref('orders')
Native query questions will have SQL code blocks inside the descriptions, formatted to look nice in dbt docs. These YAML files can be committed to source control to understand how exposures change over time.
Try running dbt-metabase exposures --help
to see all the options available for fine tuning.
Configuration
There are 3 levels of configuration in decreasing order of precedence:
- CLI arguments, e.g.
--manifest-path target/manifest.json
- Environment variables, e.g.
MANIFEST_PATH=target/manifest.json
- Configuration file, e.g.
manifest_path: target/manifest.json
Try running --help
for any command to see the full list of CLI arguments and environment variables.
A configuration file can be created in ~/.dbt-metabase/config.yml
for dbt-metabase to pick it up automatically or anywhere else by specifying dbt-metabase --config-path path/to/config.yml
(must come before the command). Here is an example YAML file:
config:
manifest_path: target/manifest.json
metabase_url: https://metabase.example.com
metabase_api_key: mb_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=
# Configuration specific to models command
models:
metabase_database: business
# Configuration specific to exposures command
exposures:
output_path: models
Note that common configurations are in the outer block and command-specific ones are in separate blocks.
Programmatic API
Alternatively, you can invoke dbt-metabase programmatically. Below is the equivalent of CLI examples:
from dbtmetabase import DbtMetabase, Filter
# Initializing instance
c = DbtMetabase(
manifest_path="target/manifest.json",
metabase_url="https://metabase.example.com",
metabase_api_key="mb_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=",
)
# Exporting models
c.export_models(
metabase_database="business",
schema_filter=Filter(include=["public"]),
)
# Extracting exposures
c.extract_exposures(
output_path=".",
collection_filter=Filter(exclude=["temp*"]),
)
See function header comments for information about other parameters.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dbt_metabase-1.3.9.tar.gz
.
File metadata
- Download URL: dbt_metabase-1.3.9.tar.gz
- Upload date:
- Size: 480.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36baf8fa79c9968a66298da1697543c1bab1a8197b763019430249509e9f7750 |
|
MD5 | 1ec0efeabeabcf12bc71b7fd6082f2ad |
|
BLAKE2b-256 | 1d9be8f7164cfbe641e02a03403d136b7b25e1e71a6625beac74d35d2d84a433 |
Provenance
The following attestation bundles were made for dbt_metabase-1.3.9.tar.gz
:
Publisher:
master.yml
on gouline/dbt-metabase
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
dbt_metabase-1.3.9.tar.gz
- Subject digest:
36baf8fa79c9968a66298da1697543c1bab1a8197b763019430249509e9f7750
- Sigstore transparency entry: 145855313
- Sigstore integration time:
- Predicate type:
File details
Details for the file dbt_metabase-1.3.9-py3-none-any.whl
.
File metadata
- Download URL: dbt_metabase-1.3.9-py3-none-any.whl
- Upload date:
- Size: 28.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d65cdc57cb1d7c5c64c32303e889b778f9e9cb8c08a4384c7592be8d168ec4f |
|
MD5 | f0c73a49782148cb3338308202d5bea2 |
|
BLAKE2b-256 | 6cec1f79673a754c467d2a58dc6b11c265aa6d0e27b2ee4de88477f8f2fbaa08 |
Provenance
The following attestation bundles were made for dbt_metabase-1.3.9-py3-none-any.whl
:
Publisher:
master.yml
on gouline/dbt-metabase
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
dbt_metabase-1.3.9-py3-none-any.whl
- Subject digest:
9d65cdc57cb1d7c5c64c32303e889b778f9e9cb8c08a4384c7592be8d168ec4f
- Sigstore transparency entry: 145855314
- Sigstore integration time:
- Predicate type: