Table of Contents

在指标视图中重命名对象

本操作指南演示如何使用“复制-修改”模式,对指标视图的维度进行批量重命名。 同样的模式也适用于指标视图中的所有集合。

对这些代码示例的 Metric View 进行反序列化

本操作指南使用一个示例电商 Metric View 来表示销售数据,其中三个维度表(product、customer、date)连接到一个事实表(orders)。 如果你想在阅读本操作指南其余部分时跟着代码一起操作,请先运行下面的代码片段

SemanticBridge.MetricView.Deserialize("""
    version: 0.1
    source: sales.fact.orders
    joins:
      - name: product
        source: sales.dim.product
        on: source.product_id = product.product_id
      - name: customer
        source: sales.dim.customer
        on: source.customer_id = customer.customer_id
      - name: date
        source: sales.dim.date
        on: source.order_date = date.date_key
    dimensions:
      - name: product_name
        expr: product.product_name
      - name: product_category
        expr: product.category
      - name: customer_segment
        expr: customer.segment
      - name: order_date
        expr: date.full_date
      - name: order_year
        expr: date.year
      - name: order_month
        expr: date.month_name
    measures:
      - name: total_revenue
        expr: SUM(revenue)
      - name: order_count
        expr: COUNT(order_id)
      - name: avg_order_value
        expr: AVG(revenue)
      - name: unique_customers
        expr: COUNT(DISTINCT customer_id)
    """);

复制-修改模式

由于指标视图的维度名称是集合中对象的属性,最简单的做法是:

  1. 创建新的指标视图 Dimension 对象,并使用修改后的名称
  2. 清空原始集合
  3. 将新对象添加回集合

这样可以避免在迭代时修改对象所带来的问题。

将 snake_case 转换为 Title Case

将指标视图维度名称从 product_name 转换为 Product Name

using System.Globalization;
using MetricView = TabularEditor.SemanticBridge.Platforms.Databricks.MetricView;

var view = SemanticBridge.MetricView.Model;
var textInfo = CultureInfo.CurrentCulture.TextInfo;

var sb = new System.Text.StringBuilder();
sb.AppendLine("BEFORE");
sb.AppendLine("------");
foreach (var dim in view.Dimensions)
{
    sb.AppendLine($"  {dim.Name}");
}

// Create renamed dimensions
var renamed = view.Dimensions.Select(dim => new MetricView.Dimension
{
    Name = textInfo.ToTitleCase(dim.Name.Replace('_', ' ')),
    Expr = dim.Expr
}).ToList();

// Replace the collection
view.Dimensions.Clear();
foreach (var dim in renamed)
{
    view.Dimensions.Add(dim);
}

sb.AppendLine();
sb.AppendLine("AFTER");
sb.AppendLine("-----");
foreach (var dim in view.Dimensions)
{
    sb.AppendLine($"  {dim.Name}");
}

Output(sb.ToString());

输出:

BEFORE
------
  product_name
  product_category
  customer_segment
  order_date
  order_year
  order_month

AFTER
-----
  产品名称
  产品类别
  客户分段
  订单日期
  订单年份
  订单月份

使用映射字典进行重命名

使用查找表应用特定重命名:

using MetricView = TabularEditor.SemanticBridge.Platforms.Databricks.MetricView;

var view = SemanticBridge.MetricView.Model;

// 定义重命名映射
var renames = new Dictionary<string, string>
{
    { "product_name", "Product" },
    { "product_category", "Category" },
    { "customer_segment", "Segment" },
    { "order_date", "Date" },
    { "order_year", "Year" },
    { "order_month", "Month" }
};

var sb = new System.Text.StringBuilder();

// 创建已重命名的维度
var renamed = view.Dimensions
    .Select(
        dim => new MetricView.Dimension
        {
            Name = renames.TryGetValue(dim.Name, out var newName) ? newName : dim.Name,
            Expr = dim.Expr
        })
    .ToList();

// 替换集合
view.Dimensions.Clear();
foreach (var dim in renamed)
{
    view.Dimensions.Add(dim);
}

sb.AppendLine("重命名后的维度:");
sb.AppendLine("-------------------");
foreach (var dim in view.Dimensions)
{
    sb.AppendLine($"  {dim.Name,-20} <- {dim.Expr}");
}

Output(sb.ToString());

输出:

重命名后的维度:
-------------------
  Product              <- product.product_name
  Category             <- product.category
  分段                  <- customer.segment
  Date                 <- date.full_date
  Year                 <- date.year
  Month                <- date.month_name

另见