redis 怎么批量获取数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis 怎么批量获取数据相关的知识,希望对你有一定的参考价值。

参考技术A Redis Mass Insertion
Sometimes Redis instances needs to be loaded with big amount of preexisting or user generated data in a short amount of time, so that millions of keys will be created as fast as possible.
This is called a mass insertion, and the goal of this document is to provide information about how to feed Redis with data as fast as possible.
Use the protocol, Luke
Using a normal Redis client to perform mass insertion is not a good idea for a few reasons: the naive approach of sending one command after the other is slow because you have to pay for the round trip time for every command. It is possible to use pipelining, but for mass insertion of many records you need to write new commands while you read replies at the same time to make sure you are inserting as fast as possible.
Only a small percentage of clients support non-blocking I/O, and not all the clients are able to parse the replies in an efficient way in order to maximize throughput. For all this reasons the preferred way to mass import data into Redis is to generate a text file containing the Redis protocol, in raw format, in order to call the commands needed to insert the required data.
For instance if I need to generate a large data set where there are billions of keys in the form: `keyN -> ValueN' I will create a file containing the following commands in the Redis protocol format:
SET Key0 Value0
SET Key1 Value1
...
SET KeyN ValueN

Once this file is created, the remaining action is to feed it to Redis as fast as possible. In the past the way to do this was to use the netcat with the following command:
(cat data.txt; sleep 10) | nc localhost 6379 > /dev/null

However this is not a very reliable way to perform mass import because netcat does not really know when all the data was transferred and can't check for errors. In the unstable branch of Redis at github the redis-cli utility supports a new mode called pipe mode that was designed in order to perform mass insertion. (This feature will be available in a few days in Redis 2.6-RC4 and in Redis 2.4.14).
Using the pipe mode the command to run looks like the following:
cat data.txt | redis-cli --pipe

That will produce an output similar to this:
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 1000000

The redis-cli utility will also make sure to only redirect errors received from the Redis instance to the standard output.

批量删除redis数据库中的key

在redis数据库中,如果大量以某些字段开头或结尾的key,一般都会用到命令keys进行模糊匹配。但是当我们想删除批量指定的keys,却犯愁了,因为redis没有提供相关的命令。那我们怎么操作能实现预期的效果呢?

(1) 删除单个key

127.0.0.1:6379> del key

如果知道有限多个key的名字,以下操作也可以实现批量操作

127.0.0.1:6379> del key1 key2 key3 ....

当key的数量达到一定数量时,这个方法明显时不现实的。

注意:redis命令行默认使用空格来分割key值,如果刚好某个key的明早带有空格,则对于有空格的key需要用引号包含起来:

127.0.0.1:6379>del ”first key" "second key"

(2) 批量删除key

批量删除key,我们可以借助Linux 的 xargs 指令来完成这个动作

127.0.0.1:6379>redis-cli keys "a*" | xargs redis-cli del
说明:如果redis-cli没有设置成系统变量,需要指定redis-cli的完整路径  
比如:/usr/local/redis/redis-cli keys "a*" | xargs /usr/local/redis/redis-cli del  

如果要指定 Redis 数据库访问密码,使用下面的命令

127.0.0.1:6379>redis-cli -a password keys "a*" | xargs redis-cli -a password del 

如果要访问 Redis 中特定的数据库,使用下面的命令

//下面的命令指定数据序号为0,即默认数据库  
127.0.0.1:6379>redis-cli -n 0 keys "a*" | xargs redis-cli -n 0 del

(3) 删除所有Key
删除所有Key,可以使用Redis的flushdb和flushall命令

//删除当前数据库中的所有Key  
flushdb  
//删除所有数据库中的key  
flushall 

 

以上是关于redis 怎么批量获取数据的主要内容,如果未能解决你的问题,请参考以下文章

如何获取redis内的所有内容

如何使用Java来获取redis中某个key的所有数据

golang 根据keys获取的key列表批量删除

我是如何解决redis集群批量获取的效率问题的

如何获取redis中String类型的全部key值

右手Redis(快速入门)