mysql数据导入redis

先编写sql脚本,查询好需要导入redis的数据,同时在脚本中设定好redis的类型;最外层的查询需要设定好查询的字段数量,key value各算一个。

我用的是hash结构,将表明以key的形式存储,字段+对应的值以json格式存储,脚本在图片下方

脚本上传到服务器后使用此命令导入数据(用户名密码、脚本路径自行更改):mysql -uroot -p123456 -h 127.0.0.1 database--skip-column-names --raw <data.sql | /usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 -a mima --pipe

SELECT CONCAT(
"*4\r\n",
'$', LENGTH(redis_cmd), '\r\n',
redis_cmd, '\r\n',
'$', LENGTH(redis_key), '\r\n',
redis_key, '\r\n',
'$',LENGTH(hkey),'\r\n',hkey,'\r\n',
'$',LENGTH(hval),'\r\n',hval,'\r\n'
)
FROM(
SELECT
'HSET' AS redis_cmd,
'tab_product' AS redis_key,
a.product_id AS hkey,
CONCAT(
'{"id":"', a.id,'"',
',"productId":"', a.product_id,'"',
',"shopId":"', a.shop_id,'"',
',"title":"', a.title,'"',
',"originalPrice":"', a.original_price,'"',
',"price":"', a.price,'"',
',"info":"', a.info,'"',
',"createTime":"', a.create_time,'"',
',"updateTime":"', ifnull(a.update_time,''),'"',
',"status":"', a.status,'"',
',"img":"', a.img,'"',
',"upperLimit":"', a.upper_limit,'"',
',"endTime":"', ifnull(a.end_time,''),'"',
',"refushReason":"', ifnull(a.refush_reason,''),'"',
',"putawayDate":"', ifnull(a.putaway_date,''),'"',
',"tagId":"', ifnull(c.tag_id,''),'"',
',"tagName":"', ifnull(d.title,''),'"',
',"shopName":"', ifnull(e.shop_name,''),'"',
',"address":"', ifnull(e.address,''),'"',
',"businessHours":"', ifnull(e.business_hours,''),'"',
',"pension":"', round(ifnull(e.pension_scale,0) * a.price, 2),'"',
',"productImageList":', ifnull(CONCAT('[',GROUP_CONCAT('{"img":"',b.img, '"}'),']'),''),'}'
) AS hval
FROM `tab_product` a
LEFT JOIN tab_product_image b on a.product_id = b.product_id and b.state = 0
LEFT JOIN tab_product_tag c on c.product_id = a.product_id and c.state = 0
LEFT JOIN tab_tag d on d.id = c.tag_id and d.state = 0
LEFT JOIN shop.tab_shop e on e.shop_id = a.shop_id
group by a.id
) AS t

这是HSET结构,用到的改一下上面的就可以了

猜你喜欢

转载自www.cnblogs.com/gqymy/p/12516333.html