使用Lua腳本可以實現更復雜的數據處理邏輯,go-mysql-transfer支持Lua5.1語法
# **示例**
t_user表,數據如下:

## **示例一**
Lua腳本:
```
-- t_user_mongo.lua
local ops = require("mongodbOps") --加載mongodb操作模塊
local row = ops.rawRow() --當前數據庫的一行數據,table類型
local action = ops.rawAction() --當前數據庫事件,包括:insert、update、delete
local result = {} -- 定義一個table
for k, v in pairs(row) do
if k ~="ID" then -- 列名不為ID
result[k] = v
end
end
local id = row["ID"] --獲取ID列的值
result["_id"] = id -- _id為MongoDB的主鍵標識
if action == "insert" then -- 新增事件
ops.INSERT("t_user",result) -- 新增,第一個參數為collection名稱,string類型;第二個參數為要修改的數據,talbe類型
elseif action == "delete" then -- 刪除事件 -- 修改事件
ops.DELETE("t_user",id) -- 刪除,第一個參數為collection名稱(string類型),第二個參數為ID
else -- 修改事件
ops.UPDATE("t_user",id,result) -- 修改,第一個參數為collection名稱,string類型;第二個參數為ID;第三個參數為要修改的數據,talbe類型
-- ops.UPSERT("t_user",id,result) -- 修改或新增,第一個參數為collection名稱,string類型;第二個參數為ID;第三個參數為要修改的數據,talbe類型
end
```
引入Lua腳本:
```
rule:
-
schema: eseap #數據庫名稱
table: t_user #表名稱
lua_file_path: lua/t_user_mongo.lua #lua腳本文件
mongodb_database: transfer #mongodb database不能為空
```
同步到MongoDB的數據如下:

## **示例二**
Lua腳本:
```
-- t_user_mongo2.lua
local ops = require("mongodbOps") --加載mongodb操作模塊
local row = ops.rawRow() --當前數據庫的一行數據,table類型
local action = ops.rawAction() --當前數據庫事件,包括:insert、update、delete
local id = row["ID"] --獲取ID列的值
local userName = row["USER_NAME"] --獲取USER_NAME列的值
local password = row["PASSWORD"] --獲取USER_NAME列的值
local createTime = row["CREATE_TIME"] --獲取CREATE_TIME列的值
local result = {} -- 定義一個table
result["_id"] = id -- _id為MongoDB的主鍵標識
result["account"] = userName
result["password"] = password
result["createTime"] = createTime
result["source"] = "binlog" -- 數據來源
if action == "insert" then -- 只監聽insert事件
ops.INSERT("t_user",result) -- 新增,第一個參數為collection名稱,string類型;第二個參數為要修改的數據,talbe類型
end
```
引入Lua腳本:
```
rule:
-
schema: eseap #數據庫名稱
table: t_user #表名稱
lua_file_path: lua/t_user_mongo2.lua #lua腳本文件
mongodb_database: transfer #mongodb database不能為空
```
同步到MongoDB的數據如下:

# **mongodbOps模塊**
提供的方法如下:
1. INSERT: 插入操作,如:ops.INSERT(collection,result)。參數collection為集合名稱,字符串類型;參數result為要插入的數據,table類型
2. UPDATE: 修改操作,如:ops.UPDATE(collection,id,result)。參數collection為集合名稱,字符串類型;參數id為要修改的數據主鍵,類型不限;參數result為要修改的數據,table類型
3. DELETE: 刪除操作,如:ops.DELETE(collection,id)。參數collection為集合名稱,字符串類型;參數id為要修改的數據主鍵,類型不限;
4. UPSERT: 修改或插入操作,如:ops.UPSERT(collection,id,result)。參數collection為集合名稱,字符串類型;參數id為要修改的數據主鍵,類型不限;參數result為要修改的數據,table類型;如果集合中有數據則修改,否則新增
- 簡介
- 部署運行
- 高可用集群
- 同步數據到Redis
- Redis配置
- 基于規則同步
- 基于Lua腳本同步
- 同步數據到MongoDB
- MongoDB配置
- 基于規則同步
- 基于Lua腳本同步
- 同步數據到RocketMQ
- RocketMQ配置
- 基于規則同步
- 基于Lua腳本同步
- 同步數據到Kafka
- Kafka配置
- 基于規則同步
- 基于Lua腳本同步
- 同步數據到RabbitMQ
- RabbitMQ配置
- 基于規則同步
- 基于Lua腳本同步
- 同步數據到Elasticsearch
- Elasticsearch配置
- 基于規則同步
- 基于Lua腳本同步
- 全量數據導入
- Lua腳本
- 基礎模塊
- Json模塊
- HttpClient模塊
- DBClient模塊
- 監控
- 性能測試
- 常見問題
- 更新記錄開發計劃