Surreal-Sync —— 資料遷移與同步
Surreal-Sync 是一個 CLI 工具,可將各種資料庫來源的資料遷移到 SurrealDB,並透過變更資料捕獲(CDC)實現完整同步與增量同步。
支援的來源
| 來源 | 完整同步 | 增量 CDC | 方法 |
|---|---|---|---|
| MongoDB | 是 | 是 | 變更串流 |
| MySQL | 是 | 是 | 觸發器式 CDC + 序列檢查點 |
| PostgreSQL(觸發器) | 是 | 是 | 觸發器式 CDC + 序列檢查點 |
| PostgreSQL(wal2json) | 是 | 是 | 基於 wal2json 外掛的邏輯複寫 |
| Neo4j | 是 | 是 | 時間戳追蹤 |
| JSONL 檔案 | 是 | 不適用 | 從 JSON Lines 批次匯入 |
| Apache Kafka | 是 | 是 | 消費者訂閱與去重 |
快速開始
# 安裝 surreal-sync(Rust 二進位檔案)
cargo install surreal-sync
# 從 PostgreSQL 進行完整同步(觸發器式)
surreal-sync from postgres trigger-full \
--connection-string "postgresql://user:pass@localhost/mydb" \
--surreal-endpoint "http://localhost:8000" \
--surreal-username root \
--surreal-password root \
--to-namespace prod \
--to-database main
# 從 PostgreSQL 進行增量 CDC(wal2json)
surreal-sync from postgres wal2json \
--connection-string "postgresql://user:pass@localhost/mydb" \
--surreal-endpoint "http://localhost:8000" \
--surreal-username root \
--surreal-password root \
--to-namespace prod \
--to-database main
# 從 MongoDB 進行完整同步
surreal-sync from mongo full \
--connection-string "mongodb://localhost:27017/mydb" \
--surreal-endpoint "http://localhost:8000" \
--surreal-username root \
--surreal-password root \
--to-namespace prod \
--to-database main
# 從 JSONL 進行批次匯入
surreal-sync from jsonl import \
--file data.jsonl \
--surreal-endpoint "http://localhost:8000" \
--surreal-username root \
--surreal-password root \
--to-namespace prod \
--to-database main
# 從 Kafka 進行消費
surreal-sync from kafka consume \
--bootstrap-servers "localhost:9092" \
--topic my-events \
--surreal-endpoint "http://localhost:8000" \
--surreal-username root \
--surreal-password root \
--to-namespace prod \
--to-database main
CLI 模式
surreal-sync from <來源> <命令> \
--connection-string [連線字串] \
--surreal-endpoint [SURREAL 端點] \
--surreal-username [SURREAL 使用者名稱] \
--surreal-password [SURREAL 密碼] \
--to-namespace <命名空間> \
--to-database <資料庫>
主要功能
- 自動推導結構並建立 SurrealDB 資料表
- 從來源主鍵進行記錄 ID 對應
- 關係萃取與圖形邊建立
- 可設定的批次大小與平行度
- 可恢復的同步,附帶檢查點追蹤
- Kafka 消費者去重
完整文件
請參閱主要技能的規則檔案以獲得完整指引:
- rules/surreal-sync.md —— 來源配置、結構對應、CDC 設定、衝突解決與生產部署
- surrealdb/surreal-sync —— 上游儲存庫