在此教學步驟中,您將從合格的 Fabric 來源將有界的客戶訊號結果載入 Fabric HorizonDB。 複製作業會寫入暫存表。 你先驗證載入,然後將其發佈到本地的提供資料表。
Important
Fabric HorizonDB 目前處於公開預覽階段。 功能、限制與行為可能會在正式推出前發生變化。 公開預覽不保證此目的地提供增量變更擷取、原始碼刪除傳播、衝突處理、結構演進、排程、檢查點或自動重播。 如需更多資訊,請參閱 Microsoft Fabric 預覽功能。
Prerequisites
重複使用上一個步驟中建立於
contoso-signals-warehouse中的dbo.customer_signals,並使用下列欄位:Column 必填值 customer_id一個與 horizon_sample.customers中客戶相符的值。risk_score一個 0 到 100 的數值。 recommended_action客戶的非空動作。 calculated_at來源計算時間戳記。 load_batch_id此受限負載的穩定識別碼。 擁有讀取來源並建立複製作業的權限。
以 PostgreSQL 設定與複製工作目的地連線的資料庫建立者身分登入。 其他使用者與安全性主體無法在目前預覽中連線。
使用開發或測試來源。 教學中不要使用敏感的生產資料。
如果您跳過了資料聯結步驟,請先完成「建立倉儲來源」。 不要在鏡像 SQL 分析端點中建立來源資料表,因為該端點為唯讀。
建立暫存與服務表
在 contoso-orders-db PostgreSQL 端點上執行:
CREATE SCHEMA IF NOT EXISTS analytics_ops;
CREATE TABLE IF NOT EXISTS analytics_ops.customer_signals (
customer_id bigint PRIMARY KEY,
risk_score numeric(5, 2) NOT NULL,
recommended_action text NOT NULL,
calculated_at timestamptz NOT NULL,
loaded_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS analytics_ops.customer_signals_stage (
customer_id bigint NOT NULL,
risk_score numeric(5, 2) NOT NULL,
recommended_action text NOT NULL,
calculated_at timestamptz NOT NULL,
load_batch_id text NOT NULL,
loaded_at timestamptz NOT NULL DEFAULT now()
);
複製作業會寫入 customer_signals_stage。 應用程式查詢僅在驗證與發佈後使用 customer_signals 。
準備有界載入
在教學執行前立即清空暫存表:
TRUNCATE TABLE analytics_ops.customer_signals_stage;
不要截斷服務資料表。
建立並執行複製作業
依照已發布的 複製工作建立指引,並設定此有界載入:
建立一個名為
load-contoso-customer-signals的複製工作。選擇
contoso-signals-warehouse倉庫和dbo.customer_signals表格。僅選擇
customer_id、risk_score、recommended_action、calculated_at和load_batch_id。選擇 適用於 PostgreSQL 的 Azure 資料庫 目標連接器。
從項目
contoso-orders-db的 Visual Studio Code 窗格複製 PostgreSQL 主機,並使用資料庫postgres。將 驗證類型 設為 組織帳戶,並以資料庫建立者身分登入。 保持勾選「使用加密連線」。
在讀取方法頁面選擇 「完整複本」。
選擇
analytics_ops.customer_signals_stage為目標資料表。將每個來源欄位對應到同名的目標欄位。
保持
loaded_at未對應,讓 PostgreSQL 預設提供載入時間,並確認calculated_at代表 UTC。選取 [儲存 + 執行]。
監控執行,並記錄執行 ID、列數、持續時間及錯誤。
成功的複製作業表示資料列已到達暫存區。 它並不能證明這些資料列是否有效或已發佈。
驗證暫存資料
請將 <batch-id> 替換為來源提供的批次識別碼(preview-demo-001 針對教學倉庫):
SELECT
count(*) AS staged_rows,
count(DISTINCT customer_id) AS distinct_customers,
min(calculated_at) AS oldest_calculation,
max(calculated_at) AS newest_calculation
FROM analytics_ops.customer_signals_stage
WHERE load_batch_id = '<batch-id>';
檢查重複與與教學合約不符的值:
SELECT
stage.customer_id,
count(*) AS occurrences
FROM analytics_ops.customer_signals_stage AS stage
WHERE stage.load_batch_id = '<batch-id>'
GROUP BY stage.customer_id
HAVING count(*) > 1;
SELECT
stage.customer_id,
stage.risk_score,
stage.recommended_action,
stage.calculated_at
FROM analytics_ops.customer_signals_stage AS stage
LEFT JOIN horizon_sample.customers AS customer
ON customer.customer_id = stage.customer_id
WHERE stage.load_batch_id = '<batch-id>'
AND (
customer.customer_id IS NULL
OR stage.risk_score NOT BETWEEN 0 AND 100
OR btrim(stage.recommended_action) = ''
);
除非 staged_rows 且 distinct_customers 兩者都符合預期的來源客戶數(3 針對未變更的教學原始碼),且兩個驗證查詢都未回傳任何列,否則不要發佈批次。
發佈最新數值
跑步:
BEGIN;
INSERT INTO analytics_ops.customer_signals (
customer_id,
risk_score,
recommended_action,
calculated_at,
loaded_at
)
SELECT DISTINCT ON (stage.customer_id)
stage.customer_id,
stage.risk_score,
stage.recommended_action,
stage.calculated_at,
now()
FROM analytics_ops.customer_signals_stage AS stage
WHERE stage.load_batch_id = '<batch-id>'
ORDER BY
stage.customer_id,
stage.calculated_at DESC
ON CONFLICT (customer_id) DO UPDATE
SET
risk_score = EXCLUDED.risk_score,
recommended_action = EXCLUDED.recommended_action,
calculated_at = EXCLUDED.calculated_at,
loaded_at = EXCLUDED.loaded_at
WHERE analytics_ops.customer_signals.calculated_at <= EXCLUDED.calculated_at;
COMMIT;
穩定客戶 ID 與 ON CONFLICT 子句可防止重複服務列,當您對同一已驗證批次重執行發佈步驟時。 在發佈之前,請確認該批次是否為最新。
查詢已操作化的資料
SELECT
customer.customer_id,
customer.customer_name,
customer.segment,
signal.risk_score,
signal.recommended_action,
signal.calculated_at
FROM horizon_sample.customers AS customer
JOIN analytics_ops.customer_signals AS signal
ON signal.customer_id = customer.customer_id
ORDER BY signal.risk_score DESC, customer.customer_name;
此查詢會讀取本地 PostgreSQL 資料表。 其新鮮度取決於 Copy 作業與發佈流程。
預期結果
load-contoso-customer-signals複製作業會將有界批次載入暫存,驗證成功,並analytics_ops.customer_signals包含用於匹配 Contoso 教學客戶 ID 的已發佈值。