> For the complete documentation index, see [llms.txt](https://docs.codatum.jp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.codatum.jp/external-integrations/bigquery-job-labels.md).

# BigQuery ジョブラベルからの利用実態取得

{% hint style="warning" %}
この機能は現在ベータ版です。仕様については予告なく変更となる場合がございます。
{% endhint %}

Codatum が BigQuery に発行するクエリジョブには、実行元（どの Notebook / Report / Query / Signed Embed から実行されたか）を示すラベルが付与されます。`INFORMATION_SCHEMA.JOBS` のラベルを参照することで、テーブルの棚卸しやコスト分析を Codatum の利用実態と突き合わせて自動化できます。

## 付与されるラベル

| ラベルキー                  | 内容                                                                                                        |
| ---------------------- | --------------------------------------------------------------------------------------------------------- |
| `source`               | 常に `codatum`                                                                                              |
| `cdm_source_type`      | `notebook` / `query` / `report` / `signed_embed` / `unknown`                                              |
| `cdm_<source_type>_id` | 主たる実行元 ID を **1 つ**（例 `cdm_notebook_id` / `cdm_report_id`）。優先順位 report > signed\_embed > notebook > query |
| `cdm_workspace_id`     | Workspace ID                                                                                              |
| `cdm_connection_id`    | Connection ID                                                                                             |
| `cdm_actor_type`       | `user` / `workflow` / `api_key` / `admin` / `system`                                                      |

> 付与対象は Notebook セル実行 / Saved Query / Report（定期実行）/ 外部 API / Signed Embed です。内部メタデータ取得系（DryRun・スキーマ取得・データプレビュー等）は対象外です。report 実行時は `cdm_notebook_id` ではなく `cdm_report_id` が載ります。

## 取得方法

```sql
SELECT
  job_id,
  creation_time,
  total_bytes_billed,
  (SELECT value FROM UNNEST(labels) WHERE key = 'cdm_source_type') AS source_type,
  (SELECT value FROM UNNEST(labels) WHERE key = 'cdm_notebook_id') AS notebook_id,
  (SELECT value FROM UNNEST(labels) WHERE key = 'cdm_report_id')   AS report_id
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
  AND EXISTS (SELECT 1 FROM UNNEST(labels) WHERE key = 'source' AND value = 'codatum')
```

`region-us` の部分は、対象データセットのリージョンに合わせて変更してください。

## ユースケース

* **テーブル棚卸し**: どの Notebook / Report がどのテーブルを参照しているかを機械的に取得し、dbt の棚卸しなどを自動化する
* **コスト分析**: 課金額の大きいクエリやスロークエリを特定し、それを発行している Notebook / Report（ダッシュボード）を逆引きする

## 他のデータウェアハウス

BigQuery 以外でも、同等の実行元情報が付与されます。取得元はデータウェアハウスごとに異なります。

* **Snowflake**: `QUERY_TAG`（JSON）として付与されます。`QUERY_HISTORY` の `QUERY_TAG` を参照してください。
* **Redshift / Databricks**: 実行する SQL の先頭コメント `/* codatum source_type=... */` として付与されます。クエリ履歴のクエリ本文から参照してください。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.codatum.jp/external-integrations/bigquery-job-labels.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
