Redis是一个开源的使用C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,
因其读取速度快、也可用于消息队列使用等场景,已经成为项目中不可缺少的一部分。
本案例是通过etl engine实现redis与mysql之间的数据同步。
读redis写mysql; 读mysql写redis
事先准备一个可读写redis服务器;一个可读写mysql服务器;
读redis的key写到mysql的t_redis_info表;读mysql的t_redis_info表记录写到redis
CREATE TABLE t_redis_info (
id VARCHAR(32) NOT NULL,
caption VARCHAR(50),
tag VARCHAR(50),
memo VARCHAR(100),
writetime VARCHAR(19),
PRIMARY KEY (id)
);
INSERT INTO t_redis_info(id,caption,tag,memo,writetime) VALUES ('1','herbin_beer_550','啤酒','哈尔滨雪花550ML','2023-01-01 11:12:13');
INSERT INTO t_redis_info(id,caption,tag,memo,writetime) VALUES ('2','qingdao_beer_550','啤酒','青岛纯生550ML','2023-01-02 01:02:03');
INSERT INTO t_redis_info(id,caption,tag,memo,writetime) VALUES ('3','qingdao_beer_330','啤酒','青岛干啤330ML','2023-02-03 01:02:03');
INSERT INTO t_redis_info(id,caption,tag,memo,writetime) VALUES ('4','herbin_beer_330','啤酒','哈尔滨勇闯天涯330ML','2023-02-03 01:02:03');
INSERT INTO t_redis_info(id,caption,tag,memo,writetime) VALUES ('5','budweiser_beer_330','啤酒','美国百威330ML','2023-03-04 01:02:03');
INSERT INTO t_redis_info(id,caption,tag,memo,writetime) VALUES ('6','wahaha_water_600','纯净水','娃哈哈600ML','2023-03-04 01:02:03');
INSERT INTO t_redis_info(id,caption,tag,memo,writetime) VALUES ('7','nongfushanquan_water_600','纯净水','农夫山泉600ML','2023-03-05 01:02:03');
<?xml version="1.0" encoding="UTF-8"?>
<Graph runMode="1">
<Node id="DB_INPUT_TABLE_1" type="DB_INPUT_TABLE" fetchSize="1000" dbConnection="CONNECT_1" desc="读数据表" >
<Script name="sqlScript">
<![CDATA[ SELECT caption AS k ,CONCAT(id,';',caption,';',memo,';', tag) AS v FROM t_redis_info]]>
</Script>
</Node>
<Node id="DB_OUTPUT_TABLE_1" type="DB_OUTPUT_TABLE" dbConnection="CONNECT_1" outputFields="id;caption;memo;tag;writetime" renameOutputFields="id;caption;memo;tag;writetime" desc="写数据表" >
<Script name="sqlScript">
<![CDATA[INSERT INTO t_redis_info (id,caption,memo,tag,writetime) VALUES(?,?,?,?,?);]]>
</Script>
<BeforeOut>
<![CDATA[package ext
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"etl-engine/etl/tool/extlibs/common"
)
func RunScript(dataValue string) (result string, topErr error) {
newRows := ""
rows := gjson.Get(dataValue, "rows")
for index, row := range rows.Array() {
//增加一个字段名称为id的列
tmpStr, _ := sjson.Set(row.String(), "id", common.GetUUID() )
//将系统默认输出的value字段拆分,并创建多个字段
values := gjson.Get(row.String(),"value").String()
vArr := strings.Split(values, ";")
caption := vArr[1]
memo := vArr[2]
tag := vArr[3]
tmpStr, _ = sjson.Set(tmpStr, "caption", caption )
tmpStr, _ = sjson.Set(tmpStr, "memo", memo )
tmpStr, _ = sjson.Set(tmpStr, "tag", tag )
tmpStr, _ = sjson.Set(tmpStr, "writetime", time.Now().Format("2006-01-02 15:04:05"))
common.GetLogger().Infoln("新行数据结构tmpStr:",tmpStr)
newRows, _ = sjson.SetRaw(newRows, "rows.-1", tmpStr)
}
return newRows, nil
}]]>
</BeforeOut>
</Node>
<Node id="REDIS_WRITER_1" type="REDIS_WRITER" nameServer="127.0.0.1:16379" password="******" db="1" isGetTTL="false" patternMatchKey="true" outputFields="k;v" renameOutputFields="key;value" desc="写redis" />
<Node id="REDIS_READER_1" type="REDIS_READER" nameServer="127.0.0.1:16379" password="******" db="1" isGetTTL="false" patternMatchKey="true" keys="*" desc="读redis" />
<Line from="DB_INPUT_TABLE_1" to="REDIS_WRITER_1" type="STANDARD" order="0" metadata="METADATA_1" id="LINE_STANDARD_1"/>
<Line from="REDIS_READER_1" to="DB_OUTPUT_TABLE_1" type="STANDARD" order="1" metadata="METADATA_2" id="LINE_STANDARD_2"/>
<Metadata id="METADATA_2" >
<Field name="id" type="string" default="" nullable="true" errDefault="" dataFormat="" dataLen=""/>
<Field name="caption" type="string" default="" nullable="true" errDefault="" dataFormat="" dataLen=""/>
<Field name="memo" type="string" default="" nullable="true" errDefault="" dataFormat="" dataLen=""/>
<Field name="tag" type="string" default="" nullable="true" errDefault="" dataFormat="" dataLen=""/>
<Field name="writetime" type="string" default="" nullable="true" errDefault="" dataFormat="" dataLen=""/>
</Metadata>
<Metadata id="METADATA_1" >
<Field name="key" type="string" default="" nullable="true" errDefault="" dataFormat="" dataLen=""/>
<Field name="value" type="string" default="" nullable="true" errDefault="" dataFormat="" dataLen=""/>
</Metadata>
<Connection id="CONNECT_1" type="MYSQL" dbURL="127.0.0.1:3306" database="db1" username="root" password="******" />
</Graph>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。