教學:在 Microsoft Fabric HorizonDB 中建立資料表並載入資料

在此教學步驟中,您使用 Fabric HorizonDB SQL 編輯器建立horizon_sample結構,並以 PostgreSQL SQL 載入一個小型 Contoso 順序資料集。

Important

Fabric HorizonDB 目前處於公開預覽階段。 功能、限制與行為可能會在正式推出前發生變化。 如需更多資訊,請參閱 Microsoft Fabric 預覽功能。

Prerequisites

  • 完成 建立 HorizonDB 資料庫。
  • 打開該 contoso-orders-db 項目。
  • 以資料庫建立者身分登入,這是目前預覽版中唯一能連接 PostgreSQL 的身份。

開啟 SQL 編輯器

  1. 開啟 contoso-orders-db 的作業資料庫查詢體驗。

  2. 建立新的 SQL 查詢。

  3. 確認查詢目標是 PostgreSQL 端點,而非 SQL 分析端點。

  4. 跑步:

    SELECT
        current_database() AS database_name,
        current_user AS signed_in_user,
        clock_timestamp() AS connected_at;
    
  5. 確認結果指出資料庫 postgres,如項目連線詳細資料所示,以及建立者的有效身分。

建立結構與資料表

執行下列指令碼:

CREATE SCHEMA IF NOT EXISTS horizon_sample;

CREATE TABLE IF NOT EXISTS horizon_sample.customers (
    customer_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    customer_name text NOT NULL UNIQUE,
    segment text NOT NULL,
    created_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS horizon_sample.orders (
    order_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    order_reference text NOT NULL UNIQUE,
    customer_id bigint NOT NULL
        REFERENCES horizon_sample.customers (customer_id),
    order_date date NOT NULL DEFAULT current_date,
    status text NOT NULL
        CHECK (status IN ('open', 'processing', 'completed', 'cancelled')),
    total numeric(12, 2) NOT NULL
        CHECK (total >= 0),
    created_at timestamptz NOT NULL DEFAULT now()
);

這些資料表使用 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;

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;

ON CONFLICT 使載入對穩定客戶名稱與訂單參照具有冪等性。 重執行時會更新範例列,而不是產生重複資料。

驗證資料

跑步:

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;

結果包含三個資料列:

訂單 客戶 區段 狀態 Total
ORD-1001 Contoso 零售 Enterprise 開啟 1250.00
ORD-1002 Fabrikam 門市 商用 加工 425.50
ORD-1003 Adventure Works Enterprise 已完成 980.25

驗證聚合:

SELECT
    count(*) AS order_count,
    sum(total) AS order_total,
    min(order_date) AS earliest_order_date,
    max(order_date) AS latest_order_date
FROM horizon_sample.orders;

期望 order_count 值為 3,期望 order_total 值為 2655.75。

重新整理物件總管

  1. 重新整理資料庫物件總管。
  2. 展開結構描述集合。
  3. 展開 horizon_sample。
  4. 展開資料表集合。
  5. 選取 customers,並檢查其欄位與主鍵。
  6. 選取 orders,並檢查其欄位、主鍵、外鍵、唯一限制條件及檢查限制條件。

如果總管沒有重新整理,請透過 PostgreSQL 的元資料驗證資料表:

SELECT
    table_schema,
    table_name,
    table_type
FROM information_schema.tables
WHERE table_schema = 'horizon_sample'
ORDER BY table_name;

在教學後面,你會使用 Copy 作業載入來自符合資格的 Fabric 來源的獨立有界 customer-signals 結果。 請保持此步驟中運作中的客戶與訂單列不變,以便比較 PostgreSQL 與鏡像分析結果。

排除載入問題

徵兆 Resolution
permission denied for schema 確認您已連線為資料庫建立者,且您正在客戶擁有的綱要中建立物件。
relation does not exist 執行建立指令碼,驗證目前資料庫,並使用 horizon_sample 限定資料表名稱。
違反唯一限制條件 在變更衝突規則前,請先檢視現有客戶名稱及訂單參考。
外鍵約束失敗 在訂單前先載入客戶並確認客戶聯結。
物件瀏覽器未更新 如有需要,提交 DDL,重新整理總管,並用 information_schema 驗證。

下一步