The HVALS
command in Redis is used to fetch all the values stored in a hash. This can be useful when you need to extract all the values from a hash, but you're not concerned with the keys. In Java, we normally use the Jedis library to interact with Redis.
import redis.clients.jedis.Jedis; public class Main { public static void main(String[] args) { // connect to Redis server Jedis jedis = new Jedis("localhost"); // add some data to our hash jedis.hset("user:1", "name", "John Doe"); jedis.hset("user:1", "email", "john@example.com"); // get all values from the hash List<String> values = jedis.hvals("user:1"); for (String value : values) { System.out.println(value); } jedis.close(); } }
In this example, we first connect to the Redis server using Jedis. We then store some data in our hash. The hvals
method is then used to fetch all the values from the hash, and we print those values.
HVALS
, remember that it may consume high CPU if the hash has many keys since it returns all the values.HVALS
on non-hash keys will result in a JedisDataException. Always ensure that the key you're working with points to a hash.Why are my values returned in a random order by HVALS? In Redis, hashes do not maintain any particular order. The order of values returned by HVALS is not guaranteed.
Can I get the keys corresponding to the values returned by HVALS?
No, HVALS only returns the values in the hash. If you also need the keys, consider using HGETALL
.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.