Redis keyに設定されているハッシュ値のフィールド値のみを全て取得する

Redis keyに設定されているハッシュ値のフィールド値のみを全て取得する

Redisで、keyに設定されているハッシュ値のフィールド値のみを全て取得する手順を記述してます。「hkeys」でキー名を指定します。ここでは、実際に実行した結果を掲載してます。

環境

  • OS CentOS Stream release 9
  • Redis 7.0.5

手順

keyに設定されているハッシュ値のフィールド値のみを全て取得するには、「hkeys」を使用します。

hkeys キー名

実際に、いくつか作成して取得してみます。

127.0.0.1:6379> hexists key1 hash1
(integer) 1

127.0.0.1:6379> hexists key1 hash2
(integer) 1

127.0.0.1:6379> hexists key1 hash3
(integer) 0

127.0.0.1:6379> hset key1 hash3 "fg"
(integer) 1

127.0.0.1:6379> hkeys key1
1) "hash1"
2) "hash2"
3) "hash3"

取得できていることが確認できます。

ハッシュ値がない場合は、空が返ります。

127.0.0.1:6379> set kye2 "v"
OK

127.0.0.1:6379> hkeys key2
(empty array)

127.0.0.1:6379> hkeys nokey
(empty array)

値のみを取得

値のみを取得する場合は、「hvals」を使用します。

127.0.0.1:6379> hvals key1
1) "abc"
2) "de"
3) "fg"