Redis Command: HGETALL

Summary

Command NameHGETAll
UsageGet all fields and value of a hash
Group hash
ACL Category@read
@hash
@fast
Time ComplexityO(N)
FlagREADONLY
Arity2

Notes

  • In the time complexity representation, N is the size of the hash.

Signature

HGETALL <key>

Usage

Get all the fields and values of a hash. By only providing the key of the hash we get all the fields and values. We do not need to be aware of specific field(s).

Notes

  • The response contains the field and value of the field in 2 separate lines.

Arguments

ParameterDescriptionNameType
<key>Name of the key of the hashkeykey

Return Value

Return valueCase for the return valueType
All fields/values of hashAll fields/values of the hashobject
(empty array)If the key does not existnull
errorOn successful we get a map of the field and values of the hasherror

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 HGETAll command-

# Redis HGETALL command examples

# Set some has fields using HSET
127.0.0.1:6379> hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States"
(integer) 6

# Get all field/value of the hash
127.0.0.1:6379> hgetall customer:1099:address
 1) "street"
 2) "5342 Hollister Ave"
 3) "city"
 4) "Santa Barbara"
 5) "state"
 6) "California"
 7) "zip"
 8) "93111"
 9) "phone"
10) "(805) 845-0111"
11) "country"
12) "United States"

# Try to use HGETALL on a non existing key
# we get (empty array)
127.0.0.1:6379> hgetall somenonexistingkey
(empty array)

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

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

Notes

  • On success, we get the field and value in separate lines, like below-
    1) field1 name
    2) value of field1
    3) field2 name
    4) field2 value
    ...
  • If the key does not exist we get-
    (empty array)

Code Implementations

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

// Redis HGETALL 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 some has fields usign HSET
	*
	* Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States"
	* Result: (integer) 6
	 */

	hsetResult, err := rdb.HSet(ctx, "customer:1099:address",
		"street", "342 Hollister Ave",
		"city", "Santa Barbara",
		"state", "California",
		"zip", "93111",
		"phone", "(805) 845-0111",
		"country", "United States",
	).Result()

	if err != nil {
		fmt.Println("Command: hset customer:1099:address street \"5342 Hollister Ave\" city \"Santa Barbara\" state California zip 93111 phone \"(805) 845-0111\" country \"United States\" | Error: " + err.Error())
	}

	fmt.Println("Command: hset customer:1099:address street \"5342 Hollister Ave\" city \"Santa Barbara\" state California zip 93111 phone \"(805) 845-0111\" country \"United States\" | Result: ", hsetResult)

	/**
	* Get all field/value of the hash
	*
	* Command: hgetall customer:1099:address
	* Result:
	*          1) "street"
	*          2) "5342 Hollister Ave"
	*          3) "city"
	*          4) "Santa Barbara"
	*          5) "state"
	*          6) "California"
	*          7) "zip"
	*          8) "93111"
	*          9) "phone"
	*          10) "(805) 845-0111"
	*          11) "country"
	*          12) "United States"
	 */
	hgetAllResult, err := rdb.HGetAll(ctx, "customer:1099:address").Result()

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

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

	/**
	* Try to use HGETALL on a non existing key
	* we get (empty array)
	*
	* Command: hgetall somenonexistingkey
	* Result: (empty array)
	 */
	hgetAllResult, err = rdb.HGetAll(ctx, "nonexistinghash").Result()

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

	fmt.Println("Command: hgetall somenonexistingkey | Result: ", hgetAllResult)

	/**
	* Set a string value
	*
	* Command: set bigboxstr "some string in the box"
	* Result: OK
	 */
	setResult, err := rdb.Set(ctx, "bigboxstr", "some string in the box", 0).Result()

	if err != nil {
		fmt.Println("Command: set bigboxstr \"some string in the box\" | Error: " + err.Error())
	}

	fmt.Println("Command: set bigboxstr \"some string in the box\" | Result: " + setResult)

	/**
	* Try to use the HGETALL on a string type of key
	* We get an error
	*
	* Command: hgetall bigboxstr
	* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
	 */
	hgetAllResult, err = rdb.HGetAll(ctx, "bigboxstr").Result()

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

	fmt.Println("Command: hgetall bigboxstr | Result: ", hgetAllResult)

}

Output:

Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States" | Result:  6

Command: hget customer:1099:address | Result:  map[city:Santa Barbara country:United States phone:(805) 845-0111 state:California street:342 Hollister Ave zip:93111]

Command: hgetall somenonexistingkey | Result:  map[]

Command: set bigboxstr "some string in the box" | Result: OK
Command: hgetall bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hgetall bigboxstr | Result:  map[]

Notes

  • Use “HGetAll” method from redis-go module.
  • Signature of the method is-
    HGetAll(ctx context.Context, key string) *MapStringStringCmd
// Redis HGETALL 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 some has fields usign HSET
 *
 * Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States"
 * Result: (integer) 6
 */
let commandResult = await redisClient.hSet("customer:1099:address", {
  street: "342 Hollister Ave",
  city: "Santa Barbara",
  state: "California",
  zip: "93111",
  phone: "(805) 845-0111",
  country: "United States",
});

console.log(
  'Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States" | Result: ',
  commandResult
);

/**
 * Get all field/value of the hash
 *
 * Command: hgetall customer:1099:address
 * Result:
 *          1) "street"
 *          2) "5342 Hollister Ave"
 *          3) "city"
 *          4) "Santa Barbara"
 *          5) "state"
 *          6) "California"
 *          7) "zip"
 *          8) "93111"
 *          9) "phone"
 *          10) "(805) 845-0111"
 *          11) "country"
 *          12) "United States"
 */
commandResult = await redisClient.hGetAll("customer:1099:address");

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

/**
 * Try to use HGETALL on a non existing key
 * we get (empty array)
 *
 * Command: hgetall somenonexistingkey
 * Result: (empty array)
 */
commandResult = await redisClient.hGetAll("nonexistinghash");

console.log("Command: hgetall somenonexistingkey | Result: ", commandResult);

/**
 * Set a string value
 *
 * Command: set bigboxstr "some string in the box"
 * Result: OK
 */
commandResult = await redisClient.set("bigboxstr", "some string in the box");

console.log(
  'Command: set bigboxstr "some string in the box" | Result: ',
  commandResult
);

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

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

process.exit(0);

Output:

Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States" | Result:  6

Command: hget customer:1099:address | Result:  [Object: null prototype] {
  street: '342 Hollister Ave',
  city: 'Santa Barbara',
  state: 'California',
  zip: '93111',
  phone: '(805) 845-0111',
  country: 'United States'
}

Command: hgetall somenonexistingkey | Result:  [Object: null prototype] {}

Command: set bigboxstr "some string in the box" | Result:  OK
Command: hgetall bigboxstr | Error:  [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]

