Redis Command: HSET

Summary

Command NameHSET
UsageSet hash field(s)
Group hash
ACL Category@write
@hash
@fast
Time ComplexityO(N)
FlagWRITE
DENYOOM
FAST
Arity-4

Notes

  • Time complexity of this command is O(N), where N is the number of filed/value. If a single field/value is being set then the complexity is O(1).

Signature

HSET <key> <field> <value> [ <field> <value> ... ]

Usage

Create a new hash and set fields, or change the existing value of field(s) of a hash.

Notes

  • If the key does not exist then it will be created.
  • If the field does not exist then it will be created. And if the field exists then the value will be overwritten.

Arguments

ParameterGroupDescriptionNameTypeMultiple
<key>Name of the key of the hashkeykey
<field>data[block]Name of the fieldfieldstringTrue
<value>data[block]Value for the fieldvaluestringTrue

Return Value

Return valueCase for the return valueType
Number of new fieldsOn successful execution of the commandinteger
errorIf applied on wrong data typeerror

Notes

  • On success, the command returns the number of fields created by that command. If some fields are updated, then those are not included in the resulting number.
  • 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 HSET command usage-

# Redis HSET command examples

# Set "street" field of hash
127.0.0.1:6379> hset customer:103:address street "965 Lakeville St"
(integer) 1

# Check hash
127.0.0.1:6379> hgetall customer:103:address
1) "street"
2) "965 Lakeville St"

# Set multiple fields of the hash
127.0.0.1:6379> hset customer:103:address city Petaluma state California zip 94952 country "United States"
(integer) 4

# Check hash
127.0.0.1:6379> hgetall customer:103:address
 1) "street"
 2) "965 Lakeville St"
 3) "city"
 4) "Petaluma"
 5) "state"
 6) "California"
 7) "zip"
 8) "94952"
 9) "country"
10) "United States"

# Set new fields to hash, also update some existing fields
127.0.0.1:6379> hset customer:103:address city hayward  zip 94566 phone (503)-445-4454
(integer) 1

# Check hash
127.0.0.1:6379> hgetall customer:103:address
 1) "street"
 2) "965 Lakeville St"
 3) "city"
 4) "hayward"
 5) "state"
 6) "California"
 7) "zip"
 8) "94566"
 9) "country"
10) "United States"
11) "phone"
12) "(503)-445-4454"

# Try to set the same field multiple times
# The later provided value is saved
127.0.0.1:6379> hset customer:103:address zip 94555 zip 94599
(integer) 0

# Check set value
127.0.0.1:6379> hgetall customer:103:address
 1) "street"
 2) "965 Lakeville St"
 3) "city"
 4) "hayward"
 5) "state"
 6) "California"
 7) "zip"
 8) "94599"
 9) "country"
10) "United States"
11) "phone"
12) "(503)-445-4454"

# Get single field of hash
127.0.0.1:6379> hget customer:103:address phone
"(503)-445-4454"

# Get multiple fields of hash
127.0.0.1:6379> hmget customer:103:address zip phone country
1) "94599"
2) "(503)-445-4454"
3) "United States"

# Set a string key
127.0.0.1:6379> set bigboxstr "some string value here"
OK

# Try to apply HSET on the string data type
# We get an error
127.0.0.1:6379> hset bigboxstr testfield "test val"
(error) WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • If the same field of a hash is set multiple time in the same HSET command, then the value provided later is saved to the field.

Code Implementations

Here are the usage examples of the Redis HSET command in different programming languages.

