Chapter 6: Opening the Way of Operation and Maintenance - Redis's Five Data Types: string, list, hash, set, zset

Keywords: Redis shell Java Attribute

1. The last article introduced the basis of Redis and provided two ways to start it. Here we summarize the following:

Write shell script and run shell script. See the end of the last article for details.

②[root@localhost ~]# cd /usr/local/redis/

[root@localhost redis]# ./bin/redis-server ./redis.conf

Check whether the startup mode is:

[root@localhost redis]# ps -ef | grep -i redis
Or see if you can ping:

[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

Stop service: [root@localhost redis]#. / bin/redis-cli shutdown

 

The following strings need to be learned into the redis client. The command goes to the redis client as described above:

[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379>

2. string Type of Redis Data Type

Common functions of string: setting values; selecting values; deleting values; increasing or decreasing values; and other extensions (ignoring case, SET, set, Set can be used)

Set value: set key value sets the value of the specified key

(2) Value: get key to get the value of the specified key

127.0.0.1:6379> set myKey biandanLoveYou
OK
127.0.0.1:6379> get myKey
"biandanLoveYou"

Delete: del key deletes the value of the specified key

127.0.0.1:6379> del myKey
(integer) 1
127.0.0.1:6379> get myKey
(nil)

(4) Value increase or decrease:

incr key is self-increasing, if not defined, it defaults to start at 0; non-numeric types report errors.

127.0.0.1:6379> incr n
(integer) 1
127.0.0.1:6379> get n
"1"

Following are examples of self-increasing numerical values and non-numerical errors:

The numerical value increases by itself:

127.0.0.1:6379> set n1 5
OK
127.0.0.1:6379> incr n1
(integer) 6
127.0.0.1:6379> get n1
"6"

Non-numerical self-increasing may cause errors:

127.0.0.1:6379> set n2 boy
OK
127.0.0.1:6379> incr n2
(error) ERR value is not an integer or out of range

Decrease: decr key starts at 0 by default if it is not defined; non-numeric types will report errors.

127.0.0.1:6379> decr m
(integer) -1
127.0.0.1:6379> get m
"-1"
127.0.0.1:6379> decr n1
(integer) 5
127.0.0.1:6379> get n1
"5"
127.0.0.1:6379> decr n2
(error) ERR value is not an integer or out of range

tip: generally set the number of goods, will be deposited in redis, users buy one, reduce by 1.

string extension command:

incrby key number specifies increment; decrby key number specifies decrement; append key str appends string.

127.0.0.1:6379> get m
"4"
127.0.0.1:6379> incrby m 5
(integer) 9
127.0.0.1:6379> get m
"9"
127.0.0.1:6379> decrby m 2
(integer) 7
127.0.0.1:6379> get m
"7"
127.0.0.1:6379> get n2
"boy"
127.0.0.1:6379> append n2 girl
(integer) 7
127.0.0.1:6379> get n2
"boygirl"

3. list of Redis Data Types

The Redis list is a simple list of strings, sorted in insertion order. You can add an element to the top (left) or tail (right) of the list.
A list can contain up to 232 - 1 elements (4294967295, with more than 4 billion elements per list).

You can understand list as follows: the train from the right to the left, the front on the left and the rear on the right.

Common commands: add at both ends; pop-up at both ends; expand commands

(1) Addition at both ends:

Add on the left: lpush key value, lpush (left push meaning) note that from the left to start adding, the next added element will rank on the left of the top one, squeezing the last element on the right. The last element added squeezes all the other elements to the right. Generally, it is seldom used to add from the left. Because the default on the left is the headstock.

127.0.0.1:6379> lpush myList1 a b c d haha
(integer) 5
127.0.0.1:6379> lpush myList1 1 2 5 88 90
(integer) 10

Note that each data type has commands to add, retrieve, and delete. For example, get commands cannot be used to retrieve list s.

127.0.0.1:6379> get myList1
(error) WRONGTYPE Operation against a key holding the wrong kind of value

To get the list collection, start from the left, and use: lrange key start stop (left range meaning): start is the start position, stop is the end position, and - 1 is the length - 1, just like Java's for loop list.

127.0.0.1:6379> lrange myList1 0 -1
 1) "90"
 2) "88"
 3) "5"
 4) "2"
 5) "1"
 6) "haha"
 7) "d"
 8) "c"
 9) "b"
10) "a"

List does not have the concept of array crossing, such as the value of stop can be larger than the length of list, which will get all list data:

127.0.0.1:6379> lrange myList1 0 20

127.0.0.1:6379> lrange myList1 0 20
 1) "90"
 2) "88"
 3) "5"
 4) "2"
 5) "1"
 6) "haha"
 7) "d"
 8) "c"
 9) "b"
