Redis 值更新 [英] Redis value update
问题描述
我目前有一个 redis 数据集,其键将 id 和值表示为 json .我需要在 json 中为每个用户 ID(键)添加一个新实体.是否有任何现有的开源工具?我应该如何更新 100 万个数据密钥.
Im currently having a redis data set with key representing ids and values as a json . I need to add a new entity in the json for every userid(keys). Is there any existing opensource tool? what is the way i should proceed to update for 1M keys of data.
推荐答案
有几种可能:
这是一些使用 Redis 2.6 Lua 脚本执行此操作的伪代码.
Here's some pseudo code for doing this with Redis 2.6 Lua scripting.
for userid in users:
EVAL 'local obj = cjson.decode(redis.call("GET", KEY[1])); obj.subobj.newjsonkey = ARGV[1]; redis.call("SET", KEY[1], cjson.encode(obj));' 1 userid "new value!"
简而言之,您可能需要停止服务并使用 GET 和 SET 来执行此操作,因为您可能没有适当的锁定机制.如果您可以强制锁定,请参阅 http://redis.io/commands/setnx
有一些工具可用于更新 rdb.https://github.com/sripathikrishnan/redis-rdb-tools https://github.com/nrk/redis-rdb
There are a few tools for updating an rdb. https://github.com/sripathikrishnan/redis-rdb-tools https://github.com/nrk/redis-rdb
注意,这个答案适用于我对以下问题的回答:使用嵌套对象在 Redis 中?
Note, this answer was adapted to my answer to: Working with nested objects in Redis?
这篇关于Redis 值更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!