Summary
Command Name | HKEYS |
Usage | Get all field names of a hash |
Group | hash |
ACL Category | @read @hash @slow |
Time Complexity | O(N) |
Flag | READONLY |
Arity | 2 |
Notes
- Time complexity is O(N), where N is the number of fields in the hash.
Signature
HKEYS <key>
Usage
Get the list of all fields of a hash. All the items returned, are unique as the field names are unique in a hash.
Arguments
Parameter | Description | Name | Type |
---|---|---|---|
<key> | Name of the key of the hash | key | key |
Return Value
Return value | Case for the return value | Type |
---|---|---|
List of keys | On successful we get the list of keys | array[string] |
(empty array) | If key does not exist | (empty array) |
error | If applied to wrong data type | error |
Notes
- If the key does not exist then we get (empty array) in response, which represents an empty array when used in any programming language.
- 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 HKEYS command-
# Redis HKEYS command examples
# Set hash field/value
127.0.0.1:6379> hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
(integer) 8
# Check hash full data
127.0.0.1:6379> hgetall customer:1786:address
1) "street"
2) "6414 Losee Rd"
3) "city"
4) "North Las Vegas"
5) "state"
6) "North Carolina"
7) "zip"
8) "89086"
9) "phone"
10) "(702) 399-9939"
11) "country"
12) "United States"
13) "latutude"
14) "36.27704"
15) "longitude"
16) "-115.115868"
# Get all the keys of hash
127.0.0.1:6379> hkeys customer:1786:address
1) "street"
2) "city"
3) "state"
4) "zip"
5) "phone"
6) "country"
7) "latutude"
8) "longitude"
# Use HKEYS on a non existing key
# We get (empty list)
127.0.0.1:6379> hkeys nonexistingkey
(empty array)
# Set string value
127.0.0.1:6379> set bigboxstr "some stiring value for HKEYS command testing"
OK
# Try to use HKEYS on a hash
# We get an error
127.0.0.1:6379> hkeys bigboxstr
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Code Implementations
Redis HKEYS command example implementation in Golang, NodeJS, Java, C#, PHP, Python, Ruby-
// Redis HKEYS command example in Golang
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
var rdb *redis.Client
var ctx context.Context
func init() {
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "default",
Password: "",
DB: 0,
})
ctx = context.Background()
}
func main() {
/**
* Set hash field/value
* Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
* Result: (integer) 8
*/
hsetResult, err := rdb.HSet(ctx, "customer:1786:address",
"street", "6414 Losee Rd",
"city", "North Las Vegas",
"state", "North Carolina",
"zip", "89086",
"phone", "(702) 399-9939",
"country", "United States",
"latitude", "36.27704",
"longitude", "-115.115868",
).Result()
if err != nil {
fmt.Println("Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Error: " + err.Error())
}
fmt.Println("Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: ", hsetResult)
/**
* Check hash full data
* Command: hgetall customer:1786:address
* Result:
* 1) "street"
* 2) "6414 Losee Rd"
* 3) "city"
* 4) "North Las Vegas"
* 5) "state"
* 6) "North Carolina"
* 7) "zip"
* 8) "89086"
* 9) "phone"
* 10) "(702) 399-9939"
* 11) "country"
* 12) "United States"
* 13) "latutude"
* 14) "36.27704"
* 15) "longitude"
* 16) "-115.115868"
*/
hgetAllResult, err := rdb.HGetAll(ctx, "customer:1786:address").Result()
if err != nil {
fmt.Println("Command: hgetall customer:1786:address | Error: " + err.Error())
}
fmt.Println("Command: hgetall customer:1786:address | Result: ", hgetAllResult)
/**
* Get all the keys of hash
*
* Command: hkeys customer:1786:address
* Result:
* 1) "street"
* 2) "city"
* 3) "state"
* 4) "zip"
* 5) "phone"
* 6) "country"
* 7) "latutude"
* 8) "longitude"
*/
hkeysResult, err := rdb.HKeys(ctx, "customer:1786:address").Result()
if err != nil {
fmt.Println("Command: hkeys customer:1786:address | Error: " + err.Error())
}
fmt.Println("Command: hkeys customer:1786:address | Result: ", hkeysResult)
/**
* Use HKEYS on a non existing key
* We get (empty list)
*
* Command: hkeys nonexistingkey
* Result: (empty array)
*/
hkeysResult, err = rdb.HKeys(ctx, "nonexistingkey").Result()
if err != nil {
fmt.Println("Command: hkeys nonexistingkey | Error: " + err.Error())
}
fmt.Println("Command: hkeys nonexistingkey | Result: ", hkeysResult)
/**
* Set string value
*
* Command: set bigboxstr "some stiring value for HKEYS command testing"
* Result: OK
*/
setResult, err := rdb.Set(ctx, "bigboxstr", "some stiring value for HKEYS command testing", 0).Result()
if err != nil {
fmt.Println("Command: set bigboxstr "some stiring value for HKEYS command testing" | Error: " + err.Error())
}
fmt.Println("Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: " + setResult)
/**
* Try to use HKEYS on a hash
* We get an error
*
* Command: hkeys bigboxstr
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
hkeysResult, err = rdb.HKeys(ctx, "bigboxstr").Result()
if err != nil {
fmt.Println("Command: hkeys bigboxstr | Error: " + err.Error())
}
fmt.Println("Command: hkeys bigboxstr | Result: ", hkeysResult)
}
Output:
Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8
Command: hgetall customer:1786:address | Result: map[city:North Las Vegas country:United States latitude:36.27704 longitude:-115.115868 phone:(702) 399-9939 state:North Carolina street:6414 Losee Rd zip:89086]
Command: hkeys customer:1786:address | Result: [street city state zip phone country latitude longitude]
Command: hkeys nonexistingkey | Result: []
Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: OK
Command: hkeys bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hkeys bigboxstr | Result: []
Notes
- Use “
HKeys
” method from redis-go module. - Signature of the method is-
HKeys(ctx context.Context, key string) *StringSliceCmd
// Redis HKEYS command example in JavaScript(NodeJS)
import { createClient } from "redis";
// Create redis client
const redisClient = createClient({
url: "redis://default:@localhost:6379",
});
redisClient.on("error", (err) =>
console.log("Error while connecting to Redis", err)
);
// Connect Redis client
await redisClient.connect();
/**
* Set hash field/value
* Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
* Result: (integer) 8
*/
let commandResult = await redisClient.hSet("customer:1786:address", {
street: "6414 Losee Rd",
city: "North Las Vegas",
state: "North Carolina",
zip: "89086",
phone: "(702) 399-9939",
country: "United States",
latitude: "36.27704",
longitude: "-115.115868",
});
console.log(
'Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: ' +
commandResult
);
/**
* Check hash full data
* Command: hgetall customer:1786:address
* Result:
* 1) "street"
* 2) "6414 Losee Rd"
* 3) "city"
* 4) "North Las Vegas"
* 5) "state"
* 6) "North Carolina"
* 7) "zip"
* 8) "89086"
* 9) "phone"
* 10) "(702) 399-9939"
* 11) "country"
* 12) "United States"
* 13) "latutude"
* 14) "36.27704"
* 15) "longitude"
* 16) "-115.115868"
*/
commandResult = await redisClient.hGetAll("customer:1786:address");
console.log(
"Command: hgetall customer:1099:address | Result: ",
commandResult
);
/**
* Get all the keys of hash
* Command: hkeys customer:1786:address
* Result:
* 1) "street"
* 2) "city"
* 3) "state"
* 4) "zip"
* 5) "phone"
* 6) "country"
* 7) "latutude"
* 8) "longitude"
*/
commandResult = await redisClient.hKeys("customer:1786:address");
console.log(
"Command: hgetall customer:1099:address | Result: ",
commandResult
);
/**
* Use HKEYS on a non existing key
* We get (empty list)
*
* Command: hkeys nonexistingkey
* Result: (empty array)
*/
commandResult = await redisClient.hKeys("nonexistingkey");
console.log("Command: hkeys nonexistingkey | Result: ", commandResult);
/**
* Set string value
* Command: set bigboxstr "some stiring value for HKEYS command testing"
* Result: OK
*/
commandResult = await redisClient.set(
"bigboxstr",
"some stiring value for HKEYS command testing"
);
console.log(
'Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: ',
commandResult
);
/**
* Try to use HKEYS on a hash
* We get an error
* Command: hkeys bigboxstr
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.hKeys("bigboxstr");
console.log("Command: hkeys bigboxstr | Result: " + commandResult);
} catch (err) {
console.log("Command: hkeys bigboxstr | Error: ", err);
}
process.exit(0);
Output:
Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8
Command: hgetall customer:1099:address | Result: [Object: null prototype] {
street: '6414 Losee Rd',
city: 'North Las Vegas',
state: 'North Carolina',
zip: '89086',
phone: '(702) 399-9939',
country: 'United States',
latitude: '36.27704',
longitude: '-115.115868'
}
Command: hgetall customer:1099:address | Result: [
'street', 'city',
'state', 'zip',
'phone', 'country',
'latitude', 'longitude'
]
Command: hkeys nonexistingkey | Result: []
Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: OK
Command: hkeys bigboxstr | Error: [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
Notes
- Use the function “
hKeys
” of node-redis. - Signature of the method-
function hKeys(key: RedisCommandArgument)
// Redis HKEYS Command example in Java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HKeys {
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Set hash field/value
*
* Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
* Result: (integer) 8
*/
Map<String, String> hashData = new HashMap<>() {{
put("street", "6414 Losee Rd");
put("city", "North Las Vegas");
put("state", "North Carolina");
put("zip", "89086");
put("phone", "(702) 399-9939");
put("country", "United States");
put("latitude", "36.27704");
put("longitude", "-115.115868");
}};
long hsetResult = jedis.hset("customer:1786:address", hashData);
System.out.println("Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: " + hsetResult);
/**
* Check hash full data
*
* Command: hgetall customer:1786:address
* Result:
* 1) "street"
* 2) "6414 Losee Rd"
* 3) "city"
* 4) "North Las Vegas"
* 5) "state"
* 6) "North Carolina"
* 7) "zip"
* 8) "89086"
* 9) "phone"
* 10) "(702) 399-9939"
* 11) "country"
* 12) "United States"
* 13) "latutude"
* 14) "36.27704"
* 15) "longitude"
* 16) "-115.115868"
*/
Map<String, String> hgetAllResult = jedis.hgetAll("customer:1786:address");
System.out.println("Command: hgetall customer:1099:address | Result: " + hgetAllResult.toString());
/**
* Get all the keys of hash
*
* Command: hkeys customer:1786:address
* Result:
* 1) "street"
* 2) "city"
* 3) "state"
* 4) "zip"
* 5) "phone"
* 6) "country"
* 7) "latutude"
* 8) "longitude"
*/
Set<String> hkeysResult = jedis.hkeys("customer:1786:address");
System.out.println("Command: hkeys customer:1099:address | Result: " + hkeysResult.toString());
/**
* Use HKEYS on a non existing key
* We get (empty list)
*
* Command: hkeys nonexistingkey
* Result: (empty array)
*/
hkeysResult = jedis.hkeys("nonexistingkey");
System.out.println("Command: hkeys nonexistingkey | Result: " + hkeysResult);
/**
* Set string value
*
* Command: set bigboxstr "some stiring value for HKEYS command testing"
* Result: OK
*/
String setResult = jedis.set("bigboxstr", "some stiring value for HKEYS command testing");
System.out.println("Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: " + setResult);
/**
* Try to use HKEYS on a hash
* We get an error
*
* Command: hkeys bigboxstr
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
hkeysResult = jedis.hkeys("bigboxstr");
System.out.println("Command: hkeys bigboxstr | Result: " + hkeysResult);
} catch (Exception e) {
System.out.println("Command: hkeys bigboxstr | Error: " + e.getMessage());
}
}
jedisPool.close();
}
}
Output:
Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8
Command: hgetall customer:1099:address | Result: {zip=89086, country=United States, state=North Carolina, city=North Las Vegas, phone=(702) 399-9939, street=6414 Losee Rd, latitude=36.27704, longitude=-115.115868}
Command: hgetall customer:1099:address | Result: [zip, country, city, phone, street, latitude, state, longitude]
Command: hkeys nonexistingkey | Result: []
Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: OK
Command: hkeys bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “
hkeys
” from Jedis package. - The signature of the method is-
public Set<String> hkeys(final String key)
// Redis HKEYS command examples in C#
using StackExchange.Redis;
using System.Collections.Generic;
namespace HKeys
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Set hash field/value
*
* Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
* Result: (integer) 8
*/
HashEntry[] hashData = new HashEntry[] {
new HashEntry("street", "6414 Losee Rd"),
new HashEntry("city", "North Las Vegas"),
new HashEntry("state", "North Carolina"),
new HashEntry("zip", "89086"),
new HashEntry("phone", "(702) 399-9939"),
new HashEntry("country", "United States"),
new HashEntry("latitude", "36.27704"),
new HashEntry("longitude", "-115.115868"),
};
rdb.HashSet("customer:1786:address", hashData);
Console.WriteLine("Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868");
/**
* Check hash full data
*
* Command: hgetall customer:1786:address
* Result:
* 1) "street"
* 2) "6414 Losee Rd"
* 3) "city"
* 4) "North Las Vegas"
* 5) "state"
* 6) "North Carolina"
* 7) "zip"
* 8) "89086"
* 9) "phone"
* 10) "(702) 399-9939"
* 11) "country"
* 12) "United States"
* 13) "latutude"
* 14) "36.27704"
* 15) "longitude"
* 16) "-115.115868"
*/
HashEntry[] hgetAllResult = rdb.HashGetAll("customer:1786:address");
Console.WriteLine("Command: hgetall customer:1099:address | Result: " + String.Join(", ", hgetAllResult));
/**
* Get all the keys of hash
*
* Command: hkeys customer:1786:address
* Result:
* 1) "street"
* 2) "city"
* 3) "state"
* 4) "zip"
* 5) "phone"
* 6) "country"
* 7) "latutude"
* 8) "longitude"
*/
RedisValue[] hkeysResult = rdb.HashKeys("customer:1786:address");
Console.WriteLine("Command: hkeys customer:1099:address | Result: " + String.Join(", ", hkeysResult));
/**
* Use HKEYS on a non existing key
* We get (empty list)
*
* Command: hkeys nonexistingkey
* Result: (empty array)
*/
hkeysResult = rdb.HashKeys("nonexistingkey");
Console.WriteLine("Command: hkeys nonexistingkey | Result: " + String.Join(", ", hkeysResult));
/**
* Set string value
*
* Command: set bigboxstr "some stiring value for HKEYS command testing"
* Result: OK
*/
bool setResult = rdb.StringSet("bigboxstr", "some stiring value for HKEYS command testing");
Console.WriteLine("Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: " + setResult);
/**
* Try to use HKEYS on a hash
* We get an error
*
* Command: hkeys bigboxstr
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
hkeysResult = rdb.HashKeys("bigboxstr");
Console.WriteLine("Command: hkeys bigboxstr | Result: " + String.Join(", ", hkeysResult));
}
catch (Exception e)
{
Console.WriteLine("Command: hkeys bigboxstr | Error: " + e.Message);
}
}
}
}
Output:
Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
Command: hgetall customer:1099:address | Result: street: 6414 Losee Rd, city: North Las Vegas, state: North Carolina, zip: 89086, phone: (702) 399-9939, country: United States, latitude: 36.27704, longitude: -115.115868
Command: hkeys customer:1099:address | Result: street, city, state, zip, phone, country, latitude, longitude
Command: hkeys nonexistingkey | Result:
Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: True
Command: hkeys bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “
HashKeys
” from StackExchange.Redis. - Signatures of the method are-
RedisValue[] HashKeys(RedisKey key, CommandFlags flags = CommandFlags.None)
<?php
// Redis HKEYS command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Set hash field/value
* Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
* Result: (integer) 8
*/
$commandResult = $redisClient->hset("customer:1786:address",
"street", "6414 Losee Rd",
"city", "North Las Vegas",
"state", "North Carolina",
"zip", "89086",
"phone", "(702) 399-9939",
"country", "United States",
"latitude", "36.27704",
"longitude", "-115.115868",
);
echo "Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: " . $commandResult . "n";
/**
* Check hash full data
* Command: hgetall customer:1786:address
* Result:
* 1) "street"
* 2) "6414 Losee Rd"
* 3) "city"
* 4) "North Las Vegas"
* 5) "state"
* 6) "North Carolina"
* 7) "zip"
* 8) "89086"
* 9) "phone"
* 10) "(702) 399-9939"
* 11) "country"
* 12) "United States"
* 13) "latutude"
* 14) "36.27704"
* 15) "longitude"
* 16) "-115.115868"
*/
$commandResult = $redisClient->hgetall("customer:1786:address");
echo "Command: hgetall customer:1099:address | Result:";
print_r($commandResult);
/**
* Get all the keys of hash
* Command: hkeys customer:1786:address
* Result:
* 1) "street"
* 2) "city"
* 3) "state"
* 4) "zip"
* 5) "phone"
* 6) "country"
* 7) "latutude"
* 8) "longitude"
*/
$commandResult = $redisClient->hkeys("customer:1786:address");
echo "Command: hgetall customer:1099:address | Result:";
print_r($commandResult);
/**
* Use HKEYS on a non existing key
* We get (empty list)
*
* Command: hkeys nonexistingkey
* Result: (empty array)
*/
$commandResult = $redisClient->hkeys("nonexistingkey");
echo "Command: hkeys nonexistingkey | Result:";
print_r($commandResult);
/**
* Set string value
* Command: set bigboxstr "some stiring value for HKEYS command testing"
* Result: OK
*/
$commandResult = $redisClient->set("bigboxstr", "some stiring value for HKEYS command testing");
echo "Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: " . $commandResult . "n";
/**
* Try to use HKEYS on a hash
* We get an error
* Command: hkeys bigboxstr
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
$commandResult = $redisClient->hkeys("bigboxstr");
echo "Command: hkeys bigboxstr | Result:";
print_r($commandResult);
} catch (Exception $e) {
echo "Command: hkeys bigboxstr | Error: " . $e->getMessage() . "n";
}
Output:
Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8
Command: hgetall customer:1099:address | Result:Array
(
[street] => 6414 Losee Rd
[city] => North Las Vegas
[state] => North Carolina
[zip] => 89086
[phone] => (702) 399-9939
[country] => United States
[latitude] => 36.27704
[longitude] => -115.115868
)
Command: hgetall customer:1099:address | Result:Array
(
[0] => street
[1] => city
[2] => state
[3] => zip
[4] => phone
[5] => country
[6] => latitude
[7] => longitude
)
Command: hkeys nonexistingkey | Result:Array
(
)
Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: OK
Command: hkeys bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “
hkeys
” of predis. - Signature of the method is-
hkeys(string $key): array
# Redis HKEYS command example in Python
import redis
import time
# Create Redis client
redisClient = redis.Redis(
host="localhost", port=6379, username="default", password="", decode_responses=True
)
# Set hash field/value
# Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
# Result: (integer) 8
commandResult = redisClient.hset(
"customer:1786:address",
mapping={
"street": "6414 Losee Rd",
"city": "North Las Vegas",
"state": "North Carolina",
"zip": "89086",
"phone": "(702) 399-9939",
"country": "United States",
"latitude": "36.27704",
"longitude": "-115.115868",
},
)
print(
'Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: {}'.format(
commandResult
)
)
# Check hash full data
# Command: hgetall customer:1786:address
# Result:
# 1) "street"
# 2) "6414 Losee Rd"
# 3) "city"
# 4) "North Las Vegas"
# 5) "state"
# 6) "North Carolina"
# 7) "zip"
# 8) "89086"
# 9) "phone"
# 10) "(702) 399-9939"
# 11) "country"
# 12) "United States"
# 13) "latutude"
# 14) "36.27704"
# 15) "longitude"
# 16) "-115.115868"
commandResult = redisClient.hgetall("customer:1786:address")
print("Command: hgetall customer:1099:address | Result: {}".format(commandResult))
# Get all the keys of hash
# Command: hkeys customer:1786:address
# Result:
# 1) "street"
# 2) "city"
# 3) "state"
# 4) "zip"
# 5) "phone"
# 6) "country"
# 7) "latutude"
# 8) "longitude"
commandResult = redisClient.hkeys("customer:1786:address")
print("Command: hgetall customer:1099:address | Result: {}".format(commandResult))
# Use HKEYS on a non existing key
# We get (empty list)
# Command: hkeys nonexistingkey
# Result: (empty array)
commandResult = redisClient.hkeys("nonexistingkey")
print("Command: hkeys nonexistingkey | Result: {}".format(commandResult))
# Set string value
# Command: set bigboxstr "some stiring value for HKEYS command testing"
# Result: OK
commandResult = redisClient.set(
"bigboxstr", "some stiring value for HKEYS command testing"
)
print(
'Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: {}'.format(
commandResult
)
)
# Try to use HKEYS on a hash
# We get an error
# Command: hkeys bigboxstr
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.hkeys("bigboxstr")
print("Command: hkeys bigboxstr | Result: {}".format(commandResult))
except Exception as error:
print("Command: hkeys bigboxstr | Error: ", error)
Output:
Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8
Command: hgetall customer:1099:address | Result: {'street': '6414 Losee Rd', 'city': 'North Las Vegas', 'state': 'North Carolina', 'zip': '89086', 'phone': '(702) 399-9939', 'country': 'United States', 'latitude': '36.27704', 'longitude': '-115.115868'}
Command: hgetall customer:1099:address | Result: ['street', 'city', 'state', 'zip', 'phone', 'country', 'latitude', 'longitude']
Command: hkeys nonexistingkey | Result: []
Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: True
Command: hkeys bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “
hkeys
” from redis-py. - Signature of the method is –
def hkeys(self, name: str) -> Union[Awaitable[List], List]
# Redis HKEYS command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Set hash field/value
# Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868
# Result: (integer) 8
commandResult = redis.hset("customer:1786:address", {
"street" => "6414 Losee Rd",
"city" => "North Las Vegas",
"state" => "North Carolina",
"zip" => "89086",
"phone" => "(702) 399-9939",
"country" => "United States",
"latitude" => "36.27704",
"longitude" => "-115.115868",
},
)
print("Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: ", commandResult, "n")
# Check hash full data
# Command: hgetall customer:1786:address
# Result:
# 1) "street"
# 2) "6414 Losee Rd"
# 3) "city"
# 4) "North Las Vegas"
# 5) "state"
# 6) "North Carolina"
# 7) "zip"
# 8) "89086"
# 9) "phone"
# 10) "(702) 399-9939"
# 11) "country"
# 12) "United States"
# 13) "latutude"
# 14) "36.27704"
# 15) "longitude"
# 16) "-115.115868"
commandResult = redis.hgetall("customer:1786:address")
print("Command: hgetall customer:1099:address | Result: ", commandResult, "n")
# Get all the keys of hash
# Command: hkeys customer:1786:address
# Result:
# 1) "street"
# 2) "city"
# 3) "state"
# 4) "zip"
# 5) "phone"
# 6) "country"
# 7) "latutude"
# 8) "longitude"
commandResult = redis.hkeys("customer:1786:address")
print("Command: hgetall customer:1099:address | Result: ", commandResult, "n")
# Use HKEYS on a non existing key
# We get (empty list)
# Command: hkeys nonexistingkey
# Result: (empty array)
commandResult = redis.hkeys("nonexistingkey")
print("Command: hkeys nonexistingkey | Result: ", commandResult, "n")
# Set string value
# Command: set bigboxstr "some stiring value for HKEYS command testing"
# Result: OK
commandResult = redis.set(
"bigboxstr", "some stiring value for HKEYS command testing"
)
print("Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: ", commandResult, "n")
# Try to use HKEYS on a hash
# We get an error
# Command: hkeys bigboxstr
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.hkeys("bigboxstr")
print("Command: hkeys bigboxstr | Result: ", commandResult, "n")
rescue => e
print("Command: hkeys bigboxstr | Error: ", e, "n")
end
Output:
Command: hset customer:1786:address street "6414 Losee Rd" city "North Las Vegas" state "North Carolina" zip "89086" phone "(702) 399-9939" country "United States" latutude 36.27704 longitude -115.115868 | Result: 8
Command: hgetall customer:1099:address | Result: {"street"=>"6414 Losee Rd", "city"=>"North Las Vegas", "state"=>"North Carolina", "zip"=>"89086", "phone"=>"(702) 399-9939", "country"=>"United States", "latitude"=>"36.27704", "longitude"=>"-115.115868"}
Command: hgetall customer:1099:address | Result: ["street", "city", "state", "zip", "phone", "country", "latitude", "longitude"]
Command: hkeys nonexistingkey | Result: []
Command: set bigboxstr "some stiring value for HKEYS command testing" | Result: OK
Command: hkeys bigboxstr | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “
hkeys
” from the redis-rb. - Signature of the method is-
# @param [String] key
# @return [Array<String>]
def hkeys(key)
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 |