10) "a"

Add (most people use): rpush key value, rpush (right push meaning) such as: 127.0.0.1:6379 > rpush myList1 boy girl

127.0.0.1:6379> lrange myList1 0 -1
 1) "90"
 2) "88"
 3) "5"
 4) "2"
 5) "1"
 6) "haha"
 7) "d"
 8) "c"
 9) "b"
10) "a"
11) "boy"
12) "girl"

(2) Get the length of the list list: llen key

127.0.0.1:6379> llen myList1
(integer) 12

(3) Obtaining data through index: lindex key

127.0.0.1:6379> lindex myList1 2
"5"

(4) Remove from the left and get the first element of the list: lpop key
First create a list of tests and add elements:

127.0.0.1:6379> lpush myList2  11 22 33 boy friend
(integer) 5
127.0.0.1:6379> lrange myList2 0 -1
1) "friend"
2) "boy"
3) "33"
4) "22"
5) "11"

Then move out and get one element on the left (that is, the last added element, the other elements are squeezed on the right by the last element):

127.0.0.1:6379> lpop myList2
"friend"
127.0.0.1:6379> lrange myList2 0 -1
1) "boy"
2) "33"
3) "22"
4) "11"

It was found that the friend element was removed and acquired.

Remove from the right and get the first element of the list: rpop key

127.0.0.1:6379> rpop myList2
"11"
127.0.0.1:6379> lrange myList2 0 -1
1) "boy"
2) "33"
3) "22"

Adding elements to the head and tail of the set: lpushx key value 1 value 2... rpushx key value 1 value 2....

127.0.0.1:6379> lpushx myList2 good
(integer) 4
127.0.0.1:6379> rpushx myList2 bad
(integer) 5
127.0.0.1:6379> lrange myList2 0 -1
1) "good"
2) "boy"
3) "33"
4) "22"
5) "bad"

_Delete the collection element from the specified direction: lrem key count value, paying special attention to this count

First add a test collection myList3

127.0.0.1:6379> lpush myList3 2 4 6 8 1 8 6 4 2
(integer) 9
127.0.0.1:6379> lrange myList3 0 -1
1) "2"
2) "4"
3) "6"
4) "8"
5) "1"
6) "8"
7) "6"
8) "4"
9) "2"

From the left, delete 2 6:lrem myList 326

127.0.0.1:6379> lrem myList3 2 6
(integer) 2
127.0.0.1:6379> lrange myList3 0 -1
1) "2"
2) "4"
3) "8"
4) "1"
5) "8"
6) "4"
7) "2"

Description: Start on the left, count is a positive integer; start on the right, count is a negative integer. 0 means deleting all

From the right, delete 1 4:lrem myList 3-14

127.0.0.1:6379> lrem myList3 -1 4
(integer) 1
127.0.0.1:6379> lrange myList3 0 -1
1) "2"
2) "4"
3) "8"
4) "1"
5) "8"
6) "2"

Delete all 1:lrem myList 301

127.0.0.1:6379> lrem myList3 0 1
(integer) 1
127.0.0.1:6379> lrange myList3 0 -1
1) "2"
2) "4"
3) "8"
4) "8"
5) "2"

(5) Set the value of the collection element: lset key index value. Note that the index starts at 0. It's the same as Java's list, array collection, starting from zero.

127.0.0.1:6379> lset myList3 2 66
OK
127.0.0.1:6379> lrange myList3 0 -1
1) "2"
2) "4"
3) "66"
4) "8"
5) "2"

In this way, the original 8 in the third position becomes 66.

_Add the element before and after the specified element: linsert key before|after pivot value

127.0.0.1:6379> linsert myList3 before 66 boy
(integer) 6
127.0.0.1:6379> linsert myList3 after 4 girl
(integer) 7
127.0.0.1:6379> lrange myList3 0 -1
1) "2"
2) "4"
3) "girl"
4) "boy"
5) "66"
6) "8"
7) "2"

Pop up the tail element of A set and insert it into the head of B set: rpoplpush sourceList descList

127.0.0.1:6379> rpoplpush myList2 myList3
"bad"
127.0.0.1:6379> lrange myList3 0 -1
1) "bad"
2) "2"
3) "4"
4) "girl"
5) "boy"
6) "66"
7) "8"
8) "2"

4. hash of Redis Data Type

Redis hash is a mapping table of field and value of string type, and hash is especially suitable for storing objects.
Each hash in Redis can store 232-1 key-value pairs (more than 4 billion).

Common hash commands: assign, take, delete, add numbers, determine whether a field exists, get the number of hash attributes, get all the names of hash attributes.

(1) Assignment and selection: hset key field value; hget key field value

