关于 lookml-model
面向 LookML 模型文件编写者的参考技能,说明连接、文件包含范围与缓存策略的配置。重点介绍用 datagroups 将缓存失效与 ETL/ELT 流程对齐,并以精确的 include 模式控制范围。
主要功能
- 01配置模型连接与文件包含
- 02用 datagroups 管理缓存策略
- 03设置模型标签和周起始日
- 04选择精确的 include 模式
适用场景
- 01为 Looker 项目编写模型文件
- 02让缓存失效与 ETL/ELT 完成状态同步
- 03限制模型加载的视图和仪表板范围
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ lkrdev/ lookml_skills --skill lookml-model安装后在 Claude Code 中输入/lookml-model确认已加载。
详细介绍
说明
- 定义模型文件:模型文件通常对应一个数据库连接,并包含 Explores。
- 必需参数:
connection: "connection_name":必须与 Looker 管理后台中定义的连接一致。include: "pattern":指定模型可用的视图和仪表板文件。
- 最佳实践:
- 包含文件:尽可能避免使用
include: "*.view",以免造成性能问题和命名空间混乱。请使用具体路径或通配符,例如include: "/views/users.view"或include: "/views/marketing/*.view"。 - 标签:使用
label:为 UI 中的模型指定易于用户识别的名称。 - 每周起始日:如果业务逻辑要求指定每周的起始日(例如
monday),请设置week_start_day:。 - Datagroups 和缓存:始终使用 datagroups 管理缓存策略,使 Looker 与 ETL/ELT 流程保持一致。
- 包含文件:尽可能避免使用
4. Datagroups 和缓存
Datagroups 是管理缓存策略的首选机制。
- 定义:在模型文件中定义。
- sql_trigger:返回单个值(例如最大时间戳)的查询。如果该值发生变化,缓存就会失效。
- max_cache_age:触发条件未变化时的备用时长。
- persist_with:将 datagroup 应用于 Explores 或整个模型。
Datagroups 与 persist_for 对比
| 特性 | Datagroups(推荐) | persist_for |
|---|---|---|
| 触发条件 | SQL 查询(智能) | 固定时间(不够智能) |
| 一致性 | 与 ETL/ELT 完成时间保持一致 | 不一致(依赖猜测) |
| 管理方式 | 集中在模型文件中 | 分散在 Explores/Models 中 |
| 使用场景 | 生产仪表板、ETL 同步 | 临时查询、实时(<1h)需求 |
提示
仅在需要每隔 X 分钟强制刷新缓存的实时仪表板中使用
persist_for(例如股票行情、快速变化的库存)。其他情况一律使用 Datagroups。
5. Include 模式
使用严格的模式来控制范围和性能。
| 模式 | 描述 | 使用场景 |
|---|---|---|
include: "/views/*.view" | 特定文件夹中的所有视图 | 标准模块化 |
include: "/views/marketing/users.view" | 特定文件 | 精确控制,避免冲突 |
include: "/**/*.view" | 递归(项目中的所有视图) | 除非项目很小,否则避免使用 |
include: "/dashboards/*.dashboard" | 文件夹中的所有仪表板 | 导入仪表板 |
示例
基础模型
connection: "thelook"
# Include all views in the views/ folder
include: "/views/*.view"
# Include all dashboards
include: "/*.dashboard"
# Define an Explore (usually better to define in separate files for large projects, but acceptable here for small ones)
explore: orders {
join: users {
type: left_outer
sql_on: ${orders.user_id} = ${users.id} ;;
relationship: many_to_one
}
}
使用特定设置的模型
connection: "snowlooker"
label: "eCommerce Analytics"
# Set valid week start day
week_start_day: monday
# Include specific folders
include: "/views/finance/*.view"
include: "/views/marketing/*.view"
## Model with Datagroup (Best Practice)
```lookml
connection: "thelook"
# Define the caching policy
datagroup: ecommerce_etl {
description: "Triggers when the max created_at date changes in the events table."
sql_trigger: SELECT MAX(created_at) FROM `project.dataset.events` ;;
max_cache_age: "24 hours" # Fallback
}
# Apply default caching to all Explores in this model
persist_with: ecommerce_etl
include: "/views/*.view"
explore: orders {
# This explore inherits 'persist_with: ecommerce_etl' from the model default
}
explore: real_time_dashboard {
# Override with a different policy if needed
persist_for: "5 minutes"
}
```
评论
支持 Markdown