Summary
Command Name | HGET |
Usage | Get single hash field value |
Group | hash |
ACL Category | @read @hash @fast |
Time Complexity | O(1) |
Flag | READONLY FAST |
Arity | 3 |
Signature
HGET <key> <field>
Usage
Get the value of a single field of a hash. This is the simplest command for getting the value from a hash, but only one field can be fetched from the hash.
Notes
- HMGET supports multiple fields, and HGETALL returns all data of a hash. So you can use these if these commands serve your purpose.
Arguments
Parameter | Description | Name | Type |
---|---|---|---|
<key> | Name of the key of the hash | key | key |
<field> | Name of the field | field | string |
Return Value
Return value | Case for the return value | Type |
---|---|---|
Field value | On successful we get the value saved for the field | string |
(nil) | If the key(for the hash) does not exist, or the field is not present in the hash | null |
error | If applied to wrong data type | error |
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 HGET 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 HGET 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:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States"
* Result: (integer) 5
*/
hsetResult, err := rdb.HSet(ctx, "customer:99:address",
"street", "2855 W 76 Country Blvd",
"city", "Branson",
"state", "Mississippi",
"zip", "65616",
"country", "United States",
).Result()
if err != nil {
fmt.Println("Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Error: " + err.Error())
}
fmt.Println("Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: ", hsetResult)
/**
* Check zip field of the hash
*
* Command: hget customer:99:address zip
* Result: "65616"
*/
hgetResult, err := rdb.HGet(ctx, "customer:99:address", "zip").Result()
if err != nil {
fmt.Println("Command: hget customer:99:address zip | Error: " + err.Error())
}
fmt.Println("Command: hget customer:99:address zip | Result: " + hgetResult)
/**
* Check state field of the hash
*
* Command: hget customer:99:address state
* Result: "Mississippi"
*/
hgetResult, err = rdb.HGet(ctx, "customer:99:address", "state").Result()
if err != nil {
fmt.Println("Command: hget customer:99:address state | Error: " + err.Error())
}
fmt.Println("Command: hget customer:99:address state | Result: " + hgetResult)
/**
* Try to get value of a field that does not exist
* We get (nil)
*
* Command: hget customer:99:address nonexistingfield
* Result: (nil)
*/
hgetResult, err = rdb.HGet(ctx, "customer:99:address", "nonexistingfield").Result()
if err != nil {
fmt.Println("Command: hget customer:99:address nonexistingfield | Error: " + err.Error())
}
fmt.Println("Command: hget customer:99:address nonexistingfield | Result: " + hgetResult)
/**
* Try to get field value from a non existing hash
* We get (nil)
*
* Command: hget nonexistinghash somefield
* Result: (nil)
*/
hgetResult, err = rdb.HGet(ctx, "nonexistinghash", "somefield").Result()
if err != nil {
fmt.Println("Command: hget nonexistinghash somefield | Error: " + err.Error())
}
fmt.Println("Command: hget nonexistinghash somefield | Result: " + hgetResult)
/**
* 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 HGET on a string type of key
* We get an error
*
* Command: hget bigboxstr somefield
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
hgetResult, err = rdb.HGet(ctx, "bigboxstr", "somefield").Result()
if err != nil {
fmt.Println("Command: hget bigboxstr somefield | Error: " + err.Error())
}
fmt.Println("Command: hget bigboxstr somefield | Result: " + hgetResult)
}
Output:
Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: 5
Command: hget customer:99:address zip | Result: 65616
Command: hget customer:99:address state | Result: Mississippi
Command: hget customer:99:address nonexistingfield | Error: redis: nil
Command: hget customer:99:address nonexistingfield | Result:
Command: hget nonexistinghash somefield | Error: redis: nil
Command: hget nonexistinghash somefield | Result:
Command: set bigboxstr "some string in the box" | Result: OK
Command: hget bigboxstr somefield | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hget bigboxstr somefield | Result:
Notes
- Use “
HGet
” method from redis-go module. - Signature of the method is-
HSet(ctx context.Context, key string, values ...interface{}) *IntCmd
// Redis HGET 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:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States"
* Result: (integer) 5
*/
let commandResult = await redisClient.hSet("customer:99:address", {
street: "2855 W 76 Country Blvd",
city: "Branson",
state: "Mississippi",
zip: "65616",
country: "United States",
});
console.log(
'Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: ' +
commandResult
);
/**
* Check zip field of the hash
*
* Command: hget customer:99:address zip
* Result: "65616"
*/
commandResult = await redisClient.hGet("customer:99:address", "zip");
console.log("Command: hget customer:99:address zip | Result: " + commandResult);
/**
* Check state field of the hash
*
* Command: hget customer:99:address state
* Result: "Mississippi"
*/
commandResult = await redisClient.hGet("customer:99:address", "state");
console.log(
"Command: hget customer:99:address state | Result: " + commandResult
);
/**
* Try to get value of a field that does not exist
* We get (nil)
*
* Command: hget customer:99:address nonexistingfield
* Result: (nil)
*/
commandResult = await redisClient.hGet(
"customer:99:address",
"nonexistingfield"
);
console.log(
"Command: hget customer:99:address nonexistingfield | Result: " +
commandResult
);
/**
* Try to get field value from a non existing hash
* We get (nil)
*
* Command: hget nonexistinghash somefield
* Result: (nil)
*/
commandResult = await redisClient.hGet("nonexistinghash", "somefield");
console.log(
"Command: hget nonexistinghash somefield | 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 HGET on a string type of key
* We get an error
*
* Command: hget bigboxstr somefield
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.hGet("bigboxstr", "somefield");
console.log("Command: hget bigboxstr somefield | Result: " + commandResult);
} catch (err) {
console.log("Command: hget bigboxstr somefield | Error: ", err);
}
process.exit(0);
Output:
Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: 5
Command: hget customer:99:address zip | Result: 65616
Command: hget customer:99:address state | Result: Mississippi
Command: hget customer:99:address nonexistingfield | Result: null
Command: hget nonexistinghash somefield | Result: null
Command: set bigboxstr "some string in the box" | Result: OK
Command: hget bigboxstr somefield | Error: [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
Notes
- Use the function “
hGet
” of node-redis. - Signature of the method-
function hGet(key: RedisCommandArgument, field: RedisCommandArgument)
// Redis HGET Command example in Java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.HashMap;
import java.util.Map;
public class Hget {
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:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States"
* Result: (integer) 5
*/
Map<String, String> hashData = new HashMap<>() {{
put("street", "2855 W 76 Country Blvd");
put("city", "Branson");
put("state", "Mississippi");
put("zip", "65616");
put("country", "United States");
}};
long hsetResult = jedis.hset("customer:99:address", hashData);
System.out.println("Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: " + hsetResult);
/**
* Check zip field of the hash
*
* Command: hget customer:99:address zip
* Result: "65616"
*/
String hgetResult = jedis.hget("customer:99:address", "zip");
System.out.println("Command: hget customer:99:address zip | Result: " + hgetResult);
/**
* Check state field of the hash
*
* Command: hget customer:99:address state
* Result: "Mississippi"
*/
hgetResult = jedis.hget("customer:99:address", "state");
System.out.println("Command: hget customer:99:address state | Result: " + hgetResult);
/**
* Try to get value of a field that does not exist
* We get (nil)
*
* Command: hget customer:99:address nonexistingfield
* Result: (nil)
*/
hgetResult = jedis.hget("customer:99:address", "nonexistingfield");
System.out.println("Command: hget customer:99:address nonexistingfield | Result: " + hgetResult);
/**
* Try to get field value from a non existing hash
* We get (nil)
*
* Command: hget nonexistinghash somefield
* Result: (nil)
*/
hgetResult = jedis.hget("nonexistinghash", "somefield");
System.out.println("Command: hget nonexistinghash somefield | Result: " + hgetResult);
/**
* 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 HGET on a string type of key
* We get an error
*
* Command: hget bigboxstr somefield
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
hgetResult = jedis.hget("bigboxstr", "somefield");
System.out.println("Command: hget bigboxstr somefield | Result: " + hgetResult);
} catch (Exception e) {
System.out.println("Command: hget bigboxstr somefield | Error: " + e.getMessage());
}
}
jedisPool.close();
}
}
Output:
Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: 5
Command: hget customer:99:address zip | Result: 65616
Command: hget customer:99:address state | Result: Mississippi
Command: hget customer:99:address nonexistingfield | Result: null
Command: hget nonexistinghash somefield | Result: null
Command: set bigboxstr "some string in the box" | Result: OK
Command: hget bigboxstr somefield | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “
hget
” from Jedis package. - The signature of the method is-
public String hget(final String key, final String field)
// Redis HGET command examples in C#
using StackExchange.Redis;
namespace Hget
{
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:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States"
* Result: (integer) 5
*/
HashEntry[] hashData = new HashEntry[] {
new HashEntry("street", "2855 W 76 Country Blvd"),
new HashEntry("city", "Branson"),
new HashEntry("state", "Mississippi"),
new HashEntry("zip", "65616"),
new HashEntry("country", "United States")
};
rdb.HashSet("customer:99:address", hashData);
Console.WriteLine("Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States"");
/**
* Check zip field of the hash
*
* Command: hget customer:99:address zip
* Result: "65616"
*/
RedisValue hgetResult = rdb.HashGet("customer:99:address", "zip");
Console.WriteLine("Command: hget customer:99:address zip | Result: " + hgetResult);
/**
* Check state field of the hash
*
* Command: hget customer:99:address state
* Result: "Mississippi"
*/
hgetResult = rdb.HashGet("customer:99:address", "state");
Console.WriteLine("Command: hget customer:99:address state | Result: " + hgetResult);
/**
* Try to get value of a field that does not exist
* We get (nil)
*
* Command: hget customer:99:address nonexistingfield
* Result: (nil)
*/
hgetResult = rdb.HashGet("customer:99:address", "nonexistingfield");
Console.WriteLine("Command: hget customer:99:address nonexistingfield | Result: " + hgetResult);
/**
* Try to get field value from a non existing hash
* We get (nil)
*
* Command: hget nonexistinghash somefield
* Result: (nil)
*/
hgetResult = rdb.HashGet("nonexistinghash", "somefield");
Console.WriteLine("Command: hget nonexistinghash somefield | Result: " + hgetResult);
/**
* 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 HGET on a string type of key
* We get an error
*
* Command: hget bigboxstr somefield
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
hgetResult = rdb.HashGet("bigboxstr", "somefield");
Console.WriteLine("Command: hget bigboxstr somefield | Result: " + hgetResult);
}
catch (Exception e)
{
Console.WriteLine("Command: hget bigboxstr somefield | Error: " + e.Message);
}
}
}
}
Output:
Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States"
Command: hget customer:99:address zip | Result: 65616
Command: hget customer:99:address state | Result: Mississippi
Command: hget customer:99:address nonexistingfield | Result:
Command: hget nonexistinghash somefield | Result:
Command: set bigboxstr "some string in the box" | Result: True
Command: hget bigboxstr somefield | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “
HashGet
” from StackExchange.Redis. - Signatures of the method are-
RedisValue HashGet(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None)
<?php
// Redis HGET command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Set some has fields usign HSET
*
* Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States"
* Result: (integer) 5
*/
$commandResult = $redisClient->hset("customer:99:address",
"street", "2855 W 76 Country Blvd",
"city", "Branson",
"state", "Mississippi",
"zip", "65616",
"country", "United States",
);
echo "Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: " . $commandResult . "n";
/**
* Check zip field of the hash
*
* Command: hget customer:99:address zip
* Result: "65616"
*/
$commandResult = $redisClient->hget("customer:99:address", "zip");
echo "Command: hget customer:99:address zip | Result: " . $commandResult . "n";
/**
* Check state field of the hash
*
* Command: hget customer:99:address state
* Result: "Mississippi"
*/
$commandResult = $redisClient->hget("customer:99:address", "state");
echo "Command: hget customer:99:address state | Result: " . $commandResult . "n";
/**
* Try to get value of a field that does not exist
* We get (nil)
*
* Command: hget customer:99:address nonexistingfield
* Result: (nil)
*/
$commandResult = $redisClient->hget("customer:99:address", "nonexistingfield");
echo "Command: hget customer:99:address nonexistingfield | Result: " . $commandResult . "n";
/**
* Try to get field value from a non existing hash
* We get (nil)
*
* Command: hget nonexistinghash somefield
* Result: (nil)
*/
$commandResult = $redisClient->hget("nonexistinghash", "somefield");
echo "Command: hget nonexistinghash somefield | Result: " . $commandResult . "n";
/**
* 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 HGET on a string type of key
* We get an error
*
* Command: hget bigboxstr somefield
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
$commandResult = $redisClient->hget("bigboxstr", "somefield");
echo "Command: hget bigboxstr somefield | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: hget bigboxstr somefield | Error: " . $e->getMessage() . "n";
}
Output:
Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: 5
Command: hget customer:99:address zip | Result: 65616
Command: hget customer:99:address state | Result: Mississippi
Command: hget customer:99:address nonexistingfield | Result:
Command: hget nonexistinghash somefield | Result:
Command: set bigboxstr "some string in the box" | Result: OK
Command: hget bigboxstr somefield | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “
hget
” of predis. - Signature of the method is-
hget(string $key, string $field): string|null
# Redis HGET 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:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States"
# Result: (integer) 5
commandResult = redisClient.hset("customer:99:address", mapping= {
"street": "2855 W 76 Country Blvd",
"city": "Branson",
"state": "Mississippi",
"zip": "65616",
"country": "United States",
})
print("Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: {}".format(commandResult))
# Check zip field of the hash
# Command: hget customer:99:address zip
# Result: "65616"
commandResult = redisClient.hget("customer:99:address", "zip")
print("Command: hget customer:99:address zip | Result: {}".format(commandResult))
# Check state field of the hash
# Command: hget customer:99:address state
# Result: "Mississippi"
commandResult = redisClient.hget("customer:99:address", "state")
print("Command: hget customer:99:address state | Result: {}".format(commandResult))
# Try to get value of a field that does not exist
# We get (nil)
# Command: hget customer:99:address nonexistingfield
# Result: (nil)
commandResult = redisClient.hget("customer:99:address", "nonexistingfield")
print("Command: hget customer:99:address nonexistingfield | Result: {}".format(commandResult))
# Try to get field value from a non existing hash
# We get (nil)
# Command: hget nonexistinghash somefield
# Result: (nil)
commandResult = redisClient.hget("nonexistinghash", "somefield")
print("Command: hget nonexistinghash somefield | 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 HGET on a string type of key
# We get an error
# Command: hget bigboxstr somefield
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.hget("bigboxstr", "somefield")
print("Command: hget bigboxstr somefield | Result: {}".format(commandResult))
except Exception as error:
print("Command: hget bigboxstr somefield | Error: ", error , "n")
Output:
Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: 5
Command: hget customer:99:address zip | Result: 65616
Command: hget customer:99:address state | Result: Mississippi
Command: hget customer:99:address nonexistingfield | Result: None
Command: hget nonexistinghash somefield | Result: None
Command: set bigboxstr "some string in the box" | Result: True
Command: hget bigboxstr somefield | 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 HGET command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Set some has fields usign HSET
# Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States"
# Result: (integer) 5
commandResult = redis.hset("customer:99:address", {
"street" => "2855 W 76 Country Blvd",
"city" => "Branson",
"state" => "Mississippi",
"zip" => "65616",
"country" => "United States",
})
print("Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: ", commandResult, "n")
# Check zip field of the hash
# Command: hget customer:99:address zip
# Result: "65616"
commandResult = redis.hget("customer:99:address", "zip")
print("Command: hget customer:99:address zip | Result: ", commandResult, "n")
# Check state field of the hash
# Command: hget customer:99:address state
# Result: "Mississippi"
commandResult = redis.hget("customer:99:address", "state")
print("Command: hget customer:99:address state | Result: ", commandResult, "n")
# Try to get value of a field that does not exist
# We get (nil)
# Command: hget customer:99:address nonexistingfield
# Result: (nil)
commandResult = redis.hget("customer:99:address", "nonexistingfield")
print("Command: hget customer:99:address nonexistingfield | Result: ", commandResult, "n")
# Try to get field value from a non existing hash
# We get (nil)
# Command: hget nonexistinghash somefield
# Result: (nil)
commandResult = redis.hget("nonexistinghash", "somefield")
print("Command: hget nonexistinghash somefield | 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 HGET on a string type of key
# We get an error
# Command: hget bigboxstr somefield
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.hget("bigboxstr", "somefield")
print("Command: hget bigboxstr somefield | Result: ", commandResult, "n")
rescue => e
print("Command: hget bigboxstr somefield | Error: ", e, "n")
end
Output:
Command: hset customer:99:address street "2855 W 76 Country Blvd" city Branson state Mississippi zip 65616 country "United States" | Result: 5
Command: hget customer:99:address zip | Result: 65616
Command: hget customer:99:address state | Result: Mississippi
Command: hget customer:99:address nonexistingfield | Result:
Command: hget nonexistinghash somefield | Result:
Command: set bigboxstr "some string in the box" | Result: OK
Command: hget bigboxstr somefield | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “
hget
” from the redis-rb. - Signature of the method is-
# @param [String] key
# @param [String] field
# @return [String]
def hget(key, field)
Source Code
Use the following links to get the source code used in this article-
Source Code of | Source Code Link |
---|---|
Command Examples | GitHub |
Golang Implementation | GitHub |
NodeJS Implementation | GitHub |
Java Implementation | GitHub |
C# Implementation | GitHub |
PHP Implementation | GitHub |
Python Implementation | GitHub |
Ruby Implementation | GitHub |
Related Commands
Command | Details |
---|---|
HSETNX | Command Details |
HMGET | Command Details |
HGETALL | Command Details |
HSET | Command Details |