Table of Contents

加载并检查指标视图

本操作指南演示如何将 Databricks Metric View 加载到 Tabular Editor 中,并使用 C# Script 探索其结构。这是进行其他所有 Metric View 操作的基础。

Note

这些操作指南适用于 Tabular Editor 3.26.2 及更高版本。较早版本不支持本文所示的 v1.1 指标视图功能。

为这些代码示例加载示例 Metric View

开始前,请确保你已打开 Tabular Editor 3,并已打开一个表格模型;如果没有,也可以新建一个模型。

本操作指南使用一个示例电商 Metric View 来表示销售数据,其中三个维度表(product、customer、date)连接到一个事实表(orders)。使用下面任一方法来加载它(“下载并加载”或“复制并反序列化”),然后继续阅读本操作指南的其余内容。你可以在与本示例其余部分相同的 C# Script 中运行任一命令;也可以先在单独的 C# Script 中运行该命令,再在另一个 C# Script 中运行本示例的其余部分。

下载 sample-metricview.yaml 然后按路径加载:

SemanticBridge.MetricView.Load("C:/path/to/sample-metricview.yaml");

访问已加载的 Metric View

加载完成后,可在任何脚本中通过 SemanticBridge.MetricView.Model 访问该 Metric View。这会返回一个 Metric View 的 View 对象,它是 Metric View 对象图 的根节点。

var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;

sb.AppendLine($"Version: {view.Version}");
sb.AppendLine($"Source (fact table): {view.Source}");
Output(sb.ToString());

输出

Version: 1.1
Source (fact table): sales.fact.orders

检查 Metric View 的连接(维度表)

Metric View 的 Joins 属性包含与事实表连接的维度表。

var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;

sb.AppendLine($"Number of joins: {view.Joins.Count}");
sb.AppendLine("");

foreach (var join in view.Joins)
{
    sb.AppendLine($"Join: {join.Name}");
    sb.AppendLine($"  Source: {join.Source}");
    sb.AppendLine($"  On: {join.On}");
    sb.AppendLine($"  Cardinality: {join.Cardinality?.ToString() ?? "ManyToOne (default)"}");
    sb.AppendLine("");
}

Output(sb.ToString());

输出:

Number of joins: 3

Join: product
  Source: sales.dim.product
  On: source.product_id = product.product_id
  Cardinality: ManyToOne

Join: customer
  Source: sales.dim.customer
  On: source.customer_id = customer.customer_id
  Cardinality: ManyToOne

Join: date
  Source: sales.dim.date
  On: source.order_date = date.date_key
  Cardinality: ManyToOne

检查指标视图字段

指标视图的 Fields 属性包含所有字段定义。

var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;

sb.AppendLine($"Number of fields: {view.Fields.Count}");
sb.AppendLine("");

foreach (var field in view.Fields)
{
    sb.AppendLine($"{field.Name,-20} <- {field.Expr}");
}

Output(sb.ToString());

输出:

Number of fields: 6

product_name         <- product.product_name
product_category     <- product.category
customer_segment     <- customer.segment
order_date           <- date.full_date
order_year           <- date.year
order_month          <- date.month_name

查看 Metric View 度量值

Metric View 的 Measures 属性包含所有 Metric View 度量值定义及其聚合表达式。

var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;

sb.AppendLine($"Number of measures: {view.Measures.Count}");
sb.AppendLine("");

foreach (var measure in view.Measures)
{
    sb.AppendLine($"{measure.Name,-20} = {measure.Expr}");
}

Output(sb.ToString());

输出:

Number of measures: 6

total_revenue        = SUM(revenue)
gross_margin         = SUM(revenue) - SUM(cost)
order_count          = COUNT(*)
avg_order_value      = AVG(revenue)
revenue_to_budget    = (SUM(revenue) - SUM(budget)) / SUM(budget)
unique_customers     = COUNT(DISTINCT customer_id)

生成完整摘要

下面是一段完整脚本,用于输出整个 Metric View 的格式化摘要。

var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;

sb.AppendLine("METRIC VIEW SUMMARY");
sb.AppendLine("===================");
sb.AppendLine("");
sb.AppendLine($"Version: {view.Version}");
sb.AppendLine($"Fact Source: {view.Source}");
sb.AppendLine("");

// Joins
sb.AppendLine($"JOINS ({view.Joins.Count})");
sb.AppendLine("---------");
foreach (var join in view.Joins)
{
    sb.AppendLine($"  {join.Name,-15} -> {join.Source}");
}
sb.AppendLine("");

// Fields
sb.AppendLine($"FIELDS ({view.Fields.Count})");
sb.AppendLine("--------------");
foreach (var field in view.Fields)
{
    sb.AppendLine($"  {field.Name,-20} <- {field.Expr}");
}
sb.AppendLine("");

// Measures
sb.AppendLine($"MEASURES ({view.Measures.Count})");
sb.AppendLine("------------");
foreach (var measure in view.Measures)
{
    sb.AppendLine($"  {measure.Name,-20} = {measure.Expr}");
}

Output(sb.ToString());

输出

METRIC VIEW SUMMARY
===================

Version: 1.1
Fact Source: sales.fact.orders

JOINS (3)
---------
  product         -> sales.dim.product
  customer        -> sales.dim.customer
  date            -> sales.dim.date

FIELDS (6)
--------------
  product_name         <- product.product_name
  product_category     <- product.category
  customer_segment     <- customer.segment
  order_date           <- date.full_date
  order_year           <- date.year
  order_month          <- date.month_name

MEASURES (6)
------------
  total_revenue        = SUM(revenue)
  gross_margin         = SUM(revenue) - SUM(cost)
  order_count          = COUNT(*)
  avg_order_value      = AVG(revenue)
  revenue_to_budget    = (SUM(revenue) - SUM(budget)) / SUM(budget)
  unique_customers     = COUNT(DISTINCT customer_id)

后续步骤

现在你已经能够加载并检查 Metric View 了,你还可以:

另见