|
| 1 | +--- |
| 2 | +title: Using dbt |
| 3 | +permalink: /schema/advanced/using-dbt |
| 4 | +category: Data Schema |
| 5 | +subCategory: Advanced |
| 6 | +menuOrder: 10 |
| 7 | +--- |
| 8 | + |
| 9 | +<InfoBox> |
| 10 | + |
| 11 | +This functionality only works with models written in JavaScript, not YAML. |
| 12 | + |
| 13 | +</InfoBox> |
| 14 | + |
| 15 | +Cube's metrics layer is able to read metrics definitions from [dbt][dbt] via the |
| 16 | +[Metadata API][dbt-docs-metadata-api] and translate them into an equivalent Cube |
| 17 | +data model. |
| 18 | + |
| 19 | +## Connect dbt Core to Cube |
| 20 | + |
| 21 | +This can be done through reading the `dbt-project.yml` file. |
| 22 | + |
| 23 | +```javascript |
| 24 | +import dbt from '@cubejs-backend/dbt-schema-extension'; |
| 25 | + |
| 26 | +asyncModule(async () => { |
| 27 | + // `/dbt` is the path to a folder containing `dbt-project.yml` |
| 28 | + await dbt.loadMetricCubesFromDbtProject('/dbt'); |
| 29 | +}); |
| 30 | +``` |
| 31 | + |
| 32 | +## Connect dbt Cloud to Cube |
| 33 | + |
| 34 | +Create a `config.js` file as a sibling to the `schema/` folder in your Cube |
| 35 | +project: |
| 36 | + |
| 37 | +```javascript |
| 38 | +exports.dbtJobId = 65806; |
| 39 | +exports.dbtApiKey = 'YOUR_KEY_HERE'; |
| 40 | +``` |
| 41 | + |
| 42 | +Create a file called `dbt.js` in the `schema/` folder and enter the following |
| 43 | +code snippet: |
| 44 | + |
| 45 | +```javascript |
| 46 | +import dbt from '@cubejs-backend/dbt-schema-extension'; |
| 47 | +import { dbtJobId, dbtApiKey } from '../config'; |
| 48 | + |
| 49 | +asyncModule(async () => { |
| 50 | + await dbt.loadMetricCubesFromDbtCloud(dbtJobId, dbtApiKey); |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +You can then define cubes under the above snippet: |
| 55 | + |
| 56 | +```javascript |
| 57 | +import dbt from '@cubejs-backend/dbt-schema-extension'; |
| 58 | +import { dbtJobId, dbtApiKey } from '../config'; |
| 59 | + |
| 60 | +asyncModule(async () => { |
| 61 | + await dbt.loadMetricCubesFromDbtCloud(dbtJobId, dbtApiKey); |
| 62 | +}); |
| 63 | + |
| 64 | +cube('GithubCommitStatsCommitsCached', { |
| 65 | + extends: GithubCommitStatsCommits, |
| 66 | + |
| 67 | + preAggregations: { |
| 68 | + main: { |
| 69 | + measures: [commitsCount], |
| 70 | + dimensions: [authorDomain, authorName], |
| 71 | + timeDimension: timestamp, |
| 72 | + granularity: 'day', |
| 73 | + partitionGranularity: 'year', |
| 74 | + }, |
| 75 | + }, |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +[dbt]: https://www.getdbt.com/ |
| 80 | +[dbt-docs-metadata-api]: |
| 81 | + https://docs.getdbt.com/docs/dbt-cloud-apis/metadata-api |
0 commit comments