Redis Command: HVALS

Summary

Command NameHVALS
UsageGet all values of a hash
Group hash
ACL Category@read
@hash
@slow
Time ComplexityO(1)
FlagREADONLY
Arity2

Notes

  • Time complexity is O(N), where N is the number of fields in the hash.

Signature

HVALS <key>

Usage

Get all the values of a hash. Only the values are returned as a list.

Notes

  • To get the keys we can use the HKEYS command.
  • We can use the HGETALL command to get all the keys and values.

Arguments

ParameterDescriptionNameType
<key>Name of the key of the hashkeykey

Return Value

Return valueCase for the return valueType
List of valuesOn successful we get the list of values of the hasharray[string]
(empty array)If the key does not exist(empty array)
errorIf applied to wrong data typeerror

Notes

  • If the command is applied to a key that is not a hash, then the following error is returned-
    (error) WRONGTYPE Operation against a key holding the wrong kind of value

Examples

Here are a few examples of the usage examples of the Redis HVALS command-

# Redis HGET command examples

# Set some has fields usign HSET
127.0.0.1:6379> hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States"
(integer) 5

# Check zip field of the hash
127.0.0.1:6379> hget customer:99:address zip
"65616"

# Check state field of the hash
127.0.0.1:6379> hget customer:99:address state
"Mississippi"

# Try to get value of a field that does not exist
# We get (nil)
127.0.0.1:6379> hget customer:99:address nonexistingfield
(nil)

# Try to get field value from a non existing hash
# We get (nil)
127.0.0.1:6379> hget nonexistinghash somefield
(nil)

# Set a string value
127.0.0.1:6379> set bigboxstr "some string in the box"
OK

# Try to use the HGET on a string type of key
# We get an error
127.0.0.1:6379> hget bigboxstr somefield
(error) WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • If the key does not exist or the field does not exist then the command returns Null (nil)

Code Implementations

Redis HGET command example implementation in Golang, NodeJS, Java, C#, PHP, Python, Ruby-

// Redis HVALS command example in Golang

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

var rdb *redis.Client
var ctx context.Context

func init() {
	rdb = redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Username: "default",
		Password: "",
		DB:       0,
	})

	ctx = context.Background()
}

func main() {

	/**
	* Set hash field/value
	* Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
	* Result: (integer) 8
	 */
	hsetResult, err := rdb.HSet(ctx, "customer:1786:address",
		"street", "6414 Losee Rd",
		"city", "North Las Vegas",
		"state", "North Carolina",
		"zip", "89086",
		"phone", "(702) 399-9939",
		"country", "United States",
		"latitude", "36.27704",
		"longitude", "-115.115868",
	).Result()

	if err != nil {
		fmt.Println("Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Error: " + err.Error())
	}

	fmt.Println("Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: ", hsetResult)

	/**
	* Check hash full data
	* Command: hgetall customer:1786:address
	* Result:
	*         1) "street"
	*         2) "6414 Losee Rd"
	*         3) "city"
	*         4) "North Las Vegas"
	*         5) "state"
	*         6) "North Carolina"
	*         7) "zip"
	*         8) "89086"
	*         9) "phone"
	*         10) "(702) 399-9939"
	*         11) "country"
	*         12) "United States"
	*         13) "latutude"
	*         14) "36.27704"
	*         15) "longitude"
	*         16) "-115.115868"
	 */
	hgetAllResult, err := rdb.HGetAll(ctx, "customer:1786:address").Result()

	if err != nil {
		fmt.Println("Command: hgetall customer:1786:address | Error: " + err.Error())
	}

	fmt.Println("Command: hgetall customer:1786:address | Result: ", hgetAllResult)

	/**
	* Get all the values of hash
	*
	* Command: hvals customer:1786:address
	* Result:
	*          1) "6414 Losee Rd"
	*          2) "North Las Vegas"
	*          3) "North Carolina"
	*          4) "89086"
	*          5) "(702) 399-9939"
	*          6) "United States"
	*          7) "36.27704"
	*          8) "-115.115868"
	*/
	hvalsResult, err := rdb.HVals(ctx, "customer:1786:address").Result()

	if err != nil {
		fmt.Println("Command: hvals customer:1786:address | Error: " + err.Error())
	}

	fmt.Println("Command: hvals customer:1786:address | Result: ", hvalsResult)

	/**
	* Use HVALS on a non existing key
	* We get (empty list)
	*
	* Command: hvals nonexistingkey
	* Result: (empty array)
	 */
	 hvalsResult, err = rdb.HVals(ctx, "nonexistingkey").Result()

	if err != nil {
		fmt.Println("Command: hvals nonexistingkey | Error: " + err.Error())
	}

	fmt.Println("Command: hvals nonexistingkey | Result: ", hvalsResult)

	/**
	* Set string value
	*
	* Command: set bigboxstr "some stiring value for HVALS command testing"
	* Result: OK
	 */
	setResult, err := rdb.Set(ctx, "bigboxstr", "some stiring value for HVALS command testing", 0).Result()

	if err != nil {
		fmt.Println("Command: set bigboxstr "some stiring value for HVALS command testing" | Error: " + err.Error())
	}

	fmt.Println("Command: set bigboxstr "some stiring value for HVALS command testing" | Result: " + setResult)

	/**
	* Try to use HVALS on a hash
	* We get an error
	*
	* Command: hvals bigboxstr
	* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
	 */
	 hvalsResult, err = rdb.HVals(ctx, "bigboxstr").Result()

	if err != nil {
		fmt.Println("Command: hvals bigboxstr | Error: " + err.Error())
	}

	fmt.Println("Command: hvals bigboxstr | Result: ", hvalsResult)
}

Output:

Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result:  8

Command: hgetall customer:1786:address | Result:  map[city:North Las Vegas country:United States latitude:36.27704 longitude:-115.115868 phone:(702) 399-9939 state:North Carolina street:6414 Losee Rd zip:89086]

Command: hvals customer:1786:address | Result:  [6414 Losee Rd North Las Vegas North Carolina 89086 (702) 399-9939 United States 36.27704 -115.115868]

Command: hvals nonexistingkey | Result:  []

Command: set bigboxstr "some stiring value for HVALS command testing" | Result: OK
Command: hvals bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hvals bigboxstr | Result:  []

Notes

  • Use “HVals” method from redis-go module.
  • Signature of the method is-
    HVals(ctx context.Context, key string) *StringSliceCmd
// Redis HVALS command example in JavaScript(NodeJS)

import { createClient } from "redis";

// Create redis client
const redisClient = createClient({
  url: "redis://default:@localhost:6379",
});

redisClient.on("error", (err) =>
  console.log("Error while connecting to Redis", err)
);

// Connect Redis client
await redisClient.connect();

/**
 * Set hash field/value
 * Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
 * Result: (integer) 8
 */
let commandResult = await redisClient.hSet("customer:1786:address", {
  street: "6414 Losee Rd",
  city: "North Las Vegas",
  state: "North Carolina",
  zip: "89086",
  phone: "(702) 399-9939",
  country: "United States",
  latitude: "36.27704",
  longitude: "-115.115868",
});

console.log(
  'Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: ' +
    commandResult
);

/**
 * Check hash full data
 * Command: hgetall customer:1786:address
 * Result:
 *         1) "street"
 *         2) "6414 Losee Rd"
 *         3) "city"
 *         4) "North Las Vegas"
 *         5) "state"
 *         6) "North Carolina"
 *         7) "zip"
 *         8) "89086"
 *         9) "phone"
 *         10) "(702) 399-9939"
 *         11) "country"
 *         12) "United States"
 *         13) "latutude"
 *         14) "36.27704"
 *         15) "longitude"
 *         16) "-115.115868"
 */
commandResult = await redisClient.hGetAll("customer:1786:address");

console.log(
  "Command: hgetall customer:1099:address | Result: ",
  commandResult
);

/**
 * Get all the values of hash
 *
 * Command: hvals customer:1786:address
 * Result:
 *          1) "6414 Losee Rd"
 *          2) "North Las Vegas"
 *          3) "North Carolina"
 *          4) "89086"
 *          5) "(702) 399-9939"
 *          6) "United States"
 *          7) "36.27704"
 *          8) "-115.115868"
 */
commandResult = await redisClient.hVals("customer:1786:address");

console.log(
  "Command: hvals customer:1099:address | Result: ",
  commandResult
);

/**
 * Use HVALS on a non existing key
 * We get (empty list)
 *
 * Command: hvals nonexistingkey
 * Result: (empty array)
 */
commandResult = await redisClient.hVals("nonexistingkey");

console.log("Command: hvals nonexistingkey | Result: ", commandResult);

/**
 * Set string value
 * Command: set bigboxstr "some stiring value for HVALS command testing"
 * Result: OK
 */
commandResult = await redisClient.set(
  "bigboxstr",
  "some stiring value for HVALS command testing"
);

console.log(
  'Command: set bigboxstr "some stiring value for HVALS command testing" | Result: ',
  commandResult
);

/**
 * Try to use HVALS on a hash
 * We get an error
 * Command: hvals bigboxstr
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
  commandResult = await redisClient.hVals("bigboxstr");

  console.log("Command: hvals bigboxstr | Result: " + commandResult);
} catch (err) {
  console.log("Command: hvals bigboxstr | Error: ", err);
}

process.exit(0);

Output:

Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8

Command: hgetall customer:1099:address | Result:  [Object: null prototype] {
  street: '6414 Losee Rd',
  city: 'North Las Vegas',
  state: 'North Carolina',
  zip: '89086',
  phone: '(702) 399-9939',
  country: 'United States',
  latitude: '36.27704',
  longitude: '-115.115868'
}

Command: hgetall customer:1099:address | Result:  [
  '6414 Losee Rd',
  'North Las Vegas',
  'North Carolina',
  '89086',
  '(702) 399-9939',
  'United States',
  '36.27704',
  '-115.115868'
]

Command: hvals nonexistingkey | Result:  []

Command: set bigboxstr "some stiring value for HVALS command testing" | Result:  OK
Command: hvals bigboxstr | Error:  [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]

Notes

  • Use the function “hVals” of node-redis.
  • Signature of the method-
    function hVals(key: RedisCommandArgument)
// Redis HVALS Command example in Java

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HVals {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("localhost", 6379);

        try (Jedis jedis = jedisPool.getResource()) {

            /**
             * Set hash field/value
             *
             * Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
             * Result: (integer) 8
             */
            Map<String, String> hashData = new HashMap<>() {{
                put("street", "6414 Losee Rd");
                put("city", "North Las Vegas");
                put("state", "North Carolina");
                put("zip", "89086");
                put("phone", "(702) 399-9939");
                put("country", "United States");
                put("latitude", "36.27704");
                put("longitude", "-115.115868");
            }};
            long hsetResult = jedis.hset("customer:1786:address", hashData);

            System.out.println("Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: " + hsetResult);


            /**
             * Check hash full data
             *
             * Command: hgetall customer:1786:address
             * Result:
             *         1) "street"
             *         2) "6414 Losee Rd"
             *         3) "city"
             *         4) "North Las Vegas"
             *         5) "state"
             *         6) "North Carolina"
             *         7) "zip"
             *         8) "89086"
             *         9) "phone"
             *         10) "(702) 399-9939"
             *         11) "country"
             *         12) "United States"
             *         13) "latutude"
             *         14) "36.27704"
             *         15) "longitude"
             *         16) "-115.115868"
             */
            Map<String, String> hgetAllResult = jedis.hgetAll("customer:1786:address");

            System.out.println("Command: hgetall customer:1099:address | Result: " + hgetAllResult.toString());

            /**
             * Get all the values of hash
             *
             * Command: hvals customer:1786:address
             * Result:
             *          1) "6414 Losee Rd"
             *          2) "North Las Vegas"
             *          3) "North Carolina"
             *          4) "89086"
             *          5) "(702) 399-9939"
             *          6) "United States"
             *          7) "36.27704"
             *          8) "-115.115868"
             */
            List<String> hvalsResult = jedis.hvals("customer:1786:address");

            System.out.println("Command: hvals customer:1099:address | Result: " + hvalsResult.toString());

            /**
             * Use HVALS on a non existing key
             * We get (empty list)
             *
             * Command: hvals nonexistingkey
             * Result: (empty array)
             */
            hvalsResult = jedis.hvals("nonexistingkey");

            System.out.println("Command: hvals nonexistingkey | Result: " + hvalsResult);

            /**
             * Set string value
             *
             * Command: set bigboxstr "some stiring value for HVALS command testing"
             * Result: OK
             */
            String setResult = jedis.set("bigboxstr", "some stiring value for HVALS command testing");

            System.out.println("Command: set bigboxstr "some stiring value for HVALS command testing" | Result: " + setResult);

            /**
             * Try to use HVALS on a hash
             * We get an error
             *
             * Command: HVALS bigboxstr
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try {
                hvalsResult = jedis.hvals("bigboxstr");

                System.out.println("Command: hvals bigboxstr | Result: " + hvalsResult);
            } catch (Exception e) {
                System.out.println("Command: hvals bigboxstr | Error: " + e.getMessage());
            }

        }

        jedisPool.close();
    }
}

Output:

Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8

Command: hgetall customer:1099:address | Result: {zip=89086, country=United States, state=North Carolina, city=North Las Vegas, phone=(702) 399-9939, street=6414 Losee Rd, latitude=36.27704, longitude=-115.115868}

Command: hvals customer:1099:address | Result: [89086, United States, North Las Vegas, (702) 399-9939, 6414 Losee Rd, 36.27704, North Carolina, -115.115868]

Command: hvals nonexistingkey | Result: []

Command: set bigboxstr "some stiring value for HVALS command testing" | Result: OK
Command: hvals bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use method “hvals” from Jedis package.
  • The signature of the method is-
    public List<String> hvals(final String key)
// Redis HVALS command examples in C#

using StackExchange.Redis;

namespace HVals
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            IDatabase rdb = redis.GetDatabase();

            /**
             * Set hash field/value
             *
             * Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
             * Result: (integer) 8
             */
            HashEntry[] hashData = new HashEntry[] {
                new HashEntry("street", "6414 Losee Rd"),
            new HashEntry("city", "North Las Vegas"),
            new HashEntry("state", "North Carolina"),
            new HashEntry("zip", "89086"),
            new HashEntry("phone", "(702) 399-9939"),
            new HashEntry("country", "United States"),
            new HashEntry("latitude", "36.27704"),
            new HashEntry("longitude", "-115.115868"),
        };
            rdb.HashSet("customer:1786:address", hashData);

            Console.WriteLine("Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868");

            /**
             * Check hash full data
             *
             * Command: hgetall customer:1786:address
             * Result:
             *         1) "street"
             *         2) "6414 Losee Rd"
             *         3) "city"
             *         4) "North Las Vegas"
             *         5) "state"
             *         6) "North Carolina"
             *         7) "zip"
             *         8) "89086"
             *         9) "phone"
             *         10) "(702) 399-9939"
             *         11) "country"
             *         12) "United States"
             *         13) "latutude"
             *         14) "36.27704"
             *         15) "longitude"
             *         16) "-115.115868"
             */
            HashEntry[] hgetAllResult = rdb.HashGetAll("customer:1786:address");

            Console.WriteLine("Command: hgetall customer:1099:address | Result: " + String.Join(", ", hgetAllResult));

            /**
             * Get all the values of hash
             *
             * Command: hvals customer:1786:address
             * Result:
             *         1) "street"
             *         2) "city"
             *         3) "state"
             *         4) "zip"
             *         5) "phone"
             *         6) "country"
             *         7) "latutude"
             *         8) "longitude"
             */
            RedisValue[] hvalsResult = rdb.HashValues("customer:1786:address");

            Console.WriteLine("Command: hvals customer:1099:address | Result: " + String.Join(", ", hvalsResult));

            /**
             * Use HVALS on a non existing key
             * We get (empty list)
             *
             * Command: hvals nonexistingkey
             * Result: (empty array)
             */
            hvalsResult = rdb.HashValues("nonexistingkey");

            Console.WriteLine("Command: hvals nonexistingkey | Result: " + String.Join(", ", hvalsResult));

            /**
             * Set string value
             *
             * Command: set bigboxstr "some stiring value for HVALS command testing"
             * Result: OK
             */
            bool setResult = rdb.StringSet("bigboxstr", "some stiring value for HVALS command testing");

            Console.WriteLine("Command: set bigboxstr "some stiring value for HVALS command testing" | Result: " + setResult);

            /**
             * Try to use HVALS on a hash
             * We get an error
             *
             * Command: hvals bigboxstr
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try
            {
                hvalsResult = rdb.HashValues("bigboxstr");

                Console.WriteLine("Command: hvals bigboxstr | Result: " + String.Join(", ", hvalsResult));
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: hvals bigboxstr | Error: " + e.Message);
            }
        }
    }
}

Output:

Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868

Command: hgetall customer:1099:address | Result: street: 6414 Losee Rd, city: North Las Vegas, state: North Carolina, zip: 89086, phone: (702) 399-9939, country: United States, latitude: 36.27704, longitude: -115.115868

Command: hvals customer:1099:address | Result: 6414 Losee Rd, North Las Vegas, North Carolina, 89086, (702) 399-9939, United States, 36.27704, -115.115868

Command: hvals nonexistingkey | Result:

Command: set bigboxstr "some stiring value for HVALS command testing" | Result: True
Command: hvals bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use the method “HashValues” from StackExchange.Redis.
  • Signatures of the method are-
    RedisValue[] HashValues(RedisKey key, CommandFlags flags = CommandFlags.None)
<?php
// Redis HVALS command example in PHP

require 'vendor/autoload.php';

// Connect to Redis
$redisClient = new PredisClient([
    'scheme' => 'tcp',
    'host' => 'localhost',
    'port' => 6379,
]);


/**
 * Set hash field/value
 * Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
 * Result: (integer) 8
 */
$commandResult = $redisClient->hset("customer:1786:address",
    "street", "6414 Losee Rd",
    "city", "North Las Vegas",
    "state", "North Carolina",
    "zip", "89086",
    "phone", "(702) 399-9939",
    "country", "United States",
    "latitude", "36.27704",
    "longitude", "-115.115868",
);

echo "Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: " . $commandResult . "n";


/**
 * Check hash full data
 * Command: hgetall customer:1786:address
 * Result:
 *         1) "street"
 *         2) "6414 Losee Rd"
 *         3) "city"
 *         4) "North Las Vegas"
 *         5) "state"
 *         6) "North Carolina"
 *         7) "zip"
 *         8) "89086"
 *         9) "phone"
 *         10) "(702) 399-9939"
 *         11) "country"
 *         12) "United States"
 *         13) "latutude"
 *         14) "36.27704"
 *         15) "longitude"
 *         16) "-115.115868"
 */
$commandResult = $redisClient->hgetall("customer:1786:address");

echo "Command: hgetall customer:1099:address | Result:";
print_r($commandResult);

/**
 * Get all the values of hash
 *
 * Command: hvals customer:1786:address
 * Result:
 *          1) "6414 Losee Rd"
 *          2) "North Las Vegas"
 *          3) "North Carolina"
 *          4) "89086"
 *          5) "(702) 399-9939"
 *          6) "United States"
 *          7) "36.27704"
 *          8) "-115.115868"
 */