// Redis HSET 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 "street" field of hash
	*
	* Command: hset customer:103:address street "965 Lakeville St"
	* Result: (integer) 1
	 */
	hsetResult, err := rdb.HSet(ctx, "customer:103:address", "street", "965 Lakeville St").Result()

	if err != nil {
		fmt.Println("Command: hset customer:103:address street "965 Lakeville St" | Error: " + err.Error())
	}

	fmt.Println("Command: hset customer:103:address street "965 Lakeville St" | Result: ", hsetResult)

	/**
	* Check hash
	*
	* Command: hgetall customer:103:address
	* Result:
	*      1) "street"
	*      2) "965 Lakeville St"
	 */
	hgetAllResult, err := rdb.HGetAll(ctx, "customer:103:address").Result()

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

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

	/**
	* Set multiple fields of the hash
	*
	* Command: hset customer:103:address city Petaluma state California zip 94952 country "United States"
	* Result: (integer) 4
	 */
	hsetResult, err = rdb.HSet(ctx, "customer:103:address",
		"city", "Petaluma",
		"state", "California",
		"zip", "94952",
		"country", "United States",
	).Result()

	if err != nil {
		fmt.Println("Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Error: " + err.Error())
	}

	fmt.Println("Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: ", hsetResult)

	/**
	* Check hash
	*
	* Command: hgetall customer:103:address
	* Result:
	*      1) "street"     2) "965 Lakeville St"
	*      3) "city"       4) "Petaluma"
	*      5) "state"      6) "California"
	*      7) "zip"        8) "94952"
	*      9) "country"    10) "United States"
	 */
	hgetAllResult, err = rdb.HGetAll(ctx, "customer:103:address").Result()

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

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

	/**
	* Set new fields to hash, also update some existing fields
	*
	* Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454
	* Result: (integer) 1
	 */
	hsetResult, err = rdb.HSet(ctx, "customer:103:address",
		"city", "hayward",
		"zip", "94566",
		"phone", "(503)-445-4454",
	).Result()

	if err != nil {
		fmt.Println("Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Error: " + err.Error())
	}

	fmt.Println("Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result: ", hsetResult)

	/**
	* Check hash
	*
	* Command: hgetall customer:103:address
	* Result:
	*      1) "street"     2) "965 Lakeville St"
	*      3) "city"       4) "hayward"
	*      5) "state"      6) "California"
	*      7) "zip"        8) "94566"
	*      9) "country"    10) "United States"
	*      11) "phone"     12) "(503)-445-4454"
	 */
	hgetAllResult, err = rdb.HGetAll(ctx, "customer:103:address").Result()

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

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

	/**
	* Try to set the same field multiple times
	* The later provided value is saved
	*
	* Command: hset customer:103:address zip 94555 zip 94599
	* Result: (integer) 0
	 */
	hsetResult, err = rdb.HSet(ctx, "customer:103:address",
		"zip", "94555",
		"zip", "94599",
	).Result()

	if err != nil {
		fmt.Println("Command: hset customer:103:address zip 94555 zip 94599 | Error: " + err.Error())
	}

	fmt.Println("Command: hset customer:103:address zip 94555 zip 94599 | Result: ", hsetResult)

	/**
	* Check set value
	*
	* Command: hgetall customer:103:address
	* Result:
	*      1) "street"     2) "965 Lakeville St"
	*      3) "city"       4) "hayward"
	*      5) "state"      6) "California"
	*      7) "zip"        8) "94599"
	*      9) "country"    10) "United States"
	*      11) "phone"     12) "(503)-445-4454"
	 */
	hgetAllResult, err = rdb.HGetAll(ctx, "customer:103:address").Result()

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

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

	/**
	* Get single field of hash
	*
	* Command: hget customer:103:address phone
	* Result: "(503)-445-4454"
	 */
	hgetResult, err := rdb.HGet(ctx, "customer:103:address", "phone").Result()

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

	fmt.Println("Command: hget customer:103:address phone | Result: " + hgetResult)

	/**
	* Get multiple fields of hash
	*
	* Command: hmget customer:103:address zip phone country
	* Result:
	*      1) "94599"
	*      2) "(503)-445-4454"
	*      3) "United States"
	 */
	hmgetResult, err := rdb.HMGet(ctx, "customer:103:address", "zip", "phone", "country").Result()

	if err != nil {
		fmt.Println("Command: hmget customer:103:address zip phone country | Error: " + err.Error())
	}

	fmt.Println("Command: hmget customer:103:address zip phone country | Result: ", hmgetResult)

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

	if err != nil {
		fmt.Println("Command: set bigboxstr "some string value here" | Error: " + err.Error())
	}

	fmt.Println("Command: set bigboxstr "some string value here" | Result: " + setResult)

	/**
	* Try to apply HSET on the string data type
	* We get an error
	*
	* Command: hset bigboxstr testfield "test val"
	* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
	 */
	hsetResult, err = rdb.HSet(ctx, "bigboxstr", "testfield", "test val").Result()

	if err != nil {
		fmt.Println("Command: hset bigboxstr testfield "test val" | Error: " + err.Error())
	}

	fmt.Println("Command: hset bigboxstr testfield "test val" | Result: ", hsetResult)

}

Output:

Command: hset customer:103:address street "965 Lakeville St" | Result:  1
Command: hgetall customer:103:address | Result:  map[street:965 Lakeville St]

Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result:  4
Command: hgetall customer:103:address | Result:  map[city:Petaluma country:United States state:California street:965 Lakeville St zip:94952]

Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result:  1
Command: hgetall customer:103:address | Result:  map[city:hayward country:United States phone:(503)-445-4454 state:California street:965 Lakeville St zip:94566]

Command: hset customer:103:address zip 94555 zip 94599 | Result:  0
Command: hgetall customer:103:address | Result:  map[city:hayward country:United States phone:(503)-445-4454 state:California street:965 Lakeville St zip:94599]

Command: hget customer:103:address phone | Result: (503)-445-4454
Command: hmget customer:103:address zip phone country | Result:  [94599 (503)-445-4454 United States]

Command: set bigboxstr "some string value here" | Result: OK
Command: hset bigboxstr testfield "test val" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hset bigboxstr testfield "test val" | Result:  0

Notes

  • Use “HSet” method from redis-go module.
  • Signature of the method is-
    HSet(ctx context.Context, key string, values ...interface{}) *IntCmd
// Redis HSET 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 "street" field of hash
 *
 * Command: hset customer:103:address street "965 Lakeville St"
 * Result: (integer) 1
 */
let commandResult = await redisClient.hSet(
  "customer:103:address",
  "street",
  "965 Lakeville St"
);

console.log(
  'Command: hset customer:103:address street "965 Lakeville St" | Result: ' +
    commandResult
);

/**
 * Check hash
 *
 * Command: hgetall customer:103:address
 * Result:
 *      1) "street"
 *      2) "965 Lakeville St"
 */
commandResult = await redisClient.hGetAll("customer:103:address");

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

/**
 * Set multiple fields of the hash
 *
 * Command: hset customer:103:address city Petaluma state California zip 94952 country "United States"
 * Result: (integer) 4
 */
commandResult = await redisClient.hSet("customer:103:address", {
  city: "Petaluma",
  state: "California",
  zip: "94952",
  country: "United States",
});

console.log(
  'Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: ' +
    commandResult
);

