# 数据源

数据源（Sources）是指数据库中原始表的引用，您可以在模型中使用这些数据源。

<figure><img src="/files/crxbn2O0HGqIjUY4u1fP" alt=""><figcaption></figcaption></figure>

在传统的 SQL 转换中，原始表通常通过表名直接引用。这会带来一些问题，例如代码可读性差、表间的依赖管理难。相比之下，数据源提供了一种更结构化和模块化的方式来引用原始表，带来以下几个优点：

* **清晰的数据血缘**：将原始表定义为数据源可以帮助您明确标识外部数据源，从而建立清晰的数据血缘关系，便于追踪数据从源头到转换模型的流动。
* **数据测试**：声明数据源后，您可以实施数据测试，验证数据质量假设，确保后续的转换操作基于可信的数据。
* **改善沟通**：在数据源的元数据中，您可以添加描述、负责人等信息，以明确您的使用场景。这有助于提高透明度，促进跨团队的有效沟通。

## 创建数据源

要创建数据源，请按照以下步骤操作：

1. &#x5728;**“模型/ Models”**&#x6807;签页中，点击 **+** 图标并选&#x62E9;**“添加数据源/Add source”**。
2. 在弹出的窗口中：
   1. 选择连接类型。目前，Recurve 支持从数据仓库中引用数据源。
   2. 选择目标连接。如果您还没有设置过连接，请参考：[项目连接管理](/cn-reorc-help-center/lian-jie/xiang-mu-lian-jie-guan-li.md)。
3. 点&#x51FB;**“下一步/ Next ”**。Recurve 将显示目标连接中所有可用的表。

<figure><img src="/files/0POYEIKmvIEKp2MByjLW" alt=""><figcaption><p>从连接中选择表</p></figcaption></figure>

4. 选择所需的原始表或模型。&#x20;
5. 点&#x51FB;**“添加数据源/ Add source ”**。

选定的表将被添加&#x5230;**“数据源/ Sources ”**&#x6587;件夹，并按连接名称进行分组。

<figure><img src="/files/I3WXYWT1DDyTdTdH12TV" alt=""><figcaption></figcaption></figure>

您可以通过在编辑器中打开数据源来查看其架构，架构包含字段名（或列名）、数据类型和注释。

## 从数据源中选择

通过将原始表组织为数据源，您可以使用 `{{ source() }}` Jinja 函数在模型中引用它们。 请查阅： [Jinja 模板](/cn-reorc-help-center/advanced_usage/jinja-mu-ban.md)。

该函数需要两个参数：

* `source_name`: 包含数据源的文件夹名称。
* `table_name`: 表的名称。

例如，如果 `raw_orders` 表被添加到 `jaffle_shop`文件夹下，我们可以这样引用：

```sql
select 
    id as order_id,
    customer as customer_id,
    ordered_at as order_date

from {{ source("jaffle_shop", "raw_orders") }}
```


---

# Agent Instructions: 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.reorc.com/cn-reorc-help-center/shu-ju-jian-mo/shu-ju-yuan.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.
