HVALS
Syntax
HVALS key
- Available since:
- Redis Open Source 2.0.0
- Time complexity:
- O(N) where N is the size of the hash.
- ACL categories:
@read
,@hash
,@slow
,
Returns all values in the hash stored at key
.
Examples
redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HVALS myhash
1) "Hello"
2) "World"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.
import redis
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
res1 = r.hset("myhash", "field1", "Hello")
print(res1)
# >>> 1
res2 = r.hget("myhash", "field1")
print(res2)
# >>> Hello
res3 = r.hset("myhash", mapping={"field2": "Hi", "field3": "World"})
print(res3)
# >>> 2
res4 = r.hget("myhash", "field2")
print(res4)
# >>> Hi
res5 = r.hget("myhash", "field3")
print(res5)
# >>> World
res6 = r.hgetall("myhash")
print(res6)
# >>> { "field1": "Hello", "field2": "Hi", "field3": "World" }
res7 = r.hset("myhash", "field1", "foo")
print(res7)
# >>> 1
res8 = r.hget("myhash", "field1")
print(res8)
# >>> foo
res9 = r.hget("myhash", "field2")
print(res9)
# >>> None
res10 = r.hset("myhash", mapping={"field1": "Hello", "field2": "World"})
res11 = r.hgetall("myhash")
print(res11) # >>> { "field1": "Hello", "field2": "World" }
res10 = r.hset("myhash", mapping={"field1": "Hello", "field2": "World"})
res11 = r.hvals("myhash")
print(res11) # >>> [ "Hello", "World" ]
import assert from 'node:assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
const res1 = await client.hSet('myhash', 'field1', 'Hello')
console.log(res1) // 1
const res2 = await client.hGet('myhash', 'field1')
console.log(res2) // Hello
const res3 = await client.hSet(
'myhash',
{
'field2': 'Hi',
'field3': 'World'
}
)
console.log(res3) // 2
const res4 = await client.hGet('myhash', 'field2')
console.log(res4) // Hi
const res5 = await client.hGet('myhash', 'field3')
console.log(res5) // World
const res6 = await client.hGetAll('myhash')
console.log(res6)
const res7 = await client.hSet('myhash', 'field1', 'foo')
console.log(res7) // 1
const res8 = await client.hGet('myhash', 'field1')
console.log(res8) // foo
const res9 = await client.hGet('myhash', 'field2')
console.log(res9) // null
const res10 = await client.hSet(
'myhash',
{
'field1': 'Hello',
'field2': 'World'
}
)
const res11 = await client.hGetAll('myhash')
console.log(res11) // [Object: null ] { field1: 'Hello', field2: 'World' }
const res12 = await client.hSet(
'myhash',
{
'field1': 'Hello',
'field2': 'World'
}
)
const res13 = await client.hVals('myhash')
console.log(res13) // [ 'Hello', 'World' ]
await client.quit();
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Collections;
import redis.clients.jedis.UnifiedJedis;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class CmdsHashExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
Map<String, String> hGetExampleParams = new HashMap<>();
hGetExampleParams.put("field1", "foo");
long hGetResult1 = jedis.hset("myhash", hGetExampleParams);
System.out.println(hGetResult1); // >>> 1
String hGetResult2 = jedis.hget("myhash", "field1");
System.out.println(hGetResult2); // >>> foo
String hGetResult3 = jedis.hget("myhash", "field2");
System.out.println(hGetResult3); // >>> null
Map<String, String> hGetAllExampleParams = new HashMap<>();
hGetAllExampleParams.put("field1", "Hello");
hGetAllExampleParams.put("field2", "World");
long hGetAllResult1 = jedis.hset("myhash", hGetAllExampleParams);
System.out.println(hGetAllResult1); // >>> 2
Map<String, String> hGetAllResult2 = jedis.hgetAll("myhash");
System.out.println(
hGetAllResult2.entrySet().stream()
.sorted((s1, s2)-> s1.getKey().compareTo(s2.getKey()))
.collect(toList())
.toString()
);
// >>> [field1=Hello, field2=World]
Map<String, String> hSetExampleParams = new HashMap<>();
hSetExampleParams.put("field1", "Hello");
long hSetResult1 = jedis.hset("myhash", hSetExampleParams);
System.out.println(hSetResult1); // >>> 1
String hSetResult2 = jedis.hget("myhash", "field1");
System.out.println(hSetResult2); // >>> Hello
hSetExampleParams.clear();
hSetExampleParams.put("field2", "Hi");
hSetExampleParams.put("field3", "World");
long hSetResult3 = jedis.hset("myhash",hSetExampleParams);
System.out.println(hSetResult3); // >>> 2
String hSetResult4 = jedis.hget("myhash", "field2");
System.out.println(hSetResult4); // >>> Hi
String hSetResult5 = jedis.hget("myhash", "field3");
System.out.println(hSetResult5); // >>> World
Map<String, String> hSetResult6 = jedis.hgetAll("myhash");
for (String key: hSetResult6.keySet()) {
System.out.println("Key: " + key + ", Value: " + hSetResult6.get(key));
}
// >>> Key: field3, Value: World
// >>> Key: field2, Value: Hi
// >>> Key: field1, Value: Hello
Map<String, String> hValsExampleParams = new HashMap<>();
hValsExampleParams.put("field1", "Hello");
hValsExampleParams.put("field2", "World");
long hValsResult1 = jedis.hset("myhash", hValsExampleParams);
System.out.println(hValsResult1); // >>> 2
List<String> hValsResult2 = jedis.hvals("myhash");
Collections.sort(hValsResult2);
System.out.println(hValsResult2);
// >>> [Hello, World]
jedis.close();
}
}
package example_commands_test
import (
"context"
"fmt"
"sort"
".com/redis/go-redis/v9"
)
func ExampleClient_hset() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.HSet(ctx, "myhash", "field1", "Hello").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.HGet(ctx, "myhash", "field1").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> Hello
res3, err := rdb.HSet(ctx, "myhash",
"field2", "Hi",
"field3", "World",
).Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> 2
res4, err := rdb.HGet(ctx, "myhash", "field2").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> Hi
res5, err := rdb.HGet(ctx, "myhash", "field3").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> World
res6, err := rdb.HGetAll(ctx, "myhash").Result()
if err != nil {
panic(err)
}
keys := make([]string, 0, len(res6))
for key, _ := range res6 {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
fmt.Printf("Key: %v, value: %v\n", key, res6[key])
}
// >>> Key: field1, value: Hello
// >>> Key: field2, value: Hi
// >>> Key: field3, value: World
}
func ExampleClient_hget() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res7, err := rdb.HSet(ctx, "myhash", "field1", "foo").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> 1
res8, err := rdb.HGet(ctx, "myhash", "field1").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> foo
res9, err := rdb.HGet(ctx, "myhash", "field2").Result()
if err != nil {
fmt.Println(err)
}
fmt.Println(res9) // >>> <empty string>
}
func ExampleClient_hgetall() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password
DB: 0, // use default DB
})
hGetAllResult1, err := rdb.HSet(ctx, "myhash",
"field1", "Hello",
"field2", "World",
).Result()
if err != nil {
panic(err)
}
fmt.Println(hGetAllResult1) // >>> 2
hGetAllResult2, err := rdb.HGetAll(ctx, "myhash").Result()
if err != nil {
panic(err)
}
keys := make([]string, 0, len(hGetAllResult2))
for key, _ := range hGetAllResult2 {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
fmt.Printf("Key: %v, value: %v\n", key, hGetAllResult2[key])
}
// >>> Key: field1, value: Hello
// >>> Key: field2, value: World
}
func ExampleClient_hvals() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
hValsResult1, err := rdb.HSet(ctx, "myhash",
"field1", "Hello",
"field2", "World",
).Result()
if err != nil {
panic(err)
}
fmt.Println(hValsResult1) // >>> 2
hValsResult2, err := rdb.HVals(ctx, "myhash").Result()
if err != nil {
panic(err)
}
sort.Strings(hValsResult2)
fmt.Println(hValsResult2) // >>> [Hello World]
}
using StackExchange.Redis;
public class CmdsHashExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.HashSet("myhash", "field1", "foo");
RedisValue res2 = db.HashGet("myhash", "field1");
Console.WriteLine(res2); // >>> foo
RedisValue res3 = db.HashGet("myhash", "field2");
Console.WriteLine(res3); // >>> Null
bool res4 = db.HashSet("myhash", "field1", "Hello");
RedisValue res5 = db.HashGet("myhash", "field1");
Console.WriteLine(res5); // >>> Hello
db.HashSet(
"myhash",
new HashEntry[] {
new HashEntry("field2", "Hi"),
new HashEntry("field3", "World")
}
);
RedisValue res6 = db.HashGet("myhash", "field2");
Console.WriteLine(res6); // >>> Hi
RedisValue res7 = db.HashGet("myhash", "field3");
Console.WriteLine(res7); // >>> World
HashEntry[] res8 = db.HashGetAll("myhash");
Console.WriteLine($"{string.Join(", ", res8.Select(h => $"{h.Name}: {h.Value}"))}");
// >>> field1: Hello, field2: Hi, field3: World
db.HashSet("myhash",
new HashEntry[] {
new HashEntry("field1", "Hello"),
new HashEntry("field2", "World")
}
);
HashEntry[] hGetAllResult = db.HashGetAll("myhash");
Array.Sort(hGetAllResult, (a1, a2) => a1.Name.CompareTo(a2.Name));
Console.WriteLine(
string.Join(", ", hGetAllResult.Select(e => $"{e.Name}: {e.Value}"))
);
// >>> field1: Hello, field2: World
db.HashSet("myhash",
new HashEntry[] {
new HashEntry("field1", "Hello"),
new HashEntry("field2", "World")
}
);
RedisValue[] hValsResult = db.HashValues("myhash");
Array.Sort(hValsResult);
Console.WriteLine(string.Join(", ", hValsResult));
// >>> Hello, World
}
}
Give these commands a try in the interactive console:
Return information
Array reply: a list of values in the hash, or an empty list when the key does not exist
Array reply: a list of values in the hash, or an empty list when the key does not exist.