redis-cli pipe方式导入mysql sql查询导出redisProtocol格式数据
mysql的数据导入到redis中可以通过编写脚本来实现,先连接db然后将查询出的数据批量写入到redis中,但redis pipe的方式更加简便高效。
分享下最代码的相关脚本代码片段:
linux下直接执行
1
mysql -uroot -p111111 javaniu --skip-column-names --default-character-set=utf8 --raw < eve_uid_tp_scid_st_t.tpl| redis-cli -p 6379 --pipe
windows下
1
mysql -uroot -p111111 javaniu --skip-column-names --default-character-set=utf8 --raw < c:/eve_uid_tp_scid_st_t.tpl | redis-cli -p 6379 --pipe
linux下导出文件分隔符是,而windows下是
linux下脚本
01
SELECT CONCAT(
02
"*4",
03
'$', LENGTH(redis_cmd), '',
04
redis_cmd, '',
05
'$', LENGTH(eve_st), '',
06
eve_st, '',
07
'$', LENGTH(score), '',
08
score, '',
09
'$', LENGTH(member), '',
10
member, ''
11
)
12
FROM (
13
SELECT
14
'zadd' as redis_cmd,
15
concat('eve_',status) as eve_st,
16
id as member,
17
unix_timestamp(create_time) AS score
18
FROM event where status=0
19
) AS eve
windows下脚本
01
SELECT CONCAT(
02
"*4",
03
'$', LENGTH(redis_cmd), '',
04
redis_cmd, '',
05
'$', LENGTH(eve_st), '',
06
eve_st, '',
07
'$', LENGTH(score), '',
08
score, '',
09
'$', LENGTH(member), '',
10
member
11
)
12
FROM (
13
SELECT
14
'zadd' as redis_cmd,
15
concat('eve_',status) as eve_st,
16
id as member,
17
unix_timestamp(create_time) AS score
18
FROM event where status=0
19
) AS eve
redis Protocol格式说明如下:
01
#表示有4个参数
02
*4
03
#表示命令长度
04
$LENGTH(redis_cmd)
05
#命令
06
redis_cmd
07
#key长度
08
$LENGTH(eve_st)
09
#key值
10
eve_st
11
#value长度
12
$LENGTH(score)
13
#value值
14
score
执行结果如下图: