Redis 命令获取所有可用密钥?

发布于 2024-10-20 20:58:47 字数 86 浏览 5 评论 0 原文

是否有一个 Redis 命令可以获取数据库中的所有键?我见过一些 python-redis 库获取它们。但想知道是否可以通过 redis-client 实现。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

夕色琉璃 2024-10-27 20:58:47

尝试查看 KEYS 命令。 KEYS * 将列出存储在 redis 中的所有密钥。

编辑:请注意KEYS文档页面顶部的警告:

时间复杂度: O(N),其中 N 是数据库中键的数量,假设数据库中的键名称和给定模式的长度有限。

更新(V2.8或更高版本): 扫描 是 KEYS 的更好替代方案,因为它不会阻塞服务器,也不会消耗大量资源。更喜欢使用它。

Try to look at KEYS command. KEYS * will list all keys stored in 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.

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.

谁的年少不轻狂 2024-10-27 20:58:47

针对 Redis 2.8 及更高版本进行了更新

正如此问题之前答案的评论中所述,KEYS 是一个潜在危险的命令,因为您的 Redis 服务器将无法执行其他操作它为它服务。 KEYS 的另一个风险是它可能会消耗(取决于密钥空间的大小)大量 RAM 来准备响应缓冲区,从而可能耗尽服务器的内存。

Redis 2.8 版本引入了 SCAN 系列命令,这些命令更加礼貌并且可用于相同的目的目的。

CLI 还提供了一种很好的使用方式:

$ redis-cli --scan --pattern '*'

Updated for Redis 2.8 and above

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.

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.

The CLI also provides a nice way to work with it:

$ redis-cli --scan --pattern '*'
影子的影子 2024-10-27 20:58:47

可能会发生这样的情况:使用 redis-cli 连接到远程 redis 服务器,然后命令:

KEYS *

不显示任何内容,或者更好的是,它显示:
(空列表或集)

如果您绝对确定您使用的 Redis 服务器就是您拥有数据的服务器,那么您的 redis-cli 可能没有连接到正确的 Redis 数据库实例。

正如 Redis 文档中提到的,新连接默认连接到 db 0

就我而言,KEYS 命令未检索结果,因为我的数据库为 1。为了选择所需的数据库,请使用 选择
db 由一个整数标识。

SELECT 1
KEYS *

我发布此信息是因为以前的答案都没有解决我的问题。

It can happen that using redis-cli, you connect to your remote redis-server, and then the command:

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.

As it is mentioned in the Redis docs, new connections connect as default to the 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.

SELECT 1
KEYS *

I post this info because none of the previous answers was solving my issue.

七堇年 2024-10-27 20:58:47

获取 Redis 中的所有密钥

使用 --scan 选项获取所有密钥:

$ redis-cli --scan --pattern '*'

使用 KEYS 命令列出所有密钥:

$ redis-cli KEYS '*'

Get All Keys In Redis

Get all keys using the --scan option:

$ redis-cli --scan --pattern '*'

List all keys using the KEYS command:

$ redis-cli KEYS '*'
暗地喜欢 2024-10-27 20:58:47

--> 从 redis-cli 获取所有键

-redis 127.0.0.1:6379> keys *

--> 获取模式列表

-redis 127.0.0.1:6379> keys d??

这将生成以 'd' 开头的三个字符的键。

-redis 127.0.0.1:6379> keys *t*

这将获得与键中的“t”字符匹配的键

-->通过命令行对键进行计数

-redis-cli keys * |wc -l

-->或者您可以使用dbsize

-redis-cli dbsize

-->Get all keys from 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.

-redis 127.0.0.1:6379> keys *t*

This wil get keys with matches 't' character in key

-->Count keys from command line by

-redis-cli keys * |wc -l

-->Or you can use dbsize

-redis-cli dbsize
情深已缘浅 2024-10-27 20:58:47

请查看以下 Redis 备忘单
要使用 redis-cli 获取 redis 键的子集,我使用以下命令

KEYS "prefix:*"

Take a look at following Redis Cheat Sheet.
To get a subset of redis keys with the redis-cli i use the command

KEYS "prefix:*"
ヤ经典坏疍 2024-10-27 20:58:47

SCAN 不需要客户端像 KEYS 那样将所有密钥加载到内存中。 SCAN 为您提供了一个可以使用的迭代器。我的 redis 中有一条 1B 记录,但我永远无法获得足够的内存来一次返回所有键。

下面是一个 python 代码片段,用于从存储中获取与模式匹配的所有键并删除它们:

import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
for key in r.scan_iter("key_pattern*"):
    print key

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.

Here is a python snippet to get all keys from the store matching a pattern and delete them:

import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
for key in r.scan_iter("key_pattern*"):
    print key
弥枳 2024-10-27 20:58:47

是的,您可以使用此获取所有密钥

var redis = require('redis');
redisClient = redis.createClient(redis.port, redis.host);    
  redisClient.keys('*example*', function (err, keys) {
})

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) {
})
一花一树开 2024-10-27 20:58:47
redis-cli -h <host> -p <port> keys * 

其中 * 是列出所有键的模式

redis-cli -h <host> -p <port> keys * 

where * is the pattern to list all keys

萌化 2024-10-27 20:58:47

按键模式

自 1.0.0 起可用。

时间复杂度:O(N),N 为数字
数据库中的键的数量,假设键名称为
数据库和给定模式的长度有限。

返回与模式匹配的所有键。

警告:不建议使用此命令,因为在大型数据库上执行该命令时可能会破坏性能,而不是可以使用 扫描SETS

要使用的 KEYS 命令示例:

redis> MSET firstname Jack lastname Stuntman age 35
"OK"
redis> KEYS *name*
1) "lastname"
2) "firstname"
redis> KEYS a??
1) "age"
redis> KEYS *
1) "lastname"
2) "age"
3) "firstname"

KEYS pattern

Available since 1.0.0.

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.

Returns all keys matching pattern.

Warning : This command is not recommended to use because it may ruin performance when it is executed against large databases instead of KEYS you can use SCAN or SETS.

Example of KEYS command to use :

redis> MSET firstname Jack lastname Stuntman age 35
"OK"
redis> KEYS *name*
1) "lastname"
2) "firstname"
redis> KEYS a??
1) "age"
redis> KEYS *
1) "lastname"
2) "age"
3) "firstname"
╭⌒浅淡时光〆 2024-10-27 20:58:47

为了获取 redis 服务器中可用的所有密钥,您应该打开 redis-cli 并输入:
按键*
为了获得更多帮助,请访问此页面:
此链接

In order to get all the keys available in redis server, you should open redis-cli and type:
KEYS *
In order to get more help please visit this page:
This Link

浮萍、无处依 2024-10-27 20:58:47

如果你的redis是集群,可以使用这个脚本

#!/usr/bin/env bash
redis_list=("172.23.3.19:7001,172.23.3.19:7002,172.23.3.19:7003,172.23.3.19:7004,172.23.3.19:7005,172.23.3.19:7006")

arr=($(echo "$redis_list" | tr ',' '\n'))

for info in ${arr[@]}; do
  echo "start :${info}"
  redis_info=($(echo "$info" | tr ':' '\n'))
  ip=${redis_info[0]}
  port=${redis_info[1]}
  echo "ip="${ip}",port="${port}
  redis-cli -c -h $ip -p $port set laker$port '湖人总冠军'
  redis-cli -c -h $ip -p $port keys \*

done

echo "end"

If your redis is a cluster,you can use this script

#!/usr/bin/env bash
redis_list=("172.23.3.19:7001,172.23.3.19:7002,172.23.3.19:7003,172.23.3.19:7004,172.23.3.19:7005,172.23.3.19:7006")

arr=($(echo "$redis_list" | tr ',' '\n'))

for info in ${arr[@]}; do
  echo "start :${info}"
  redis_info=($(echo "$info" | tr ':' '\n'))
  ip=${redis_info[0]}
  port=${redis_info[1]}
  echo "ip="${ip}",port="${port}
  redis-cli -c -h $ip -p $port set laker$port '湖人总冠军'
  redis-cli -c -h $ip -p $port keys \*

done

echo "end"
猫腻 2024-10-27 20:58:47

您可以简单地使用 redis-cli 连接到您的 redis 服务器,选择您的数据库并输入 KEYS *,请记住,它将为您提供所选 redis 数据库中存在的所有密钥。

You can simply connect to your redis server using redis-cli, select your database and type KEYS *, please remember it will give you all the keys present in selected redis database.

风流物 2024-10-27 20:58:47

对于那些需要打字稿助手(使用 ioredis)

import Redis from 'ioredis';
import { from, Observable, of } from 'rxjs';
import { first, mergeMap } from 'rxjs/operators';

export function scanKeysFromRedis(redisStore: Redis.Redis, key: string, 
target: number = 0, keys: string[] = []): Observable<string[]> {
  return from(redisStore.scan(target, 'MATCH', key)).pipe(
    first(),
    mergeMap((_keys) => {
      const _target = Number(_keys[0]);
      if (_target !== 0) {
        return scanKeysFromRedis(redisStore, key, _target, [...keys, ..._keys[1]]);
       }
      return of([...keys, ..._keys[1]]);
    }),
  );
}

并使用以下方式调用它的人: scanKeysFromRedis(store, 'hello');

For the ones that wants a typescript helper (using ioredis)

import Redis from 'ioredis';
import { from, Observable, of } from 'rxjs';
import { first, mergeMap } from 'rxjs/operators';

export function scanKeysFromRedis(redisStore: Redis.Redis, key: string, 
target: number = 0, keys: string[] = []): Observable<string[]> {
  return from(redisStore.scan(target, 'MATCH', key)).pipe(
    first(),
    mergeMap((_keys) => {
      const _target = Number(_keys[0]);
      if (_target !== 0) {
        return scanKeysFromRedis(redisStore, key, _target, [...keys, ..._keys[1]]);
       }
      return of([...keys, ..._keys[1]]);
    }),
  );
}

and call it with: scanKeysFromRedis(store, 'hello');

凉城已无爱 2024-10-27 20:58:47

如果您使用 Laravel 框架,那么您可以简单地使用:

$allKeyList = Redis::KEYS("*");

print_r($allKeyList);

在 Core PHP 中:

$redis = new Redis();

$redis->connect('hostname', 6379);

$allKeyList = $redis->keys('*');

print_r($allKeyList);

If you are using Laravel Framework then you can simply use this:

$allKeyList = Redis::KEYS("*");

print_r($allKeyList);

In Core PHP:

$redis = new Redis();

$redis->connect('hostname', 6379);

$allKeyList = $redis->keys('*');

print_r($allKeyList);
梦与时光遇 2024-10-27 20:58:47

我们应该将 --scan --pattern 与 redis 2.8 及更高版本一起使用。

您可以尝试在 redis-cli 之上使用此包装器。
https://github.com/VijayantSoni/redis-helper

We should be using --scan --pattern with redis 2.8 and later.

You can try using this wrapper on top of redis-cli.
https://github.com/VijayantSoni/redis-helper

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文