关于 lookml-model
面向 LookML 开发者的模型文件编写指南,涵盖连接、文件引入与缓存策略。重点说明如何通过 datagroups 将缓存刷新与 ETL/ELT 流程对齐。
主要功能
- 01配置模型连接与标签
- 02使用精确的 include 模式
- 03设置周起始日
- 04通过 datagroups 管理缓存
适用场景
- 01为 LookML 模型选择视图和仪表板文件
- 02根据 ETL/ELT 状态刷新生产仪表板缓存
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ looker-open-source/ looker-skills --skill lookml-model安装后在 Claude Code 中输入/lookml-model确认已加载。
详细介绍
说明
- 定义模型文件:模型文件通常对应一个数据库连接,并包含多个 Explore。
- 必需参数:
connection: "connection_name":必须与 Looker Admin 中定义的连接匹配。include: "pattern":指定模型可用的视图文件和仪表板文件。
- 最佳实践:
- Include:尽可能避免使用
include: "*.view",以防性能问题和命名空间杂乱。使用特定路径或通配符,例如include: "/views/users.view"或include: "/views/marketing/*.view"。 - 标签:使用
label:为 UI 中的模型提供便于用户理解的名称。 - 周起始日:如果业务逻辑要求特定的周起始日(例如
monday),请设置week_start_day:。 - Datagroups 和缓存:始终使用 datagroups 管理缓存策略,使 Looker 与 ETL/ELT 流程保持一致。
- Include:尽可能避免使用
4. Datagroups 和缓存
Datagroups 是管理缓存策略的首选机制。
- 定义:在模型文件中定义。
- sql_trigger:返回单个值(例如最大时间戳)的查询。如果该值发生变化,缓存就会失效。
- max_cache_age:触发器未发生变化时的备用时长。
- persist_with:将 datagroup 应用于 Explore 或整个模型。
Datagroups 与 persist_for 的对比
| 功能 | Datagroups(推荐) | persist_for |
|---|---|---|
| 触发方式 | SQL 查询(智能) | 固定时间(不够灵活) |
| 同步 | 与 ETL/ELT 完成时间同步 | 不同步(依赖估算) |
| 管理方式 | 集中在模型文件中 | 分散在 Explore/模型中 |
| 适用场景 | 生产仪表板、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"
使用 Datagroup 的模型(最佳实践)
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