Guides on Using the Commands in Redis Cheat Sheet Part 3

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Remote Dictionary Server (Redis) supports hashes, sets, lists, strings, and so much more. Since the data structure store is open source and features in-memory (diskless), it’s perfect for use as a message broker, cache or database. Automation is a key component of Redis too. It has built-in transactions handling and replication. Redis also handles large range queries that include indexes, bitmaps, and HyperLogLogs. What’s more, it’s compatible with most programming languages. Because Redis is comprehensive in scope and usage capabilities, there are many commands you’ll want to use on a regular basis. Use this Redis Cheat Sheet Part 3, and leave relying on your own instant command recall a thing of the past.

Prerequisites

  • Complete Parts 1 and 2 of “Guides on Using the Commands in Redis Cheat Sheet.”

  • Possess familiarity with using general Redis commands and data types in interactive mode command-line interface (Redis-cli).

Hash commands in Redis

Redis makes great use of containers. A Hash command stores a value there when you map a string name.

>NOTE: The following commands in this section of this Redis cheat sheet are key fields; therefore, they refer to the hash stored at key.

  • HSET – Sets the stored hash field key to value. If there isn’t a key, a new one with a hash is made. If there’s an existing key, it’s replaced with the new value.
1
2
127.0.0.1:6379> HSET hash field "Redis"
(integer) 1
  • HSETNX – If there’s no field, it sets the hash field at the key to value. And as always, a new key with a hash is made if there wasn’t a key there already. Nothing will happen if there’s a field prior to executing the HSETNX command.
1
2
127.0.0.1:6379> HSETNX hash field "Sample"
(integer) 0
  • HGET – Returns the stored field’s value in the hash.
1
2
127.0.0.1:6379> HGET hash field
"Sample"
  • HGETALL – All values and fields are returned. The reply is long because after a field is returned, its associated value follows.
1
2
3
4
5
6
7
8
9
10
11
127.0.0.1:6379> HSET hash field1 "Tutorial"
(integer) 1
127.0.0.1:6379> HSET hash field2 "Redis"
(integer) 1
127.0.0.1:6379> HGETALL hash
1) "field"
2) "Sample"
3) "field1"
4) "Tutorial"
5) "field2"
6) "Redis"
  • HMSET – Although HMSET was added to this Redis cheat sheet, this key-value field is deprecated as of the release of Redis 4.0.0. The now-defunct HMSET was used to set the stored fields with their associated values. The new key code, HSET replaced it. When it’s executed, a new value key is made if one doesn’t already exist.
1
2
127.0.0.1:6379> HMSET newhash field "object" field1 "rocket"
OK
  • HINCRBY – Puts the stored hash field number in increments. It also makes a new key with a hash if there isn’t one. The field’s value defaults to a 0 (zero) setting if there isn’t a field prior to using this command.
1
2
3
4
127.0.0.1:6379> HSET otherhash field 10
(integer) 1
127.0.0.1:6379> HINCRBY otherhash field 2
(integer) 12
  • HDEL – Deletes a specified stored hash field item. It considers the hash as having nothing in it if there isn’t a key. In that case, a 0 (zero) is returned.
1
2
127.0.0.1:6379> HDEL otherhash field
(integer) 1
  • HEXISTS – Returns a 1 (one) if the stored hash has a field. If there isn’t a key or field in the hash, a 0 (zero) is returned.
1
2
3
4
5
6
7
8
127.0.0.1:6379> HEXISTS hash field
(integer) 1
127.0.0.1:6379> HEXISTS hash field1
(integer) 1
127.0.0.1:6379> HEXISTS hash field2
(integer) 1
127.0.0.1:6379> HEXISTS hash field3
(integer) 0
  • HKEYS – Every field name in the stored hash is returned.
1
2
3
4
127.0.0.1:6379> HKEYS hash
1) "field"
2) "field1"
3) "field2"

*HLEN – Every field in the stored hash is returned.

1
2
127.0.0.1:6379> HLEN hash
(integer) 3

*HSTRLEN – The value’s string length relating to a stored hash field is returned.

1
2
3
4
5
6
127.0.0.1:6379> HSTRLEN hash field
(integer) 2
127.0.0.1:6379> HSTRLEN hash field1
(integer) 8
127.0.0.1:6379> HSTRLEN hash field2
(integer) 5

*HVALS – Every value in the stored hash is returned.

1
2
3
4
127.0.0.1:6379> HVALS hash
1) "10"
2) "Tutorial"
3) "Redis"

HyperLogLog commands in Redis

This Redis cheat sheet includes HyperLogLog commands. They provide a way to count large sets of uncommon or particular items. These different elements (cardinality) within a set can be approximately numbered somewhat with a HyperLogLog. In essence, you’ll get a very close estimation of elements.

>NOTE: The following commands in this section are key element commands that pertain only to the Redis HyperLogLog.

  • PFADD – Adds to the HyperLogLog the arguments of the element that are stored at the variable name indicated as first argument.
1
2
127.0.0.1:6379> PFADD hyper 1 2 3 4 5 6 7 8 9
(integer) 1

*PFCOUNT – Approximates the cardinality (count of unique elements) figured out by the HyperLogLog stored at an indicated variable. The variable specified must exist. If there isn’t one, 0 (zero) is returned.

1
2
127.0.0.1:6379> PFCOUNT hyper
(integer) 9
  • PFMERGE – Merges several HyperLogLog values into one. That new value estimates the cardinality of the combined source sets. A new variable destination is made if there isn’t one already set. If one has been set, then the count of that variable destination is added to the cardinality of the HyperLogLog computation.
1
2
3
4
5
6
7
8
127.0.0.1:6379> PFADD key dog cat horse crow
(integer) 1
127.0.0.1:6379> PFADD otherkey horse crow fish
(integer) 1
127.0.0.1:6379> PFMERGE newkey key otherkey
OK
127.0.0.1:6379> PFCOUNT newkey
(integer) 5

The Redis keys command

The KEYS command in Redis returns a list of all of the Redis keys available in a database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
127.0.0.1:6379> KEYS *
 1) "FLOAT"
 2) "list"
 3) "newkey2"
 4) "hash"
 5) "set"
 6) "key"
 7) "kv"
 8) "list1"
 9) "mykey"
10) "newlist"
11) "zset"
12) "otherkey"
13) "count"
14) "hyper1"
15) "key1"
16) "key3"
17) "znum"
18) "newhash"
19) "float"
20) "newkey1"
21) "hyper"
22) "player"
23) "set2"
24) "set1"
25) "newset2"
26) "1key"
27) "newkey"
28) "key2"
29) "sv1"
30) "value"
31) "2key"
32) "newset1"
33) "newset3"
34) "hyper2"
35) "sv"

Conclusion

The flexibility of Redis helps DBAs and other technical professionals work with large amounts of information seamlessly. And when using Redis, effectively managing data is easier when you have access to a comprehensive Redis cheat sheet. To that end, this guide was created to help you best utilize Redis your way. Keep parts 1 – 3 within reach.Ω

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.