127.0.0.1:6379> hset h1 username biandan
(integer) 1
127.0.0.1:6379> hget h1 username
"biandan"

Set values and take values together with multiple fields: hmset key field 1 value 1 field 2 value 2... Hmget key field 1 field 2

127.0.0.1:6379> hmset h2 username Hello password 123
OK
127.0.0.1:6379> hmget h2 username password
1) "Hello"
2) "123"

(2) Get all fields and values of the specified key in hash: hgetall key

127.0.0.1:6379> hgetall h2
1) "username"
2) "Hello"
3) "password"
4) "123"

(3) Delete attributes, you can delete more than one at a time: HDEL key field 1 field 2..

127.0.0.1:6379> hdel h2 username password
(integer) 2
127.0.0.1:6379> hgetall h2
(empty list or set)

(4) Increase the number: hincrby key field count

127.0.0.1:6379> hset h3 age 15
(integer) 1
127.0.0.1:6379> hincrby h3 age 5
(integer) 20
127.0.0.1:6379> hget h3 age
"20"

(5) Determine whether a field exists: there is 1, there is no 0: hexists key field

127.0.0.1:6379> hexists h3 age
(integer) 1
127.0.0.1:6379> hexists h3 tall
(integer) 0

Get the number of fields: hlen key

127.0.0.1:6379> hlen h3
(integer) 1

Get all attribute names (field names): hkeys key

127.0.0.1:6379> hkeys h1
1) "username"
127.0.0.1:6379> hkeys h2
(empty list or set)

_Get all attribute values: hvals key

127.0.0.1:6379> hvals h1
1) "biandan"
127.0.0.1:6379> hvals h2
(empty list or set)

5. set of Redis Data Types

Redis Set is an unordered collection of String types. Collection members are unique, which means that there can be no duplicate data in the collection.
Collections in Redis are implemented through hash tables, so the complexity of adding, deleting and searching is O(1).
The largest number of members in the set is 232 - 1 (4294967295, each set can store more than 4 billion members).

set Common Commands:
Add/delete elements, get elements in the set, union, intersection, difference set operations in the set, expand commands

(1) Add, retrieve and delete elements: Sadd key value 1 value 2... smembers key SREM key value 1 value 2...

Note: set does not allow duplicate elements to appear and does not add duplicate elements.

127.0.0.1:6379> sadd s1 n b a 1 2 5
(integer) 6
127.0.0.1:6379> smembers s1
1) "b"
2) "n"
3) "1"
4) "a"
5) "5"
6) "2"
127.0.0.1:6379> sadd s1 c b a
(integer) 1
127.0.0.1:6379> smembers s1
1) "c"
2) "a"
3) "b"
4) "1"
5) "n"
6) "5"
7) "2"
127.0.0.1:6379> srem s1 b
(integer) 1
127.0.0.1:6379> smembers s1
1) "c"
2) "a"
3) "1"
4) "n"
5) "5"
6) "2"

(2) Judging whether an element exists, there is 1, there is no 0:sismember key field.

127.0.0.1:6379> sismember s1 n
(integer) 1
127.0.0.1:6379> sismember s1 b
(integer) 0

(3) Calculating the total number of elements: scard key

127.0.0.1:6379> scard s1
(integer) 6

(4) Computing union, intersection and difference sets:

First add s2 for testing

127.0.0.1:6379> sadd s2 n c a a 5 8
(integer) 5
127.0.0.1:6379> smembers s1
1) "c"
2) "a"
3) "1"
4) "n"
5) "5"
6) "2"
127.0.0.1:6379> smembers s2
1) "a"
2) "c"
3) "5"
4) "n"
5) "8"

Union: sunion key1 key2

127.0.0.1:6379> sunion s1 s2
1) "c"
2) "a"
3) "8"
4) "1"
5) "n"
6) "2"
7) "5"

Intersection: sinter key1 key2

127.0.0.1:6379> sinter s1 s2
1) "a"
2) "c"
3) "5"
4) "n"

Difference Set: sdiff key1 key2 Note: Difference Set refers to key1 (former).

127.0.0.1:6379> sdiff s1 s2
1) "1"
2) "2"
127.0.0.1:6379> sdiff s2 s1
1) "8"

Put the union, intersection and difference sets into new sets

Merge into a new collection: sunionstore resultSet key1 key2

127.0.0.1:6379> sunionstore sr3 s1 s2
(integer) 7
127.0.0.1:6379> smembers sr3
1) "c"
2) "a"
3) "8"
4) "1"
5) "n"
6) "2"
7) "5"

Intersection into a new collection: sinterstore resultSet key1 key2