$commandResult = $redisClient->hvals("customer:1786:address");

echo "Command: hvals customer:1099:address | Result:";
print_r($commandResult);

/**
 * Use HVALS on a non existing key
 * We get (empty list)
 *
 * Command: hvals nonexistingkey
 * Result: (empty array)
 */
$commandResult = $redisClient->hvals("nonexistingkey");

echo "Command: hvals nonexistingkey | Result:";
print_r($commandResult);

/**
 * Set string value
 * Command: set bigboxstr "some stiring value for HVALS command testing"
 * Result: OK
 */
$commandResult = $redisClient->set("bigboxstr", "some stiring value for HVALS command testing");

echo "Command: set bigboxstr "some stiring value for HVALS command testing" | Result: " . $commandResult . "n";

/**
 * Try to use HVALS on a hash
 * We get an error
 * Command: hvals bigboxstr
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
    $commandResult = $redisClient->hvals("bigboxstr");

    echo "Command: hvals bigboxstr | Result:";
    print_r($commandResult);
} catch (Exception $e) {
    echo "Command: hvals bigboxstr | Error: " . $e->getMessage() . "n";
}

Output:

Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8

Command: hgetall customer:1099:address | Result:Array
(
    [street] => 6414 Losee Rd
    [city] => North Las Vegas
    [state] => North Carolina
    [zip] => 89086
    [phone] => (702) 399-9939
    [country] => United States
    [latitude] => 36.27704
    [longitude] => -115.115868
)

Command: hgetall customer:1099:address | Result:Array
(
    [0] => 6414 Losee Rd
    [1] => North Las Vegas
    [2] => North Carolina
    [3] => 89086
    [4] => (702) 399-9939
    [5] => United States
    [6] => 36.27704
    [7] => -115.115868
)

Command: hvals nonexistingkey | Result:Array
(
)

Command: set bigboxstr "some stiring value for HVALS command testing" | Result: OK
Command: hvals bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use the method “hvals” of predis.
  • Signature of the method is-
    hvals(string $key): array
# Redis HVALS command example in Python

import redis
import time

# Create Redis client
redisClient = redis.Redis(
    host="localhost", port=6379, username="default", password="", decode_responses=True
)


# Set hash field/value
# Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
# Result: (integer) 8
commandResult = redisClient.hset(
    "customer:1786:address",
    mapping={
        "street": "6414 Losee Rd",
        "city": "North Las Vegas",
        "state": "North Carolina",
        "zip": "89086",
        "phone": "(702) 399-9939",
        "country": "United States",
        "latitude": "36.27704",
        "longitude": "-115.115868",
    },
)

print(
    'Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: {}'.format(
        commandResult
    )
)


# Check hash full data
# Command: hgetall customer:1786:address
# Result:
#         1) "street"
#         2) "6414 Losee Rd"
#         3) "city"
#         4) "North Las Vegas"
#         5) "state"
#         6) "North Carolina"
#         7) "zip"
#         8) "89086"
#         9) "phone"
#         10) "(702) 399-9939"
#         11) "country"
#         12) "United States"
#         13) "latutude"
#         14) "36.27704"
#         15) "longitude"
#         16) "-115.115868"
commandResult = redisClient.hgetall("customer:1786:address")

print("Command: hgetall customer:1099:address | Result: {}".format(commandResult))

# Get all the values of hash
# Command: hvals customer:1786:address
# Result:
#          1) "6414 Losee Rd"
#          2) "North Las Vegas"
#          3) "North Carolina"
#          4) "89086"
#          5) "(702) 399-9939"
#          6) "United States"
#          7) "36.27704"
#          8) "-115.115868"
commandResult = redisClient.hvals("customer:1786:address")

print("Command: hgetall customer:1099:address | Result: {}".format(commandResult))

# Use HVALS on a non existing key
# We get (empty list)
# Command: hvals nonexistingkey
# Result: (empty array)
commandResult = redisClient.hvals("nonexistingkey")

print("Command: hvals nonexistingkey | Result: {}".format(commandResult))

# Set string value
# Command: set bigboxstr "some stiring value for HVALS command testing"
# Result: OK
commandResult = redisClient.set(
    "bigboxstr", "some stiring value for HVALS command testing"
)

print(
    'Command: set bigboxstr "some stiring value for HVALS command testing" | Result: {}'.format(
        commandResult
    )
)

# Try to use HVALS on a hash
# We get an error
# Command: hvals bigboxstr
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
    commandResult = redisClient.hvals("bigboxstr")

    print("Command: hvals bigboxstr | Result: {}".format(commandResult))
except Exception as error:
    print("Command: hvals bigboxstr | Error: ", error)

Output:

Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8

Command: hgetall customer:1099:address | Result: {'street': '6414 Losee Rd', 'city': 'North Las Vegas', 'state': 'North Carolina', 'zip': '89086', 'phone': '(702) 399-9939', 'country': 'United States', 'latitude': '36.27704', 'longitude': '-115.115868'}

Command: hgetall customer:1099:address | Result: ['6414 Losee Rd', 'North Las Vegas', 'North Carolina', '89086', '(702) 399-9939', 'United States', '36.27704', '-115.115868']

Command: hvals nonexistingkey | Result: []

Command: set bigboxstr "some stiring value for HVALS command testing" | Result: True
Command: hvals bigboxstr | Error:  WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use method “hget” from redis-py.
  • Signature of the method is –
    def hget(self, name: str, key: str)
# Redis HVALS command example in Ruby

require 'redis'

redis = Redis.new(host: "localhost", port: 6379)


# Set hash field/value
# Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
# Result: (integer) 8
commandResult = redis.hset("customer:1786:address", {
        "street" => "6414 Losee Rd",
        "city" => "North Las Vegas",
        "state" => "North Carolina",
        "zip" => "89086",
        "phone" => "(702) 399-9939",
        "country" => "United States",
        "latitude" => "36.27704",
        "longitude" => "-115.115868",
    },
)

print("Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: ", commandResult, "n")


# Check hash full data
# Command: hgetall customer:1786:address
# Result:
#         1) "street"
#         2) "6414 Losee Rd"
#         3) "city"
#         4) "North Las Vegas"
#         5) "state"
#         6) "North Carolina"
#         7) "zip"
#         8) "89086"
#         9) "phone"
#         10) "(702) 399-9939"
#         11) "country"
#         12) "United States"
#         13) "latutude"
#         14) "36.27704"
#         15) "longitude"
#         16) "-115.115868"
commandResult = redis.hgetall("customer:1786:address")

print("Command: hgetall customer:1099:address | Result: ", commandResult, "n")

# Get all the values of hash
# Command: hvals customer:1786:address
# Result:
#          1) "6414 Losee Rd"
#          2) "North Las Vegas"
#          3) "North Carolina"
#          4) "89086"
#          5) "(702) 399-9939"
#          6) "United States"
#          7) "36.27704"
#          8) "-115.115868"
commandResult = redis.hvals("customer:1786:address")

print("Command: hvals customer:1099:address | Result: ", commandResult, "n")

# Use HVALS on a non existing key
# We get (empty list)
# Command: hvals nonexistingkey
# Result: (empty array)
commandResult = redis.hvals("nonexistingkey")

print("Command: hvals nonexistingkey | Result: ", commandResult, "n")

# Set string value
# Command: set bigboxstr "some stiring value for HVALS command testing"
# Result: OK
commandResult = redis.set(
    "bigboxstr", "some stiring value for HVALS command testing"
)

print("Command: set bigboxstr "some stiring value for HVALS command testing" | Result: ", commandResult, "n")

# Try to use HVALS on a hash
# We get an error
# Command: hvals bigboxstr
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
    commandResult = redis.hvals("bigboxstr")

    print("Command: hvals bigboxstr | Result: ", commandResult, "n")
rescue => e
    print("Command: hvals bigboxstr | Error: ", e, "n")
end

Output:

Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8

Command: hgetall customer:1099:address | Result: {"street"=>"6414 Losee Rd", "city"=>"North Las Vegas", "state"=>"North Carolina", "zip"=>"89086", "phone"=>"(702) 399-9939", "country"=>"United States", "latitude"=>"36.27704", "longitude"=>"-115.115868"}

Command: hvals customer:1099:address | Result: ["6414 Losee Rd", "North Las Vegas", "North Carolina", "89086", "(702) 399-9939", "United States", "36.27704", "-115.115868"]

Command: hvals nonexistingkey | Result: []

Command: set bigboxstr "some stiring value for HVALS command testing" | Result: OK
Command: hvals bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use method “hvals” from the redis-rb.
  • Signature of the method is-
    # @param [String] key
    # @return [Array<String>]

    def hvals(key)

Source Code

Use the following links to get the source code used in this article-

Related Commands

CommandDetails
HSETNX Command Details
HMGET Command Details
HGETALL Command Details
HSET Command Details

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.