Is there a Redis command for fetching all keys in the database? I have seen some python-redis libraries fetching them. But was wondering if it is possible from redis-client.
是否有一个Redis命令来获取数据库中的所有键?我已经看到了一些python-redis的库来获取它们。但想知道是否可以从redisclient。
8 个解决方案
#1
502
Try to look at KEYS
command. KEYS *
will list all keys stored in redis.
尝试查看KEYS命令。KEYS *将列出在redis中存储的所有密钥。
EDIT: please note the warning at the top of KEYS
documentation page:
编辑:请注意关键文档页面顶部的警告:
Time complexity: O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length.
时间复杂度:O(N), N是数据库中键的数目,假设数据库中的键名和给定的模式长度有限。
UPDATE (V2.8 or greater): SCAN
is a superior alternative to KEYS
, in the sense that it does not block the server nor does it consume significant resources. Prefer using it.
更新(V2.8或更高版本):扫描是密钥的一种高级选择,因为它不会阻塞服务器,也不会消耗大量资源。更喜欢使用它。
#2
100
Updated for Redis 2.8 and above
更新为Redis 2.8和以上。
As noted in the comments of previous answers to this question, KEYS
is a potentially dangerous command since your Redis server will be unavailable to do other operations while it serves it. Another risk with KEYS
is that it can consume (dependent on the size of your keyspace) a lot of RAM to prepare the response buffer, thus possibly exhausting your server's memory.
正如前面对这个问题的回答所指出的那样,密钥是一个潜在的危险命令,因为您的Redis服务器在服务它时将无法执行其他操作。密钥的另一个风险是它可以消耗大量RAM(取决于您的密钥空间的大小)来准备响应缓冲区,从而可能耗尽服务器的内存。
Version 2.8 of Redis had introduced the SCAN family of commands that are much more polite and can be used for the same purpose.
Redis的版本2.8引入了更有礼貌的命令的扫描族,并且可以用于相同的目的。
The CLI also provides a nice way to work with it:
CLI还提供了一种很好的处理方法:
$ redis-cli --scan --pattern '*'
#3
17
It can happen that using redis-cli, you connect to your remote redis-server, and then the command:
可以使用redis-cli,连接到远程的redis-server,然后命令:
KEYS *
is not showing anything, or better, it shows:(empty list or set)
没有显示任何东西,或者更好,它显示:(空列表或集合)
If you are absolutely sure that the Redis server you use is the one you have the data, then maybe your redis-cli is not connecting to the Redis correct database instance.
如果您完全确定您使用的Redis服务器是您拥有的数据,那么可能您的redis-cli并没有连接到Redis正确的数据库实例。
As it is mentioned in the Redis docs, new connections connect as default to the db 0.
正如在Redis文档中提到的那样,新的连接连接默认为db 0。
In my case KEYS
command was not retrieving results because my database was 1. In order to select the db you want, use SELECT.
The db is identified by an integer.
在我的情况下,KEYS命令没有检索结果,因为我的数据库是1。为了选择你想要的数据库,使用select。db由一个整数标识。
SELECT 1
KEYS *
I post this info because none of the previous answers was solving my issue.
我发布这个信息是因为之前没有一个答案能解决我的问题。
#4
13
Take a look at following Redis Cheat Sheet. To get a subset of redis keys with the redis-cli i use the command
看看下面的Redis Cheat Sheet。要获取redis的子集,使用redis-cli命令。
KEYS "prefix:*"
#5
11
-->Get all keys from redis-cli
->从redis-cli获得所有密钥。
-redis 127.0.0.1:6379> keys *
-->Get list of patterns
- - >模式列表
-redis 127.0.0.1:6379> keys d??
This will produce keys which start by 'd' with three characters.
这将产生由“d”开头的三个字符。
-redis 127.0.0.1:6379> keys *t*
This wil get keys with matches 't' character in key
这个wil可以在关键字中找到匹配“t”字符的键。
-->Count keys from command line by
-从命令行到>计数键。
-redis-cli keys * |wc -l
-->Or you can use dbsize
——>或者可以使用dbsize。
-redis-cli dbsize
#6
7
Yes, you can get all keys by using this
是的,你可以用这个来得到所有的钥匙。
var redis = require('redis');
redisClient = redis.createClient(redis.port, redis.host);
redisClient.keys('*example*', function (err, keys) {
})
#7
2
SCAN doesn't require the client to load all the keys into memory like KEYS does. SCAN gives you an iterator you can use. I had a 1B records in my redis and I could never get enough memory to return all the keys at once.
扫描不需要客户机像密钥那样将所有键加载到内存中。扫描给你一个你可以使用的迭代器。我的redis有一个1B记录,而且我永远无法获得足够的内存来一次返回所有的密钥。
Here is a python snippet to get all keys from the store matching a pattern and delete them:
下面是一个python代码片段,可以从存储模式中获取所有密钥,并将它们删除:
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
for key in r.scan_iter("key_pattern*"):
print key
#8
1
redis-cli -h <host> -p <port> keys *
where * is the pattern to list all keys
列出所有键的模式在哪里?
#1
502
Try to look at KEYS
command. KEYS *
will list all keys stored in redis.
尝试查看KEYS命令。KEYS *将列出在redis中存储的所有密钥。
EDIT: please note the warning at the top of KEYS
documentation page:
编辑:请注意关键文档页面顶部的警告:
Time complexity: O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length.
时间复杂度:O(N), N是数据库中键的数目,假设数据库中的键名和给定的模式长度有限。
UPDATE (V2.8 or greater): SCAN
is a superior alternative to KEYS
, in the sense that it does not block the server nor does it consume significant resources. Prefer using it.
更新(V2.8或更高版本):扫描是密钥的一种高级选择,因为它不会阻塞服务器,也不会消耗大量资源。更喜欢使用它。
#2
100
Updated for Redis 2.8 and above
更新为Redis 2.8和以上。
As noted in the comments of previous answers to this question, KEYS
is a potentially dangerous command since your Redis server will be unavailable to do other operations while it serves it. Another risk with KEYS
is that it can consume (dependent on the size of your keyspace) a lot of RAM to prepare the response buffer, thus possibly exhausting your server's memory.
正如前面对这个问题的回答所指出的那样,密钥是一个潜在的危险命令,因为您的Redis服务器在服务它时将无法执行其他操作。密钥的另一个风险是它可以消耗大量RAM(取决于您的密钥空间的大小)来准备响应缓冲区,从而可能耗尽服务器的内存。
Version 2.8 of Redis had introduced the SCAN family of commands that are much more polite and can be used for the same purpose.
Redis的版本2.8引入了更有礼貌的命令的扫描族,并且可以用于相同的目的。
The CLI also provides a nice way to work with it:
CLI还提供了一种很好的处理方法:
$ redis-cli --scan --pattern '*'
#3
17
It can happen that using redis-cli, you connect to your remote redis-server, and then the command:
可以使用redis-cli,连接到远程的redis-server,然后命令:
KEYS *
is not showing anything, or better, it shows:(empty list or set)
没有显示任何东西,或者更好,它显示:(空列表或集合)
If you are absolutely sure that the Redis server you use is the one you have the data, then maybe your redis-cli is not connecting to the Redis correct database instance.
如果您完全确定您使用的Redis服务器是您拥有的数据,那么可能您的redis-cli并没有连接到Redis正确的数据库实例。
As it is mentioned in the Redis docs, new connections connect as default to the db 0.
正如在Redis文档中提到的那样,新的连接连接默认为db 0。
In my case KEYS
command was not retrieving results because my database was 1. In order to select the db you want, use SELECT.
The db is identified by an integer.
在我的情况下,KEYS命令没有检索结果,因为我的数据库是1。为了选择你想要的数据库,使用select。db由一个整数标识。
SELECT 1
KEYS *
I post this info because none of the previous answers was solving my issue.
我发布这个信息是因为之前没有一个答案能解决我的问题。
#4
13
Take a look at following Redis Cheat Sheet. To get a subset of redis keys with the redis-cli i use the command
看看下面的Redis Cheat Sheet。要获取redis的子集,使用redis-cli命令。
KEYS "prefix:*"
#5
11
-->Get all keys from redis-cli
->从redis-cli获得所有密钥。
-redis 127.0.0.1:6379> keys *
-->Get list of patterns
- - >模式列表
-redis 127.0.0.1:6379> keys d??
This will produce keys which start by 'd' with three characters.
这将产生由“d”开头的三个字符。
-redis 127.0.0.1:6379> keys *t*
This wil get keys with matches 't' character in key
这个wil可以在关键字中找到匹配“t”字符的键。
-->Count keys from command line by
-从命令行到>计数键。
-redis-cli keys * |wc -l
-->Or you can use dbsize
——>或者可以使用dbsize。
-redis-cli dbsize
#6
7
Yes, you can get all keys by using this
是的,你可以用这个来得到所有的钥匙。
var redis = require('redis');
redisClient = redis.createClient(redis.port, redis.host);
redisClient.keys('*example*', function (err, keys) {
})
#7
2
SCAN doesn't require the client to load all the keys into memory like KEYS does. SCAN gives you an iterator you can use. I had a 1B records in my redis and I could never get enough memory to return all the keys at once.
扫描不需要客户机像密钥那样将所有键加载到内存中。扫描给你一个你可以使用的迭代器。我的redis有一个1B记录,而且我永远无法获得足够的内存来一次返回所有的密钥。
Here is a python snippet to get all keys from the store matching a pattern and delete them:
下面是一个python代码片段,可以从存储模式中获取所有密钥,并将它们删除:
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
for key in r.scan_iter("key_pattern*"):
print key
#8
1
redis-cli -h <host> -p <port> keys *
where * is the pattern to list all keys
列出所有键的模式在哪里?