lookml-model
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseInstructions
说明
- Define the Model File: A model file generally corresponds to a single database connection and includes Explores.
- Required Parameters:
- : Must match a connection defined in Looker Admin.
connection: "connection_name" - : Specifies which view and dashboard files are available to the model.
include: "pattern"
- Best Practices:
- Includes: Avoid if possible to prevent performance issues and namespace clutter. Use specific paths or wildcards like
include: "*.view"orinclude: "/views/users.view".include: "/views/marketing/*.view" - Label: Use to provide a user-friendly name for the model in the UI.
label: - Week Start Day: Set if the business logic requires a specific start day (e.g.,
week_start_day:).monday - Datagroups & Caching: ALWAYS use datagroups for caching policies to align Looker with your ETL/ELT processes.
- Includes: Avoid
- 定义模型文件:一个模型文件通常对应单个数据库连接,并包含Explores。
- 必填参数:
- : 必须与Looker管理后台中定义的连接匹配。
connection: "connection_name" - : 指定哪些视图和仪表板文件可用于该模型。
include: "pattern"
- 最佳实践:
- 引入文件: 尽可能避免使用,以防止性能问题和命名空间混乱。使用特定路径或通配符,例如
include: "*.view"或include: "/views/users.view"。include: "/views/marketing/*.view" - 标签: 使用在UI中为模型提供用户友好的名称。
label: - 周起始日: 如果业务逻辑需要特定的起始日(例如),设置
monday。week_start_day: - 数据组与缓存: 始终使用数据组(datagroups)来设置缓存策略,使Looker与你的ETL/ELT流程保持一致。
- 引入文件: 尽可能避免使用
4. Datagroups & Caching
4. 数据组与缓存
Datagroups are the preferred mechanism for managing caching policies.
- Definition: Define in the model file.
- sql_trigger: A query that returns a single value (e.g., max timestamp). If the value changes, the cache is invalidated.
- max_cache_age: A fallback duration if the trigger doesn't change.
- persist_with: Apply the datagroup to Explores or the entire model.
数据组是管理缓存策略的首选机制。
- 定义: 在模型文件中定义。
- sql_trigger: 返回单个值的查询(例如最大时间戳)。如果值发生变化,缓存将失效。
- max_cache_age: 如果触发器未变化时的备用持续时间。
- persist_with: 将数据组应用于Explores或整个模型。
Datagroups vs persist_for
persist_for数据组 vs persist_for
persist_for| Feature | Datagroups (Recommended) | |
|---|---|---|
| Trigger | SQL Query (Smart) | Fixed Time (Dumb) |
| Alignment | Aligns with ETL/ELT completion | Misaligned (guesswork) |
| Management | Centralized in Model file | Scattered in Explores/Models |
| Use Case | Production dashboards, ETL synchronization | Ad-hoc queries, Real-time (<1h) needs |
[!TIP] UseONLY for real-time dashboards where you need to force a cache refresh every X minutes (e.g., stock tickers, fast-moving inventory). For everything else, use Datagroups.persist_for
| 特性 | 数据组(推荐) | |
|---|---|---|
| 触发方式 | SQL查询(智能) | 固定时间(固定间隔) |
| 一致性 | 与ETL/ELT完成时间对齐 | 不一致(凭猜测设置) |
| 管理方式 | 集中在模型文件中 | 分散在Explores/模型中 |
| 使用场景 | 生产仪表板、ETL同步 | 临时查询、实时需求(<1小时) |
[!提示] 仅在需要强制每隔X分钟刷新缓存的实时仪表板(例如股票行情、快速变动的库存)中使用。其他所有场景,请使用数据组。persist_for
5. Include Patterns
5. 引入路径模式
Use strict patterns to control scope and performance.
| Pattern | Description | Use Case |
|---|---|---|
| All views in specific folder | Standard modularity |
| Specific file | Precise control, avoids conflicts |
| Recursive (all views in project) | Avoid unless small project |
| All dashboards in folder | Importing dashboards |
使用严格的模式来控制范围和性能。
| 模式 | 描述 | 使用场景 |
|---|---|---|
| 指定文件夹中的所有视图 | 标准模块化 |
| 特定文件 | 精确控制,避免冲突 |
| 递归(项目中的所有视图) | 避免使用,除非是小型项目 |
| 文件夹中的所有仪表板 | 导入仪表板 |
Examples
示例
Basic Model
基础模型
lookml
connection: "thelook"lookml
connection: "thelook"Include all views in the views/ folder
引入views/文件夹中的所有视图
include: "/views/*.view"
include: "/views/*.view"
Include all dashboards
引入所有仪表板
include: "/*.dashboard"
include: "/*.dashboard"
Define an Explore (usually better to define in separate files for large projects, but acceptable here for small ones)
定义一个Explore(对于大型项目,最好在单独文件中定义,但小型项目中这样做是可接受的)
explore: orders {
join: users {
type: left_outer
sql_on: ${orders.user_id} = ${users.id} ;;
relationship: many_to_one
}
}
undefinedexplore: orders {
join: users {
type: left_outer
sql_on: ${orders.user_id} = ${users.id} ;;
relationship: many_to_one
}
}
undefinedModel with Specific Settings
带有特定设置的模型
lookml
connection: "snowlooker"
label: "eCommerce Analytics"lookml
connection: "snowlooker"
label: "eCommerce Analytics"Set valid week start day
设置有效的周起始日
week_start_day: monday
week_start_day: monday
Include specific folders
引入特定文件夹
include: "/views/finance/.view"
include: "/views/marketing/.view"
include: "/views/finance/.view"
include: "/views/marketing/.view"
Model with Datagroup (Best Practice)
带有数据组的模型(最佳实践)
lookml
connection: "thelook"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 ;;
max_cache_age: "24 hours" # Fallback
}
project.dataset.eventsdatagroup: ecommerce_etl {
description: "当events表中的最大created_at日期变化时触发。"
sql_trigger: SELECT MAX(created_at) FROM ;;
max_cache_age: "24 hours" # 备用设置
}
project.dataset.eventsApply default caching to all Explores in this model
将默认缓存应用于该模型中的所有Explores
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"
}
undefinedpersist_with: ecommerce_etl
include: "/views/*.view"
explore: orders {
此Explore继承模型默认的'persist_with: ecommerce_etl'
}
explore: real_time_dashboard {
根据需要覆盖为不同的策略
persist_for: "5 minutes"
}
undefined