lookml-model

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Instructions

说明

  1. Define the Model File: A model file generally corresponds to a single database connection and includes Explores.
  2. Required Parameters:
    • connection: "connection_name"
      : Must match a connection defined in Looker Admin.
    • include: "pattern"
      : Specifies which view and dashboard files are available to the model.
  3. Best Practices:
    • Includes: Avoid
      include: "*.view"
      if possible to prevent performance issues and namespace clutter. Use specific paths or wildcards like
      include: "/views/users.view"
      or
      include: "/views/marketing/*.view"
      .
    • Label: Use
      label:
      to provide a user-friendly name for the model in the UI.
    • Week Start Day: Set
      week_start_day:
      if the business logic requires a specific start day (e.g.,
      monday
      ).
    • Datagroups & Caching: ALWAYS use datagroups for caching policies to align Looker with your ETL/ELT processes.
  1. 定义模型文件:一个模型文件通常对应单个数据库连接,并包含Explores。
  2. 必填参数:
    • connection: "connection_name"
      : 必须与Looker管理后台中定义的连接匹配。
    • include: "pattern"
      : 指定哪些视图和仪表板文件可用于该模型。
  3. 最佳实践:
    • 引入文件: 尽可能避免使用
      include: "*.view"
      ,以防止性能问题和命名空间混乱。使用特定路径或通配符,例如
      include: "/views/users.view"
      include: "/views/marketing/*.view"
    • 标签: 使用
      label:
      在UI中为模型提供用户友好的名称。
    • 周起始日: 如果业务逻辑需要特定的起始日(例如
      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

数据组 vs
persist_for

FeatureDatagroups (Recommended)
persist_for
TriggerSQL Query (Smart)Fixed Time (Dumb)
AlignmentAligns with ETL/ELT completionMisaligned (guesswork)
ManagementCentralized in Model fileScattered in Explores/Models
Use CaseProduction dashboards, ETL synchronizationAd-hoc queries, Real-time (<1h) needs
[!TIP] Use
persist_for
ONLY 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.
PatternDescriptionUse Case
include: "/views/*.view"
All views in specific folderStandard modularity
include: "/views/marketing/users.view"
Specific filePrecise control, avoids conflicts
include: "/**/*.view"
Recursive (all views in project)Avoid unless small project
include: "/dashboards/*.dashboard"
All dashboards in folderImporting dashboards
使用严格的模式来控制范围和性能。
模式描述使用场景
include: "/views/*.view"
指定文件夹中的所有视图标准模块化
include: "/views/marketing/users.view"
特定文件精确控制,避免冲突
include: "/**/*.view"
递归(项目中的所有视图)避免使用,除非是小型项目
include: "/dashboards/*.dashboard"
文件夹中的所有仪表板导入仪表板

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 } }
undefined
explore: orders { join: users { type: left_outer sql_on: ${orders.user_id} = ${users.id} ;; relationship: many_to_one } }
undefined

Model 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
project.dataset.events
;; max_cache_age: "24 hours" # Fallback }
datagroup: ecommerce_etl { description: "当events表中的最大created_at日期变化时触发。" sql_trigger: SELECT MAX(created_at) FROM
project.dataset.events
;; max_cache_age: "24 hours" # 备用设置 }

Apply 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" }
undefined
persist_with: ecommerce_etl
include: "/views/*.view"
explore: orders {

此Explore继承模型默认的'persist_with: ecommerce_etl'

}
explore: real_time_dashboard {

根据需要覆盖为不同的策略

persist_for: "5 minutes" }
undefined