Notes

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

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

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

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

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

            /**
             * Set some has fields usign HSET
             *
             * Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States"
             * Result: (integer) 6
             */
            Map<String, String> hashData = new HashMap<>() {{
                put("street", "342 Hollister Ave");
                put("city", "Santa Barbara");
                put("state", "California");
                put("zip", "93111");
                put("phone", "(805) 845-0111");
                put("country", "United States");
            }};
            long hsetResult = jedis.hset("customer:1099:address", hashData);

            System.out.println("Command: hset customer:1099:address street \"5342 Hollister Ave\" city \"Santa Barbara\" state California zip 93111 phone \"(805) 845-0111\" country \"United States\" | Result: " + hsetResult);

            /**
             * Get all field/value of the hash
             *
             * Command: hgetall customer:1099:address
             * Result:
             *          1) "street"
             *          2) "5342 Hollister Ave"
             *          3) "city"
             *          4) "Santa Barbara"
             *          5) "state"
             *          6) "California"
             *          7) "zip"
             *          8) "93111"
             *          9) "phone"
             *          10) "(805) 845-0111"
             *          11) "country"
             *          12) "United States"
             */
            Map<String, String> hgetAllResult = jedis.hgetAll("customer:1099:address");

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

            /**
             * Try to use HGETALL on a non existing key
             * we get (empty array)
             *
             * Command: hgetall somenonexistingkey
             * Result: (empty array)
             */
            hgetAllResult = jedis.hgetAll("nonexistinghash");

            System.out.println("Command: hgetall somenonexistingkey | Result: " + hgetAllResult);

            /**
             * Set a string value
             *
             * Command: set bigboxstr "some string in the box"
             * Result: OK
             */
            String setResult = jedis.set("bigboxstr", "some string in the box");

            System.out.println("Command: set bigboxstr \"some string in the box\" | Result: " + setResult);

            /**
             * Try to use the HGETALL on a string type of key
             * We get an error
             *
             * Command: hgetall bigboxstr
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try {
                hgetAllResult = jedis.hgetAll("bigboxstr");

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

        }

        jedisPool.close();
    }
}

Output:

Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States" | Result: 6

Command: hget customer:1099:address | Result: {zip=93111, country=United States, state=California, city=Santa Barbara, phone=(805) 845-0111, street=342 Hollister Ave}

Command: hgetall somenonexistingkey | Result: {}

Command: set bigboxstr "some string in the box" | Result: OK
Command: hgetall bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

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

using StackExchange.Redis;

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

            /**
             * Set some has fields usign HSET
             *
             * Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States"
             * Result: (integer) 6
             */
            HashEntry[] hashData = new HashEntry[] {
                new HashEntry("street", "5342 Hollister Ave"),
                new HashEntry("city", "Santa Barbara"),
                new HashEntry("state", "California"),
                new HashEntry("zip", "93111"),
                new HashEntry("phone", "(805) 845-0111"),
                new HashEntry("country", "United States")
            };
            rdb.HashSet("customer:1099:address", hashData);

            Console.WriteLine("Command: hset customer:1099:address street \"5342 Hollister Ave\" city \"Santa Barbara\" state California zip 93111 phone \"(805) 845-0111\" country \"United States\"");

            /**
             * Get all field/value of the hash
             *
             * Command: hgetall customer:1099:address
             * Result:
             *          1) "street"
             *          2) "5342 Hollister Ave"
             *          3) "city"
             *          4) "Santa Barbara"
             *          5) "state"
             *          6) "California"
             *          7) "zip"
             *          8) "93111"
             *          9) "phone"
             *          10) "(805) 845-0111"
             *          11) "country"
             *          12) "United States"
             */
            HashEntry[] hgetAllResult = rdb.HashGetAll("customer:1099:address");

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

            /**
             * Try to use HGETALL on a non existing key
             * we get (empty array)
             *
             * Command: hgetall somenonexistingkey
             * Result: (empty array)
             */
            hgetAllResult = rdb.HashGetAll("nonexistinghash");

            Console.WriteLine("Command: hgetall somenonexistingkey | Result: " + string.Join(", ", hgetAllResult));

            /**
             * Set a string value
             *
             * Command: set bigboxstr "some string in the box"
             * Result: OK
             */
            bool setResult = rdb.StringSet("bigboxstr", "some string in the box");
            Console.WriteLine("Command: set bigboxstr \"some string in the box\" | Result: " + setResult);

            /**
             * Try to use the HGETALL on a string type of key
             * We get an error
             *
             * Command: hgetall bigboxstr
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try
            {
                hgetAllResult = rdb.HashGetAll("bigboxstr");

                Console.WriteLine("Command: hgetall bigboxstr | Result: " + hgetAllResult);
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: hgetall bigboxstr | Error: " + e.Message);
            }
        }
    }
}

Output:

Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States"

Command: hgetall customer:1099:address | Result: street: 5342 Hollister Ave, city: Santa Barbara, state: California, zip: 93111, phone: (805) 845-0111, country: United States

Command: hgetall somenonexistingkey | Result:

Command: set bigboxstr "some string in the box" | Result: True
Command: hgetall bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

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

require 'vendor/autoload.php';

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


/**
 * Set some has fields usign HSET
 *
 * Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States"
 * Result: (integer) 6
 */
$commandResult = $redisClient->hset("customer:1099:address",
    "street", "342 Hollister Ave",
    "city", "Santa Barbara",
    "state", "California",
    "zip", "93111",
    "phone", "(805) 845-0111",
    "country", "United States",
);

echo "Command: hset customer:1099:address street \"5342 Hollister Ave\" city \"Santa Barbara\" state California zip 93111 phone \"(805) 845-0111\" country \"United States\" | Result: " . $commandResult . "\n";

/**
 * Get all field/value of the hash
 *
 * Command: hgetall customer:1099:address
 * Result:
 *          1) "street"
 *          2) "5342 Hollister Ave"
 *          3) "city"
 *          4) "Santa Barbara"
 *          5) "state"
 *          6) "California"
 *          7) "zip"
 *          8) "93111"
 *          9) "phone"
 *          10) "(805) 845-0111"
 *          11) "country"
 *          12) "United States"
 */
$commandResult = $redisClient->hgetall("customer:1099:address");

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

/**
 * Try to use HGETALL on a non existing key
 * we get (empty array)
 *
 * Command: hgetall somenonexistingkey
 * Result: (empty array)
 */
$commandResult = $redisClient->hgetall("nonexistinghash");

echo "Command: hgetall somenonexistingkey | Result:";
print_r($commandResult);

/**
 * Set a string value
 *
 * Command: set bigboxstr "some string in the box"
 * Result: OK
 */
$commandResult = $redisClient->set("bigboxstr", "some string in the box");

echo "Command: set bigboxstr \"some string in the box\" | Result: " . $commandResult . "\n";

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

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

Output:

Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States" | Result: 6

Command: hgetall customer:1099:address | Result:Array
(
    [street] => 342 Hollister Ave
    [city] => Santa Barbara
    [state] => California
    [zip] => 93111
    [phone] => (805) 845-0111
    [country] => United States
)

