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

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

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

環境

  • OS CentOS Stream release 9
  • Redis 7.0.5

手順

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

hvals キー名

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

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> hvals key1
1) "abc"
2) "de"
3) "fg"

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

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

127.0.0.1:6379> set kye2 "v"
OK

127.0.0.1:6379> hvals key2
(empty array)

127.0.0.1:6379> hvals nokey
(empty array)

フィールド値のみを取得

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

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

両方取得

両方とも取得する場合は、「hgetall」を使用します。

127.0.0.1:6379> hgetall key1
1) "hash1"
2) "abc"
3) "hash2"
4) "de"
5) "hash3"
6) "fg"