本文將一個精簡的 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;
重新執行載入並確認數量維持三和三。
使用事務來載入應用程式
對於相關變更,請將整個載入包在交易中:
BEGIN;
-- Run the customer and order upsert statements.
COMMIT;
驗證失敗時使用 ROLLBACK; 。 對於較大的匯入,載入暫存表,驗證型別與商務鍵,然後以單一冪等交易發佈。 在未核對業務鍵值之前,不要在不確定的提交後重播交易。
清理
若要移除範例資料表中的所有資料列,但保留資料表,請執行以下指令。 這也會刪除您新增的任何非範例列;若需要保留這些列,請勿執行:
TRUNCATE TABLE
horizon_sample.orders,
horizon_sample.customers
RESTART IDENTITY;
執行命令前請先檢視資料表名稱。