/**
 * Check hash
 *
 * Command: hgetall customer:103:address
 * Result:
 *      1) "street"     2) "965 Lakeville St"
 *      3) "city"       4) "Petaluma"
 *      5) "state"      6) "California"
 *      7) "zip"        8) "94952"
 *      9) "country"    10) "United States"
 */
commandResult = await redisClient.hGetAll("customer:103:address");

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

/**
 * Set new fields to hash, also update some existing fields
 *
 * Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454
 * Result: (integer) 1
 */
commandResult = await redisClient.hSet("customer:103:address", {
  city: "hayward",
  zip: "94566",
  phone: "(503)-445-4454",
});

console.log(
  "Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result: " +
    commandResult
);

/**
 * Check hash
 *
 * Command: hgetall customer:103:address
 * Result:
 *      1) "street"     2) "965 Lakeville St"
 *      3) "city"       4) "hayward"
 *      5) "state"      6) "California"
 *      7) "zip"        8) "94566"
 *      9) "country"    10) "United States"
 *      11) "phone"     12) "(503)-445-4454"
 */
commandResult = await redisClient.hGetAll("customer:103:address");

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

/**
 * Try to set the same field multiple times
 * The later provided value is saved
 *
 * Command: hset customer:103:address zip 94555 zip 94599
 * Result: (integer) 0
 */
commandResult = await redisClient.hSet("customer:103:address", [
  ["zip", "94555"],
  ["zip", "94599"],
]);

console.log(
  "Command: hset customer:103:address zip 94555 zip 94599 | Result: " +
    commandResult
);

/**
 * Check set value
 *
 * Command: hgetall customer:103:address
 * Result:
 *      1) "street"     2) "965 Lakeville St"
 *      3) "city"       4) "hayward"
 *      5) "state"      6) "California"
 *      7) "zip"        8) "94599"
 *      9) "country"    10) "United States"
 *      11) "phone"     12) "(503)-445-4454"
 */
commandResult = await redisClient.hGetAll("customer:103:address");

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

/**
 * Get single field of hash
 *
 * Command: hget customer:103:address phone
 * Result: "(503)-445-4454"
 */
commandResult = await redisClient.hGet("customer:103:address", "phone");

console.log(
  "Command: hget customer:103:address phone | Result: " + commandResult
);

/**
 * Get multiple fields of hash
 *
 * Command: hmget customer:103:address zip phone country
 * Result:
 *      1) "94599"
 *      2) "(503)-445-4454"
 *      3) "United States"
 */
commandResult = await redisClient.hmGet(
  "customer:103:address",
  "zip",
  "phone",
  "country"
);

console.log(
  "Command: hmget customer:103:address zip phone country | Result: ",
  commandResult
);

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

console.log(
  'Command: set bigboxstr "some string value here" | Result: ' + commandResult
);

/**
 * Try to apply HSET on the string data type
 * We get an error
 *
 * Command: hset bigboxstr testfield "test val"
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
  commandResult = await redisClient.hSet("bigboxstr", "testfield", "test val");

  console.log(
    'Command: hset bigboxstr testfield "test val" | Result: ' + commandResult
  );
} catch (err) {
  console.log('Command: hset bigboxstr testfield "test val" | Error: ', err);
}

process.exit(0);

Output:

Command: hset customer:103:address street "965 Lakeville St" | Result: 1
Command: hgetall customer:103:address | Result:  [Object: null prototype] { street: '965 Lakeville St' }

Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: 4
Command: hgetall customer:103:address | Result:  [Object: null prototype] {
  street: '965 Lakeville St',
  city: 'Petaluma',
  state: 'California',
  zip: '94952',
  country: 'United States'
}

Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result: 1
Command: hgetall customer:103:address | Result:  [Object: null prototype] {
  street: '965 Lakeville St',
  city: 'hayward',
  state: 'California',
  zip: '94566',
  country: 'United States',
  phone: '(503)-445-4454'
}

Command: hset customer:103:address zip 94555 zip 94599 | Result: 0
Command: hgetall customer:103:address | Result:  [Object: null prototype] {
  street: '965 Lakeville St',
  city: 'hayward',
  state: 'California',
  zip: '94599',
  country: 'United States',
  phone: '(503)-445-4454'
}

Command: hget customer:103:address phone | Result: (503)-445-4454

Command: hmget customer:103:address zip phone country | Result:  [ '94599' ]

Command: set bigboxstr "some string value here" | Result: OK
Command: hset bigboxstr testfield "test val" | Error:  [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]

Notes

  • Use the function “hSet” of node-redis.
  • Signature of the method-
    function hSet(...[key, value, fieldValue]: SingleFieldArguments | MultipleFieldsArguments)
  • Other related types are defined as-
    type SingleFieldArguments = [...generic: GenericArguments, field: Types, value: Types];
    type MultipleFieldsArguments = [...generic: GenericArguments, value: HSETObject | HSETMap | HSETTuples];
// Redis HSET 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 Hset {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("localhost", 6379);

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

            /**
             * Set "street" field of hash
             *
             * Command: hset customer:103:address street "965 Lakeville St"
             * Result: (integer) 1
             */
            long hsetResult = jedis.hset("customer:103:address", "street", "965 Lakeville St");

            System.out.println("Command: hset customer:103:address street "965 Lakeville St" | Result: " + hsetResult);

            /**
             * Check hash
             *
             * Command: hgetall customer:103:address
             * Result:
             *      1) "street"
             *      2) "965 Lakeville St"
             */
            Map<String, String> hgetAllResult = jedis.hgetAll("customer:103:address");

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

            /**
             * Set multiple fields of the hash
             *
             * Command: hset customer:103:address city Petaluma state California zip 94952 country "United States"
             * Result: (integer) 4
             */
            Map<String, String> hashData = new HashMap<>() {{
                put("city", "Petaluma");
                put("state", "California");
                put("zip", "94952");
                put("country", "United States");
            }};
            hsetResult = jedis.hset("customer:103:address", hashData);

            System.out.println("Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: " + hsetResult);

            /**
             * Check hash
             *
             * Command: hgetall customer:103:address
             * Result:
             *      1) "street"     2) "965 Lakeville St"
             *      3) "city"       4) "Petaluma"
             *      5) "state"      6) "California"
             *      7) "zip"        8) "94952"
             *      9) "country"    10) "United States"
             */
            hgetAllResult = jedis.hgetAll("customer:103:address");

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

            /**
             * Set new fields to hash, also update some existing fields
             *
             * Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454
             * Result: (integer) 1
             */
            hashData = new HashMap<>() {{
                put("city", "hayward");
                put("zip", "94566");
                put("phone", "(503)-445-4454");
            }};
            hsetResult = jedis.hset("customer:103:address", hashData);

            System.out.println("Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result: " + hsetResult);

            /**
             * Check hash
             *
             * Command: hgetall customer:103:address
             * Result:
             *      1) "street"     2) "965 Lakeville St"
             *      3) "city"       4) "hayward"
             *      5) "state"      6) "California"
             *      7) "zip"        8) "94566"
             *      9) "country"    10) "United States"
             *      11) "phone"     12) "(503)-445-4454"
             */
            hgetAllResult = jedis.hgetAll("customer:103:address");

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

            /**
             * Try to set the same field multiple times
             * The later provided value is saved
             *
             * Command: hset customer:103:address zip 94555 zip 94599
             * Result: (integer) 0
             */
            hashData = new HashMap<>() {{
                put("zip", "94555");
                put("zip", "94599");
            }};
            hsetResult = jedis.hset("customer:103:address", hashData);

            System.out.println("Command: hset customer:103:address zip 94555 zip 94599 | Result: " + hsetResult);

            /**
             * Check set value
             *
             * Command: hgetall customer:103:address
             * Result:
             *      1) "street"     2) "965 Lakeville St"
             *      3) "city"       4) "hayward"
             *      5) "state"      6) "California"
             *      7) "zip"        8) "94599"
             *      9) "country"    10) "United States"
             *      11) "phone"     12) "(503)-445-4454"
             */
            hgetAllResult = jedis.hgetAll("customer:103:address");

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

            /**
             * Get single field of hash
             *
             * Command: hget customer:103:address phone
             * Result: "(503)-445-4454"
             */
            String hgetResult = jedis.hget("customer:103:address", "phone");

            System.out.println("Command: hget customer:103:address phone | Result: " + hgetResult);

            /**
             * Get multiple fields of hash
             *
             * Command: hmget customer:103:address zip phone country
             * Result:
             *      1) "94599"
             *      2) "(503)-445-4454"
             *      3) "United States"
             */
            List<String> hmgetResult = jedis.hmget("customer:103:address", "zip", "phone", "country");

            System.out.println("Command: hmget customer:103:address zip phone country | Result: " + hmgetResult.toString());

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

            System.out.println("Command: set bigboxstr "some string value here" | Result: " + setResult);

            /**
             * Try to apply HSET on the string data type
             * We get an error
             *
             * Command: hset bigboxstr testfield "test val"
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try {
                hsetResult = jedis.hset("bigboxstr", "testfield", "test val");

                System.out.println("Command: hset bigboxstr testfield "test val" | Result: " + hsetResult);
            } catch (Exception e) {
                System.out.println("Command: hset bigboxstr testfield "test val" | Error: " + e.getMessage());
            }

        }

        jedisPool.close();
    }
}

Output:

Command: hset customer:103:address street "965 Lakeville St" | Result: 1
Command: hgetall customer:103:address | Result: {street=965 Lakeville St}

Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: 4
Command: hgetall customer:103:address | Result: {zip=94952, country=United States, state=California, city=Petaluma, street=965 Lakeville St}

Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result: 1
Command: hgetall customer:103:address | Result: {zip=94566, country=United States, state=California, city=hayward, phone=(503)-445-4454, street=965 Lakeville St}

Command: hset customer:103:address zip 94555 zip 94599 | Result: 0
Command: hgetall customer:103:address | Result: {zip=94599, country=United States, state=California, city=hayward, phone=(503)-445-4454, street=965 Lakeville St}

Command: hget customer:103:address phone | Result: (503)-445-4454

Command: hmget customer:103:address zip phone country | Result: [94599, (503)-445-4454, United States]

Command: set bigboxstr "some string value here" | Result: OK
Command: hset bigboxstr testfield "test val" | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

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

using StackExchange.Redis;

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

            /**
             * Set "street" field of hash
             *
             * Command: hset customer:103:address street "965 Lakeville St"
             * Result: (integer) 1
             */
            bool hsetResult = rdb.HashSet("customer:103:address", "street", "965 Lakeville St");

            Console.WriteLine("Command: hset customer:103:address street "965 Lakeville St" | Result: " + hsetResult);

            /**
             * Check hash
             *
             * Command: hgetall customer:103:address
             * Result:
             *      1) "street"
             *      2) "965 Lakeville St"
             */
            HashEntry[] hgetAllResult = rdb.HashGetAll("customer:103:address");
            Console.WriteLine("Command: hgetall customer:103:address | Result: " + string.Join(", ", hgetAllResult));

            /**
             * Set multiple fields of the hash
             *
             * Command: hset customer:103:address city Petaluma state California zip 94952 country "United States"
             * Result: (integer) 4
             */
            HashEntry[] hashData = new HashEntry[] {
                new HashEntry("city", "Petaluma"),
                new HashEntry("state", "California"),
                new HashEntry("zip", "94952"),
                new HashEntry("country", "United States")
            };
            rdb.HashSet("customer:103:address", hashData);

            Console.WriteLine("Command: hset customer:103:address city Petaluma state California zip 94952 country "United States"");

            /**
             * Check hash
             *
             * Command: hgetall customer:103:address
             * Result:
             *      1) "street"     2) "965 Lakeville St"
             *      3) "city"       4) "Petaluma"
             *      5) "state"      6) "California"
             *      7) "zip"        8) "94952"
             *      9) "country"    10) "United States"
             */
            hgetAllResult = rdb.HashGetAll("customer:103:address");

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

            /**
             * Set new fields to hash, also update some existing fields
             *
             * Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454
             * Result: (integer) 1
             */
            hashData = new HashEntry[] {
                new HashEntry("city", "hayward"),
                new HashEntry("zip", "94566"),
                new HashEntry("phone", "(503)-445-4454"),
            };

            rdb.HashSet("customer:103:address", hashData);

            Console.WriteLine("Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454");

            /**
             * Check hash
             *
             * Command: hgetall customer:103:address
             * Result:
             *      1) "street"     2) "965 Lakeville St"
             *      3) "city"       4) "hayward"
             *      5) "state"      6) "California"
             *      7) "zip"        8) "94566"
             *      9) "country"    10) "United States"
             *      11) "phone"     12) "(503)-445-4454"
             */
            hgetAllResult = rdb.HashGetAll("customer:103:address");

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

            /**
             * Try to set the same field multiple times
             * The later provided value is saved
             *
             * Command: hset customer:103:address zip 94555 zip 94599
             * Result: (integer) 0
             */
            hashData = new HashEntry[] {
                new HashEntry("zip", "94555"),
                new HashEntry("zip", "94599"),
            };
            rdb.HashSet("customer:103:address", hashData);

            Console.WriteLine("Command: hset customer:103:address zip 94555 zip 94599");

            /**
             * Check set value
             *
             * Command: hgetall customer:103:address
             * Result:
             *      1) "street"     2) "965 Lakeville St"
             *      3) "city"       4) "hayward"
             *      5) "state"      6) "California"
             *      7) "zip"        8) "94599"
             *      9) "country"    10) "United States"
             *      11) "phone"     12) "(503)-445-4454"
             */
            hgetAllResult = rdb.HashGetAll("customer:103:address");

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

            /**
             * Get single field of hash
             *
             * Command: hget customer:103:address phone
             * Result: "(503)-445-4454"
             */
            RedisValue hgetResult = rdb.HashGet("customer:103:address", "phone");

            Console.WriteLine("Command: hget customer:103:address phone | Result: " + hgetResult);

            /**
             * Get multiple fields of hash
             *
             * Command: hmget customer:103:address zip phone country
             * Result:
             *      1) "94599"
             *      2) "(503)-445-4454"
             *      3) "United States"
             */
            RedisValue[] hmgetResult = rdb.HashGet("customer:103:address", new RedisValue[] { "zip", "phone", "country" });

            Console.WriteLine("Command: hmget customer:103:address zip phone country | Result: " + string.Join(", ", hmgetResult));

            /**
             * Set a string key
             *
             * Command: set bigboxstr "some string value here"
             * Result: OK
             */
            bool setResult = rdb.StringSet("bigboxstr", "some string value here");

            Console.WriteLine("Command: set bigboxstr "some string value here" | Result: " + setResult);

            /**
             * Try to apply HSET on the string data type
             * We get an error
             *
             * Command: hset bigboxstr testfield "test val"
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try
            {
                hsetResult = rdb.HashSet("bigboxstr", "testfield", "test val");

                Console.WriteLine("Command: hset bigboxstr testfield "test val" | Result: " + hsetResult);
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: hset bigboxstr testfield "test val" | Error: " + e.Message);
            }
        }
    }
}

Output:

Command: hset customer:103:address street "965 Lakeville St" | Result: True
Command: hgetall customer:103:address | Result: street: 965 Lakeville St

Command: hset customer:103:address city Petaluma state California zip 94952 country "United States"
Command: hgetall customer:103:address | Result: street: 965 Lakeville St, city: Petaluma, state: California, zip: 94952, country: United States

Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454
Command: hgetall customer:103:address | Result: street: 965 Lakeville St, city: hayward, state: California, zip: 94566, country: United States, phone: (503)-445-4454

Command: hset customer:103:address zip 94555 zip 94599
Command: hgetall customer:103:address | Result: street: 965 Lakeville St, city: hayward, state: California, zip: 94599, country: United States, phone: (503)-445-4454

Command: hget customer:103:address phone | Result: (503)-445-4454

Command: hmget customer:103:address zip phone country | Result: 94599, (503)-445-4454, United States

Command: set bigboxstr "some string value here" | Result: True
Command: hset bigboxstr testfield "test val" | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use the method “HashSet” from StackExchange.Redis.
  • Signatures of the method are-
    bool HashSet(RedisKey key, RedisValue hashField, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None)

    void HashSet(RedisKey key, HashEntry[] hashFields, CommandFlags flags = CommandFlags.None)
<?php
// Redis HSET command example in PHP

require 'vendor/autoload.php';

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


/**
 * Set "street" field of hash
 *
 * Command: hset customer:103:address street "965 Lakeville St"
 * Result: (integer) 1
 */
$commandResult = $redisClient->hset(
    "customer:103:address",
    "street",
    "965 Lakeville St"
);

echo "Command: hset customer:103:address street "965 Lakeville St" | Result: " . $commandResult . "n";

/**
 * Check hash
 *
 * Command: hgetall customer:103:address
 * Result:
 *      1) "street"
 *      2) "965 Lakeville St"
 */
$commandResult = $redisClient->hgetall("customer:103:address");

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

/**
 * Set multiple fields of the hash
 *
 * Command: hset customer:103:address city Petaluma state California zip 94952 country "United States"
 * Result: (integer) 4
 */
$commandResult = $redisClient->hset("customer:103:address",
    "city", "Petaluma",
    "state", "California",
    "zip", "94952",
    "country", "United States",
);

echo "Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: " . $commandResult . "n";

/**
 * Check hash
 *
 * Command: hgetall customer:103:address
 * Result:
 *      1) "street"     2) "965 Lakeville St"
 *      3) "city"       4) "Petaluma"
 *      5) "state"      6) "California"
 *      7) "zip"        8) "94952"
 *      9) "country"    10) "United States"
 */
$commandResult = $redisClient->hgetall("customer:103:address");

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

/**
 * Set new fields to hash, also update some existing fields
 *
 * Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454
 * Result: (integer) 1
 */
