> For the complete documentation index, see [llms.txt](https://docs.reorc.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.reorc.com/cn-reorc-help-center/shu-ju-jian-mo/mo-xing.md).

# 模型

模型（Models）是一种用于从数据源提取数据，进行转换的查询，并生成结构化的数据集。

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

Recurve 当前支持使用 SQL 编写模型。

以下示例展示了 SQL 模型的基本语法：

* 模型的核心是对数据源的常规查询。
* 最终通过一个 SELECT 语句完成数据转换。
* 此外，Jinja 模板语言被集成进来，以便进行引用并增强逻辑处理。请查阅： [Jinja 模板](/cn-reorc-help-center/advanced_usage/jinja-mu-ban.md)。

```sql
-- Select the total number of orders per customer
select
    customers.customer_id,
    customers.first_name,
    customers.last_name,
    count(orders.order_id) as total_orders
from
    {{ source('jaffle_shop', 'raw_customers') }} as customers
join
    {{ source('jaffle_shop', 'raw_orders') }} as orders
on
    customers.customer_id = orders.customer_id
group by
    customers.customer_id,
    customers.first_name,
    customers.last_name
order by
    total_orders desc
```

当 Recurve 运行您的 SQL 模型时，它会将这些模型编译为可执行的 SQL 查询。这一过程包括解析所有的 Jinja 表达式、基于`ref()` 和`source()` 调用来解析模型和数据源的依赖关系，并结合配置（如物化类型）生成最终的 SQL。

将数据转换逻辑作为模型实现，带来了多种优势：

* 简化操作：可以将复杂的转换拆分为更小的、易管理的步骤。
* 模块化和可复用性：某些转换逻辑可以提取为基础模型，在多个地方复用，使得转换可以逐步构建，而不是从头开始。
* 数据血缘和透明性：模型之间的关系会自动跟踪并展示在数据血缘图中，有助于理解模型依赖，方便调试。请参考： [数据血缘](/cn-reorc-help-center/shu-ju-jian-mo/shu-ju-xue-yuan.md)。
* （即将上线）测试和验证：您可以为每个模型编写测试，以确保数据质量。在模型运行时与测试一并执行，从而尽早捕捉数据问题。&#x20;

## 创建 SQL 模型

创建 SQL 模型的步骤如下：

1. 在“**模型 / Models**” 选项卡中，点击 **+ 图标** 并选择 “**新建 SQL 模型 / New SQL model**”**。**
2. 为模型提供一个名称，然后点击 “**创建 / Create**”。

   创建的模型将会显示在 **models** 文件夹下。
3. 在模型编辑器中，输入您的 SQL 查询。

   <figure><img src="/files/8ijxajpp6TgBQhrKVrKA" alt=""><figcaption></figcaption></figure>

示例如下：

```sql
-- select customers from raw_customers table
select id as customer_id
       name as customer_name
from {{ source('jaffle_shop', 'raw_customers') }}
```

4. 点击 “**保存 / Save**” 以确认更改。
5. 点击 “**预览 / Preview**” 以查看查询结果，在 “**结果 / Results**” 选项卡中展示输出。

   <figure><img src="/files/hVeE32lev7IyxxKNzZuC" alt=""><figcaption></figcaption></figure>
6. 要查看模型的编译代码，请点击 “**编译代码 / Compiled code”**。

## 引用模型

在一个模型中引用另一个模型，允许您在现有的转换基础上进行扩展，而无需重复编写。这种模块化的方法使得数据转换更加简洁，逻辑更易于维护和理解。

在模型中，您可以使用 `ref()` 函数来引用其他模型。此函数会在模型之间建立依赖关系，确保模型按正确的顺序构建。

示例如下：

```sql
-- model: stg_orders
select
    id as order_id,
    user_id as customer_id,
    order_date,
    status,
    amount
from {{ source('jaffle_shop', 'orders') }}
```

```sql
-- model: mart_daily_revenue

select
    date_trunc('day', order_date) as date,
    count(*) as number_of_orders,
    sum(amount) as daily_revenue
from {{ ref('stg_orders') }}
where status = 'completed'
group by 1
order by 1 desc
```

在  `mart_daily_revenu`中， `ref('stg_orders')` 函数引用了 `stg_oders` 作为依赖。这个引用使得当前模型能够利用模型的结果，并确保模型构建时按正确的顺序进行。

## 组织模型

由于数据转换可能遵循不同的策略并经过多个阶段，您可以将模型按转换的不同阶段组织到文件夹中，从而帮助保持项目结构的清晰和逻辑性。

要创建新的文件夹，点击现有文件夹的操作按钮，并选择 “**添加子文件夹/** **Add sub-folder**”：

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

要将模型移动到新文件夹中，点击模型的操作按钮，并选择 “**移动/ Move**”。然后，从列表中选择目标文件夹。&#x20;

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