Command: hgetall somenonexistingkey | Result:Array
(
)

Command: set bigboxstr "some string in the box" | Result: OK
Command: hgetall bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use the method “hgetall” of predis.
  • Signature of the method is-
    hgetall(string $key): array
# Redis HSETALL 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 some has fields usign HSET
# Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States"
# Result: (integer) 6
commandResult = redisClient.hset("customer:1099:address", mapping= {
    "street": "342 Hollister Ave",
"city": "Santa Barbara",
"state": "California",
"zip": "93111",
"phone": "(805) 845-0111",
"country": "United States",
    })

print("Command: hset customer:1099:address street \"5342 Hollister Ave\" city \"Santa Barbara\" state California zip 93111 phone \"(805) 845-0111\" country \"United States\" | Result: {}".format(commandResult))

# Get all field/value of the hash
# Command: hgetall customer:1099:address
# Result:
#          1) "street"
#          2) "5342 Hollister Ave"
#          3) "city"
#          4) "Santa Barbara"
#          5) "state"
#          6) "California"
#          7) "zip"
#          8) "93111"
#          9) "phone"
#          10) "(805) 845-0111"
#          11) "country"
#          12) "United States"
commandResult = redisClient.hgetall("customer:1099:address")

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

# Try to use HGETALL on a non existing key
# we get (empty array)
# Command: hgetall somenonexistingkey
# Result: (empty array)
commandResult = redisClient.hgetall("nonexistinghash")

print("Command: hgetall somenonexistingkey | Result: {}".format(commandResult))

# Set a string value
# Command: set bigboxstr "some string in the box"
# Result: OK
commandResult = redisClient.set("bigboxstr", "some string in the box")

print("Command: set bigboxstr \"some string in the box\" | Result: {}".format(commandResult))

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

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

Output:

Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States" | Result: 6

Command: hgetall customer:1099:address | Result: {'street': '342 Hollister Ave', 'city': 'Santa Barbara', 'state': 'California', 'zip': '93111', 'phone': '(805) 845-0111', 'country': 'United States'}

Command: hgetall somenonexistingkey | Result: {}

Command: set bigboxstr "some string in the box" | Result: True
Command: hgetall bigboxstr | Error:  WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use method “hgetall” from redis-py.
  • Signature of the method is –
    def hgetall(self, name: str) -> Union[Awaitable[dict], dict]
# Redis HGETALL command example in Ruby

require 'redis'

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


# Set some has fields usign HSET
# Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States"
# Result: (integer) 6
commandResult = redis.hset("customer:1099:address", {
    "street" => "342 Hollister Ave",
"city" => "Santa Barbara",
"state" => "California",
"zip" => "93111",
"phone" => "(805) 845-0111",
"country" => "United States",
    })

print("Command: hset customer:1099:address street \"5342 Hollister Ave\" city \"Santa Barbara\" state California zip 93111 phone \"(805) 845-0111\" country \"United States\" | Result: ", commandResult, "\n")

# Get all field/value of the hash
# Command: hgetall customer:1099:address
# Result:
#          1) "street"
#          2) "5342 Hollister Ave"
#          3) "city"
#          4) "Santa Barbara"
#          5) "state"
#          6) "California"
#          7) "zip"
#          8) "93111"
#          9) "phone"
#          10) "(805) 845-0111"
#          11) "country"
#          12) "United States"
commandResult = redis.hgetall("customer:1099:address")

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

# Try to use HGETALL on a non existing key
# we get (empty array)
# Command: hgetall somenonexistingkey
# Result: (empty array)
commandResult = redis.hgetall("nonexistinghash")

print("Command: hgetall somenonexistingkey | Result: ", commandResult, "\n")

# Set a string value
# Command: set bigboxstr "some string in the box"
# Result: OK
commandResult = redis.set("bigboxstr", "some string in the box")

print("Command: set bigboxstr \"some string in the box\" | Result: ", commandResult, "\n")

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

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

Output:

Command: hset customer:1099:address street "5342 Hollister Ave" city "Santa Barbara" state California zip 93111 phone "(805) 845-0111" country "United States" | Result: 6

Command: hgetall customer:1099:address | Result: {"street"=>"342 Hollister Ave", "city"=>"Santa Barbara", "state"=>"California", "zip"=>"93111", "phone"=>"(805) 845-0111", "country"=>"United States"}

Command: hgetall somenonexistingkey | Result: {}

Command: set bigboxstr "some string in the box" | Result: OK
Command: hgetall bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

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

    def hgetall(key)

Source Code

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

Related Commands

CommandDetails
HGET Command Details
HSETNX Command Details
HMGET Command Details
HSET Command Details

Leave a Comment


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