$commandResult = $redisClient->hset("customer:103:address",
    "city", "hayward",
    "zip", "94566",
    "phone", "(503)-445-4454",
);

echo "Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result: " . $commandResult . "n";

/**
 * Check hash
 *
 * Command: hgetall customer:103:address
 * Result:
 *      1) "street"     2) "965 Lakeville St"
 *      3) "city"       4) "hayward"
 *      5) "state"      6) "California"
 *      7) "zip"        8) "94566"
 *      9) "country"    10) "United States"
 *      11) "phone"     12) "(503)-445-4454"
 */
$commandResult = $redisClient->hgetall("customer:103:address");

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

/**
 * Try to set the same field multiple times
 * The later provided value is saved
 *
 * Command: hset customer:103:address zip 94555 zip 94599
 * Result: (integer) 0
 */
$commandResult = $redisClient->hset("customer:103:address",
    "zip", "94555",
    "zip", "94599",
);

echo "Command: hset customer:103:address zip 94555 zip 94599 | Result: " . $commandResult . "n";

/**
 * Check set value
 *
 * Command: hgetall customer:103:address
 * Result:
 *      1) "street"     2) "965 Lakeville St"
 *      3) "city"       4) "hayward"
 *      5) "state"      6) "California"
 *      7) "zip"        8) "94599"
 *      9) "country"    10) "United States"
 *      11) "phone"     12) "(503)-445-4454"
 */
$commandResult = $redisClient->hgetall("customer:103:address");

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

/**
 * Get single field of hash
 *
 * Command: hget customer:103:address phone
 * Result: "(503)-445-4454"
 */
$commandResult = $redisClient->hget("customer:103:address", "phone");

echo "Command: hget customer:103:address phone | Result: " . $commandResult . "n";

/**
 * Get multiple fields of hash
 *
 * Command: hmget customer:103:address zip phone country
 * Result:
 *      1) "94599"
 *      2) "(503)-445-4454"
 *      3) "United States"
 */
$commandResult = $redisClient->hmget("customer:103:address", [
    "zip",
    "phone",
    "country"
]);

echo "Command: hmget customer:103:address zip phone country | Result: ";
print_r($commandResult);

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

echo "Command: set bigboxstr "some string value here" | Result: " . $commandResult . "n";