127.0.0.1:6379> sinterstore sr2 s1 s2
(integer) 4
127.0.0.1:6379> smembers sr2
1) "a"
2) "c"
3) "5"
4) "n"

The difference set is stored in a new set: sdiffstore resultSet key1 key2

127.0.0.1:6379> sdiffstore sr1 s1 s2
(integer) 2
127.0.0.1:6379> smembers sr1
1) "1"
2) "2"

Storing Set usage scenarios: Tracking some unique data to maintain association between data objects.

6. Ordered Set of Redis Data Types-zset

Redis ordered sets, like collections, are also collections of string-type elements and do not allow duplicate members.
The difference is that each element is associated with a double type score. redis ranks the members of a set from small to large by scores.
The members of an ordered set are unique, but scores can be repeated.
Collections are implemented through hash tables, so the complexity of adding, deleting and searching is O(1).  
The largest number of members in the set is 232 - 1 (4294967295, each set can store more than 4 billion members).

Sorted-Set usage scenarios:
Points List of Large Online Games
Building index data

(1) Adding and querying elements: zadd key score 1 field 1 field 1 score 2 field 2; zrange key start stop

127.0.0.1:6379> zadd z1 8 aa 5 bb 2 cc
(integer) 3
127.0.0.1:6379> zrange z1 0 -1
1) "cc"
2) "bb"
3) "aa"

Description: Add set z1, elements are aa, bb and cc, score is 8, 5 and 2, score before, element after.

Sorting in the set is based on the score from small to large, so the output with small score is the first.

Repeated additions: Overlay if you have one, add if you don't. In the example, aa already exists, covering; dd does not, adding.

127.0.0.1:6379> zadd z1 25 aa
(integer) 0
127.0.0.1:6379> zadd z1 35 dd
(integer) 1
127.0.0.1:6379> zrange z1 0 -1
1) "cc"
2) "bb"
3) "aa"
4) "dd"

The previous aa score was 8. Now let's see if the aa score is 25:zscore key filed.

127.0.0.1:6379> zscore z1 aa
"25"

It's 25. The proof is covered.

(2) Delete elements: zrem key field

127.0.0.1:6379> zrem z1 bb
(integer) 1
127.0.0.1:6379> zrange z1 0 -1
1) "cc"
2) "aa"
3) "dd"

(3) Look at the number of elements in the collection: zcard key field

127.0.0.1:6379> zcard z1
(integer) 3

View elements, including ratings, are also listed:

127.0.0.1:6379> zrange z1 0 -1 withscores
1) "cc"
2) "2"
3) "aa"
4) "25"
5) "dd"
6) "35"

(4) Descending sort output: zrevrange key start stop with cores.

127.0.0.1:6379> zrevrange z1 0 -1 withscores
1) "dd"
2) "35"
3) "aa"
4) "25"
5) "cc"
6) "2"

Add several test elements:

127.0.0.1:6379> zadd z1 3 y 5 n 30 w 18 q
(integer) 4
127.0.0.1:6379> zrange z1 0 -1 withscores
 1) "cc"
 2) "2"
 3) "y"
 4) "3"
 5) "n"
 6) "5"
 7) "q"
 8) "18"
 9) "aa"
10) "25"
11) "w"
12) "30"
13) "dd"
14) "35"

Find elements according to scoring range: zrange by score key score score score 1 score 2 withscores

127.0.0.1:6379> zrangebyscore z1 10 20 withscores
1) "q"
2) "18"

Limited search start, similar to paging: zrange by score key score score score 1 score 2 with hscores limit start stop

127.0.0.1:6379> zrangebyscore z1 2 20 withscores limit 0 3
1) "cc"
2) "2"
3) "y"
4) "3"
5) "n"
6) "5"

Find the number of elements within the specified scoring range: zcount key start stop

127.0.0.1:6379> zcount z1 3 30
(integer) 5

Add points to the specified element: zincrby key score field

127.0.0.1:6379> zincrby z1 13 cc
"15"

Delete elements by rank, and delete two: zremrange by rank key start stop (calculated from zero)

127.0.0.1:6379> zremrangebyrank z1 0 1
(integer) 2
127.0.0.1:6379> zrange z1 0 -1 withscores
 1) "cc"
 2) "15"
 3) "q"
 4) "18"
 5) "aa"
 6) "25"
 7) "w"
 8) "30"
 9) "dd"
10) "35"

Delete elements according to the scoring range: zremrange by score key start stop

127.0.0.1:6379> zremrangebyscore z1 10 20
(integer) 2
127.0.0.1:6379> zrange z1 0 -1 withscores
1) "aa"
2) "25"
3) "w"
4) "30"
5) "dd"
6) "35"

 

 

Posted by loureiro on Fri, 14 Dec 2018 08:24:03 -0800