dbt table 覆盖率指标

dbt table coverage metric

鉴于我有一个数据仓库,其中包含从各种来源创建的各种表,其中许多是由 dbt 创建的,我想衡量一个像 'dbt table coverage' 这样的概念,我将其定义为:

dtc = count(tables and views that exist) / count(non ephemeral models and sources)

这对于保持 quality/completeness 的感觉非常有用,尤其是在过渡项目期间。是否有像这样的 dbt 命令:

dbt report table-coverage --schemas=['reporting','example']
>>> 96% coverage, 48/50 tables in the schemas provided are captured in dbt. 

如果没有,我们如何将其添加到项目中?!

我可以采用哪些替代方法来解决问题

为此,我可能会创建一个模型(视图)来查询 information_schema,并对 {sourceTableName}stg_{sourceTableName} 的一对一映射做出一些假设(假设这意味着您的承保范围)。

此外,我会考虑使用 graph.sources.values() JINJA 函数来遍历项目中所有记录的源代码,然后将其与 {target.schema}[= 中的每个模型进行比较17=]

https://docs.getdbt.com/reference/dbt-jinja-functions/graph#accessing-sources

如果您将 source.schema.ymlsource.information_schema 的存在进行比较。我会改变考虑将图表中的每个项目映射到源数据库 information_schema 中的项目总数的方法。

这里有一些想法,因为这对我当前的案例也很有趣:

  1. dbt 不会向命令行提供查询输出或 return 结果。 (据我所知!)因此,如果此时有 1 个本质上不受支持的功能。即 dbt reportdbt query 尚不存在。如果需要,我建议在这里构建一个功能请求: https://github.com/fishtown-analytics/dbt/issues

  2. 如果您可以在 dbt 中制作模型,然后通过您选择的客户端执行它,让我们试一试。 (我使用的是 postgres,所以相应地进行转换)

    WITH schema_map as
       (select schemaname as schema,
        tablename as name,
        'Table' as Type,
        CASE WHEN schemaname like '%dbt%' THEN 1
         ELSE 0 END as dbt_created
        from pg_tables
    WHERE NOT schemaname = ANY('{information_schema,pg_catalog}')
    UNION
    select schemaname as schema,
        viewname as name,
        'View' as Type,
        CASE WHEN schemaname like '%dbt%' THEN 1
             ELSE 0 END as dbt_created
        from pg_views
     WHERE NOT schemaname = ANY('{information_schema,pg_catalog}') )
     SELECT count(name) as total_tables_and_views,
        sum(dbt_created) as dbt_created,
        to_char((sum(dbt_created)::dec/count(name)::dec)*100,'999D99%') as dbt_coverage
     FROM schema_map
    

给出结果:

total_tables_and_views | dbt_created | dbt_coverage
391                    |292          |  74.68%

只是为了回馈社区,感谢 Jordan 和 Gscott 的启发。我为 SQL Server/ Synapse 执行的解决方案是:

  1. 每日执行 INFORMATION_SCHEMA.TABLES 和 dbt 图中的模型计数作为一个 table。
  2. 增量 table 建立在 1 的基础上,选择感兴趣的模式和聚合。在我下面的例子中,我过滤掉了暂存和测试。

DbtModelCounts:


{% set models = [] -%}

{% if execute %}
  {% for node in graph.nodes.values()
    | selectattr("resource_type", "equalto", "model")
    %}
        {%- do models.append(node.name) -%}

  {% endfor %}
{% endif %}

with tables AS
(
SELECT table_catalog [db], table_schema [schema_name], table_name [name], table_type [type]
FROM INFORMATION_SCHEMA.TABLES
),
dbt_tables AS
(
SELECT *
FROM tables
WHERE name in (
    {%- for model in models %}
    ('{{ model}}') 
    {% if not loop.last %},
    {% endif %}
    {% endfor %}
    )
)
SELECT
    tables.db, 
    tables.schema_name,
    tables.type,
    COUNT(tables.name) ModelCount,
    COUNT(dbt_tables.name) DbtModelCount
FROM tables
LEFT JOIN dbt_tables ON
    tables.name=dbt_tables.name AND
    tables.schema_name = dbt_tables.schema_name AND
    tables.db = dbt_tables.db AND 
    tables.type = dbt_tables.type
GROUP BY
    tables.db,
    tables.schema_name,
    tables.type

Dbt 覆盖率:

{{
  config(
    materialized='incremental',
    unique_key='DateCreated'
  )
}}
SELECT 
    CAST(GETDATE() AS DATE) AS DateCreated,
    GETDATE() AS DateTimeCreatedUTC,
    SUM(DbtModelCount) AS DbtModelCount, 
    SUM(ModelCount) AS TotalModels,
    SUM(DbtModelCount)*100.0/SUM(ModelCount) as DbtCoveragePercentage
FROM {{ref('DbtModelCounts')}}
WHERE schema_name NOT LIKE 'testing%' AND schema_name NOT LIKE 'staging%'

为此,为定义的源添加逻辑以计算映射到我的暂存或原始模式的源的百分比 tables 。