/**
 * Try to apply HSET on the string data type
 * We get an error
 *
 * Command: hset bigboxstr testfield "test val"
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
    $commandResult = $redisClient->hset("bigboxstr", "testfield", "test val");

    echo "Command: hset bigboxstr testfield "test val" | Result: " . $commandResult . "n";
} catch (Exception $e) {
    echo "Command: hset bigboxstr testfield "test val" | Error: " . $e->getMessage() . "n";
}

Output:

Command: hset customer:103:address street "965 Lakeville St" | Result: 1
Command: hgetall customer:103:address | Result: Array
(
    [street] => 965 Lakeville St
)

Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: 4
Command: hgetall customer:103:address | Result: Array
(
    [street] => 965 Lakeville St
    [city] => Petaluma
    [state] => California
    [zip] => 94952
    [country] => United States
)

Command: hgetall customer:103:address | Result: Array
(
    [street] => 965 Lakeville St
    [city] => hayward
    [state] => California
    [zip] => 94566
    [country] => United States
    [phone] => (503)-445-4454
)

Command: hset customer:103:address zip 94555 zip 94599 | Result: 0
Command: hgetall customer:103:address | Result: Array
(
    [street] => 965 Lakeville St
    [city] => hayward
    [state] => California
    [zip] => 94599
    [country] => United States
    [phone] => (503)-445-4454
)

Command: hget customer:103:address phone | Result: (503)-445-4454
Command: hmget customer:103:address zip phone country | Result: Array
(
    [0] => 94599
    [1] => (503)-445-4454
    [2] => United States
)

Command: set bigboxstr "some string value here" | Result: OK
Command: hset bigboxstr testfield "test val" | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use the method “hset” of predis.
  • Signature of the method is-
    hset(string $key, string $field, string $value): int
# Redis HSET 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 "street" field of hash
# Command: hset customer:103:address street "965 Lakeville St"
# Result: (integer) 1
commandResult = redisClient.hset(
    "customer:103:address",
    "street",
    "965 Lakeville St"
)

print("Command: hset customer:103:address street "965 Lakeville St" | Result: {}".format(commandResult))

# Check hash
# Command: hgetall customer:103:address
# Result:
#      1) "street"
#      2) "965 Lakeville St"
commandResult = redisClient.hgetall("customer:103:address")

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

# Set multiple fields of the hash
# Command: hset customer:103:address city Petaluma state California zip 94952 country "United States"
# Result: (integer) 4
commandResult = redisClient.hset("customer:103:address", mapping = {
    "city": "Petaluma",
    "state": "California",
    "zip": "94952",
    "country": "United States",
})

print("Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: {}".format(commandResult))

# Check hash
# Command: hgetall customer:103:address
# Result:
#      1) "street"     2) "965 Lakeville St"
#      3) "city"       4) "Petaluma"
#      5) "state"      6) "California"
#      7) "zip"        8) "94952"
#      9) "country"    10) "United States"
commandResult = redisClient.hgetall("customer:103:address")

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

# Set new fields to hash, also update some existing fields
# Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454
# Result: (integer) 1
commandResult = redisClient.hset("customer:103:address", mapping= {
    "city": "hayward",
    "zip": "94566",
    "phone": "(503)-445-4454",
})

print("Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result: {}".format(commandResult))

# Check hash
# Command: hgetall customer:103:address
# Result:
#      1) "street"     2) "965 Lakeville St"
#      3) "city"       4) "hayward"
#      5) "state"      6) "California"
#      7) "zip"        8) "94566"
#      9) "country"    10) "United States"
#      11) "phone"     12) "(503)-445-4454"
commandResult = redisClient.hgetall("customer:103:address")

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

# Try to set the same field multiple times
# The later provided value is saved
# Command: hset customer:103:address zip 94555 zip 94599
# Result: (integer) 0
commandResult = redisClient.hset("customer:103:address", items=[
    "zip", "94555",
    "zip", "94599",
])

print("Command: hset customer:103:address zip 94555 zip 94599 | Result: {}".format(commandResult))

# Check set value
# Command: hgetall customer:103:address
# Result:
#      1) "street"     2) "965 Lakeville St"
#      3) "city"       4) "hayward"
#      5) "state"      6) "California"
#      7) "zip"        8) "94599"
#      9) "country"    10) "United States"
#      11) "phone"     12) "(503)-445-4454"
commandResult = redisClient.hgetall("customer:103:address")

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

# Get single field of hash
# Command: hget customer:103:address phone
# Result: "(503)-445-4454"
commandResult = redisClient.hget("customer:103:address", "phone")

print("Command: hget customer:103:address phone | Result: {}".format(commandResult))

# Get multiple fields of hash
# Command: hmget customer:103:address zip phone country
# Result:
#      1) "94599"
#      2) "(503)-445-4454"
#      3) "United States"
commandResult = redisClient.hmget("customer:103:address",
    "zip",
    "phone",
    "country"
)

print("Command: hmget customer:103:address zip phone country | Result: {}".format(commandResult))

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

print("Command: set bigboxstr "some string value here" | Result: {}".format(commandResult))

# Try to apply HSET on the string data type
# We get an error
# Command: hset bigboxstr testfield "test val"
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
    commandResult = redisClient.hset("bigboxstr", "testfield", "test val")

    print("Command: hset bigboxstr testfield "test val" | Result: {}".format(commandResult))
except Exception as error:
    print("Command: hset bigboxstr testfield "test val" | Error: ", error , "n")

Output:

Command: hset customer:103:address street "965 Lakeville St" | Result: 1
Command: hgetall customer:103:address | Result: {'street': '965 Lakeville St'}

Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: 4
Command: hgetall customer:103:address | Result: {'street': '965 Lakeville St', 'city': 'Petaluma', 'state': 'California', 'zip': '94952', 'country': 'United States'}

Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result: 1
Command: hgetall customer:103:address | Result: {'street': '965 Lakeville St', 'city': 'hayward', 'state': 'California', 'zip': '94566', 'country': 'United States', 'phone': '(503)-445-4454'}

Command: hset customer:103:address zip 94555 zip 94599 | Result: 0
Command: hgetall customer:103:address | Result: {'street': '965 Lakeville St', 'city': 'hayward', 'state': 'California', 'zip': '94599', 'country': 'United States', 'phone': '(503)-445-4454'}

Command: hget customer:103:address phone | Result: (503)-445-4454
Command: hmget customer:103:address zip phone country | Result: ['94599', '(503)-445-4454', 'United States']

Command: set bigboxstr "some string value here" | Result: True
Command: hset bigboxstr testfield "test val" | Error:  WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use method “hset” from redis-py.
  • Signature of the method is –
    def hset(self, name: str, key: Optional[str] = None, value: Optional[str] = None, mapping: Optional[dict] = None, items: Optional[list] = None,) -> Union[Awaitable[int], int]
# Redis HSET command example in Ruby

require 'redis'

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


# Set "street" field of hash
# Command: hset customer:103:address street "965 Lakeville St"
# Result: (integer) 1
commandResult = redis.hset(
    "customer:103:address",
    "street",
    "965 Lakeville St"
)

print("Command: hset customer:103:address street "965 Lakeville St" | Result: ", commandResult, "n")

# Check hash
# Command: hgetall customer:103:address
# Result:
#      1) "street"
#      2) "965 Lakeville St"
commandResult = redis.hgetall("customer:103:address")

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

# Set multiple fields of the hash
# Command: hset customer:103:address city Petaluma state California zip 94952 country "United States"
# Result: (integer) 4
commandResult = redis.hset("customer:103:address", {
    "city" => "Petaluma",
    "state" => "California",
    "zip" => "94952",
    "country" => "United States",
})

print("Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: ", commandResult, "n")

# Check hash
# Command: hgetall customer:103:address
# Result:
#      1) "street"     2) "965 Lakeville St"
#      3) "city"       4) "Petaluma"
#      5) "state"      6) "California"
#      7) "zip"        8) "94952"
#      9) "country"    10) "United States"
commandResult = redis.hgetall("customer:103:address")

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

# Set new fields to hash, also update some existing fields
# Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454
# Result: (integer) 1
commandResult = redis.hset("customer:103:address", {
    "city" => "hayward",
    "zip" => "94566",
    "phone" => "(503)-445-4454",
})

print("Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result: ", commandResult, "n")

# Check hash
# Command: hgetall customer:103:address
# Result:
#      1) "street"     2) "965 Lakeville St"
#      3) "city"       4) "hayward"
#      5) "state"      6) "California"
#      7) "zip"        8) "94566"
#      9) "country"    10) "United States"
#      11) "phone"     12) "(503)-445-4454"
commandResult = redis.hgetall("customer:103:address")

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

# Try to set the same field multiple times
# The later provided value is saved
# Command: hset customer:103:address zip 94555 zip 94599
# Result: (integer) 0
commandResult = redis.hset("customer:103:address", "zip", "94555", "zip", "94599")

print("Command: hset customer:103:address zip 94555 zip 94599 | Result: ", commandResult, "n")

# Check set value
# Command: hgetall customer:103:address
# Result:
#      1) "street"     2) "965 Lakeville St"
#      3) "city"       4) "hayward"
#      5) "state"      6) "California"
#      7) "zip"        8) "94599"
#      9) "country"    10) "United States"
#      11) "phone"     12) "(503)-445-4454"
commandResult = redis.hgetall("customer:103:address")

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

# Get single field of hash
# Command: hget customer:103:address phone
# Result: "(503)-445-4454"
commandResult = redis.hget("customer:103:address", "phone")

print("Command: hget customer:103:address phone | Result: ", commandResult, "n")

# Get multiple fields of hash
# Command: hmget customer:103:address zip phone country
# Result:
#      1) "94599"
#      2) "(503)-445-4454"
#      3) "United States"
commandResult = redis.hmget("customer:103:address",
    "zip",
    "phone",
    "country"
)

print("Command: hmget customer:103:address zip phone country | Result: ", commandResult, "n")

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

print("Command: set bigboxstr "some string value here" | Result: ", commandResult, "n")

# Try to apply HSET on the string data type
# We get an error
# Command: hset bigboxstr testfield "test val"
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
    commandResult = redis.hset("bigboxstr", "testfield", "test val")

    print("Command: hset bigboxstr testfield "test val" | Result: ", commandResult, "n")
rescue => e
    print("Command: hset bigboxstr testfield "test val" | Error: ", e, "n")
end

Output:

Command: hset customer:103:address street "965 Lakeville St" | Result: 1
Command: hgetall customer:103:address | Result: {"street"=>"965 Lakeville St"}

Command: hset customer:103:address city Petaluma state California zip 94952 country "United States" | Result: 4
Command: hgetall customer:103:address | Result: {"street"=>"965 Lakeville St", "city"=>"Petaluma", "state"=>"California", "zip"=>"94952", "country"=>"United States"}

Command: hset customer:103:address city hayward  zip 94566 phone (503)-445-4454 | Result: 1
Command: hgetall customer:103:address | Result: {"street"=>"965 Lakeville St", "city"=>"hayward", "state"=>"California", "zip"=>"94566", "country"=>"United States", "phone"=>"(503)-445-4454"}

Command: hset customer:103:address zip 94555 zip 94599 | Result: 0
Command: hgetall customer:103:address | Result: {"street"=>"965 Lakeville St", "city"=>"hayward", "state"=>"California", "zip"=>"94599", "country"=>"United States", "phone"=>"(503)-445-4454"}

Command: hget customer:103:address phone | Result: (503)-445-4454

Command: hmget customer:103:address zip phone country | Result: ["94599", "(503)-445-4454", "United States"]

Command: set bigboxstr "some string value here" | Result: OK
Command: hset bigboxstr testfield "test val" | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Notes

  • Use method “hset” from the redis-rb.
  • Signature of the method is-
    # @param [String] key
    # @param [Array<String> | Hash<String, String>] attrs array or hash of fields and values
    # @return [Integer] The number of fields that were added to the hash

    def hset(key, *attrs)


    So we can use the method in the following ways-
    redis.hset("hash", "field1", "value1", "field2", "value2")
    redis.hset("hash", { "field1" => "value1", "field2" => "value2" })

Source Code

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

Related Commands

CommandDetails
HSETNX Command Details
HGET Command Details
HMGET Command Details
HGETALL Command Details

Leave a Comment


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