将示例数据加载到 Microsoft Fabric HorizonDB 中

本文将一个紧凑的 PostgreSQL 原生数据集加载到 Fabric HorizonDB 中。 脚本可重运行,并配合ON CONFLICT使用稳定的业务密钥。

Important

Fabric HorizonDB 处于公开预览阶段。 功能、限制和行为可能会在正式发布前发生变化。 有关详细信息,请参阅 Microsoft Fabric 预览功能。

Prerequisites

  • 一个 HorizonDB 数据库。
  • 来自 “创建表”的表。
  • 以数据库创建者身份登录。 在当前预览版中,只有此 Microsoft Entra 身份可以连接到 PostgreSQL。

插入客户

打开一个新的 PostgreSQL 查询并运行:

INSERT INTO horizon_sample.customers (customer_name, segment)
VALUES
    ('Contoso Retail', 'Enterprise'),
    ('Fabrikam Stores', 'Commercial'),
    ('Adventure Works', 'Enterprise')
ON CONFLICT (customer_name) DO UPDATE
SET segment = EXCLUDED.segment;

唯一的客户名称可作为稳定的示例业务键。

在专用测试工作区中使用合成数据。 在创建仓库信号之前,请记录生成的客户 ID;身份值不保证会从 1 开始,也不保证在重新运行后保持连续。

插入订单

INSERT INTO horizon_sample.orders (
    order_reference,
    customer_id,
    order_date,
    status,
    total
)
SELECT
    source.order_reference,
    customer.customer_id,
    source.order_date,
    source.status,
    source.total
FROM (
    VALUES
        ('ORD-1001', 'Contoso Retail', current_date, 'open', 1250.00::numeric),
        ('ORD-1002', 'Fabrikam Stores', current_date - 1, 'processing', 425.50::numeric),
        ('ORD-1003', 'Adventure Works', current_date - 2, 'completed', 980.25::numeric)
) AS source (
    order_reference,
    customer_name,
    order_date,
    status,
    total
)
JOIN horizon_sample.customers AS customer
  ON customer.customer_name = source.customer_name
ON CONFLICT (order_reference) DO UPDATE
SET
    customer_id = EXCLUDED.customer_id,
    order_date = EXCLUDED.order_date,
    status = EXCLUDED.status,
    total = EXCLUDED.total;

联接会根据稳定的客户名称解析生成的客户ID。 重新运行脚本时,更新现有订单而不是添加重复订单。

验证加载

SELECT
    order_row.order_reference,
    customer.customer_name,
    customer.segment,
    order_row.order_date,
    order_row.status,
    order_row.total
FROM horizon_sample.orders AS order_row
JOIN horizon_sample.customers AS customer
  ON customer.customer_id = order_row.customer_id
ORDER BY order_row.order_reference;

预期结果包含三笔订单,每个客户对应一笔。

验证计数:

SELECT
    (SELECT count(*) FROM horizon_sample.customers) AS customer_count,
    (SELECT count(*) FROM horizon_sample.orders) AS order_count;

重新运行加载,确认计数仍分别为 3 和 3。

使用事务进行应用程序加载

对于相关更改,请在事务中对完整加载进行封装:

BEGIN;

-- Run the customer and order upsert statements.

COMMIT;

验证失败时使用ROLLBACK;。 对于较大的导入,加载一个暂存表,验证类型和业务键,然后用一个幂等事务发布。 在提交结果未定的情况下,未经核对业务键,不要重放事务。

清理

要在保留表格的同时移除示例表中的所有行,请运行以下命令。 这也会删除你添加的任何非样本行;如果需要保留它们,请不要运行:

TRUNCATE TABLE
    horizon_sample.orders,
    horizon_sample.customers
RESTART IDENTITY;

在运行命令之前,请先检查表名称。

后续步骤

查询示例数据。