跳转至

51. redis数据类型

1. 分类

string:     #字符类型
hash:       #字典类型
list:       #列表
set:        #集合
sorted set:  #有序集合

2. 举例(略)

col1 value1

[a,b,c,d]

(a,b,c,d)

()

3. 键的通用操作

keys *  #查看已存在所有键的名字(生产中尽量不用此命令,可能导致redis崩溃,使用如下命令)
keys a 
keys a* #查看以a开头的键
type  #返回键所存储值的类型
expire/pexpire   #以秒、毫秒为单位设定生存时间
ttl/pttl        #以秒、毫秒为单位返回生存时间
psersist        #取消生存时间设置
del #删除一个key
exists  #检查key是否存在
#1.keys演示
[root@152 6379]# redis-cli -a 123456
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> get a
(nil)
127.0.0.1:6379> keys a
(empty list or set)
127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> set aa 2
OK
127.0.0.1:6379> keys *
1) "a"
2) "aa"
127.0.0.1:6379> keys a
1) "a"
127.0.0.1:6379> keys a*
1) "a"
2) "aa"

#2.查看类型
127.0.0.1:6379> type a
string
127.0.0.1:6379> type aa
string


#3.生存时间
#没有值的key
127.0.0.1:6379> get c
(nil)
127.0.0.1:6379> expire c 10
(integer) 0
127.0.0.1:6379> ttl c
(integer) -2
127.0.0.1:6379> ttl c
(integer) -2

#有值的key
127.0.0.1:6379> get a
"1"
127.0.0.1:6379> EXPIRE a 60
(integer) 1
127.0.0.1:6379> ttl a
(integer) 57
127.0.0.1:6379> ttl a
(integer) 55
127.0.0.1:6379> ttl a
(integer) 52
127.0.0.1:6379> get a
"1"
127.0.0.1:6379> ttl a
(integer) 48


#4.删除key
127.0.0.1:6379> get aa
"2"
127.0.0.1:6379> del aa
(integer) 1
127.0.0.1:6379> get aa
(nil)

#5.查看key是否存在
127.0.0.1:6379> get a
"1"
127.0.0.1:6379> get aa
(nil)
127.0.0.1:6379> exists a
(integer) 1
127.0.0.1:6379> exists aa
(integer) 0

4. string类型

#1.应用场景
    常规计数
    如:微博数、粉丝数等。订阅、礼物、页游、直播、游戏、论坛、帖子等

#2.设置一个键值对
    set name zs

#3.设置多个键值对
    mset id 101 name zs age 20 gender m

#4.计数器
    incr num    #例如每点击一次关注,执行一次如下命令
    decr num

#5.暗箱操作
    incrby num 10000    #一次增加计数器10000
    decrby num 10000    #一次减少计数器10000
#1
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379>

#2.
127.0.0.1:6379> get num
(nil)
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> get num
"2"

#3.
127.0.0.1:6379> decr num
(integer) 1
127.0.0.1:6379> get num
"1"

#4. 
127.0.0.1:6379> incrby num 10000
(integer) 10003
127.0.0.1:6379> get num
"10003"
127.0.0.1:6379> decrby num 100
(integer) 9903
127.0.0.1:6379> get num
"9903"

5. hash类型

#1.应用场景
    存储部分变更的数据,如用户信息等。
    最接近mysql表结构的一种类型

#2.存储数据
hmset stu id 101 name zs age 20 gender m

#3.取数据
hmget stu id name age gender

#4.从mysql导出数据
    #4.1设置安全导出路径
    vim /etc/my.cnf
        secure-file-priv=/tmp
    #4.2从mysql得到redis语句
    select concat("hmset city_",id," id ",id," name ",name," countrycode ",countrycode," district ",district," population ",population) from city limit 10 into outfile '/tmp/hmset.txt'
    #4.3导入redis
    cat /tmp/hmset.txt |redis-cli
#1.设置字典
127.0.0.1:6379> hmset stu id 101 name zhangsan age 20 gender m
OK

#2.获取字典的错误方法
127.0.0.1:6379> hmget stu
(error) ERR wrong number of arguments for 'hmget' command
127.0.0.1:6379> hmget stu *
1) (nil)

#2.获取字典类型的正确方法
127.0.0.1:6379> hmget stu id
1) "101"
127.0.0.1:6379> hmget stu id name age gender
1) "101"
2) "zhangsan"
3) "20"
4) "m"
#redis如何获取mysql的数据

#1.mysql设置导出安全路径
    vim /etc/my.cnf
        secure-file-priv=/tmp
#2.从mysql中获取redis字典类型语句并导出
    select concat("hmset city_",id," id ",id," name ",name," countrycode ",countrycode," district ",district," population ",population) from city limit 10 into outfile '/tmp/hmset.txt'
#3.将数据导入redis
    cat /tmp/hmset.txt |redis-cli
#151 演示如下

#1.修改配置文件
[root@151 ~]# vim /etc/my.cnf
[root@151 ~]# cat /etc/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/application/mysql/data
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.log
log_bin=/data/mysql/mysql-bin
binlog_format=row
skip-name-resolve
server_id=51
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
relay_log_purge=0
secure-file-priv=/tmp

[client]
socket=/tmp/mysql.sock

#2.重启mysql
[root@151 ~]# systemctl stop mysqld
[root@151 ~]# netstat -tunlp |grep 330
[root@151 ~]# systemctl start mysqld
[root@151 ~]# netstat -tunlp |grep 330
tcp6       0      0 :::3306                 :::*                    LISTEN      73175/mysqld

#3.登录查看是否有world数据库
[root@151 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20-log Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cp                 |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

mysql>quit

#4.准备world数据库sql文件
[root@151 ~]# ls
anaconda-ks.cfg
[root@151 ~]# ls
anaconda-ks.cfg  world.sql

#5.导入world数据库
mysql> source /root/world.sql
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cp                 |
| mysql              |
| performance_schema |
| school             |
| sys                |
| world              |
+--------------------+
7 rows in set (0.00 sec)

#6.查看city表结构
mysql> use world
Database changed
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)
mysql> desc city;
+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| ID          | int(11)  | NO   | PRI | NULL    | auto_increment |
| Name        | char(35) | NO   |     |         |                |
| CountryCode | char(3)  | NO   | MUL |         |                |
| District    | char(20) | NO   |     |         |                |
| Population  | int(11)  | NO   |     | 0       |                |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

#7.查看导出内容
mysql> select concat("hmset city_",id," id ",id," name ",name," countrycode ",countrycode," district ",district," population ",population) from city limit 10;
+------------------------------------------------------------------------------------------------------------------------------+
| concat("hmset city_",id," id ",id," name ",name," countrycode ",countrycode," district ",district," population ",population) |
+------------------------------------------------------------------------------------------------------------------------------+
| hmset city_1 id 1 name Kabul countrycode AFG district Kabol population 1780000                                               |
| hmset city_2 id 2 name Qandahar countrycode AFG district Qandahar population 237500                                          |
| hmset city_3 id 3 name Herat countrycode AFG district Herat population 186800                                                |
| hmset city_4 id 4 name Mazar-e-Sharif countrycode AFG district Balkh population 127800                                       |
| hmset city_5 id 5 name Amsterdam countrycode NLD district Noord-Holland population 731200                                    |
| hmset city_6 id 6 name Rotterdam countrycode NLD district Zuid-Holland population 593321                                     |
| hmset city_7 id 7 name Haag countrycode NLD district Zuid-Holland population 440900                                          |
| hmset city_8 id 8 name Utrecht countrycode NLD district Utrecht population 234323                                            |
| hmset city_9 id 9 name Eindhoven countrycode NLD district Noord-Brabant population 201843                                    |
| hmset city_10 id 10 name Tilburg countrycode NLD district Noord-Brabant population 193238                                    |
+------------------------------------------------------------------------------------------------------------------------------+
10 rows in set (0.00 sec)

#8.导出到外部文件
mysql> select concat("hmset city_",id," id ",id," name ",name," countrycode ",countrycode," district ",district," population ",population) from city limit 10 into outfile '/tmp/hmset.txt';
Query OK, 10 rows affected (0.00 sec)

mysql> quit
Bye
[root@151 ~]# cat /tmp/hmset.txt
hmset city_1 id 1 name Kabul countrycode AFG district Kabol population 1780000
hmset city_2 id 2 name Qandahar countrycode AFG district Qandahar population 237500
hmset city_3 id 3 name Herat countrycode AFG district Herat population 186800
hmset city_4 id 4 name Mazar-e-Sharif countrycode AFG district Balkh population 127800
hmset city_5 id 5 name Amsterdam countrycode NLD district Noord-Holland population 731200
hmset city_6 id 6 name Rotterdam countrycode NLD district Zuid-Holland population 593321
hmset city_7 id 7 name Haag countrycode NLD district Zuid-Holland population 440900
hmset city_8 id 8 name Utrecht countrycode NLD district Utrecht population 234323
hmset city_9 id 9 name Eindhoven countrycode NLD district Noord-Brabant population 201843
hmset city_10 id 10 name Tilburg countrycode NLD district Noord-Brabant population 193238

#9.将内容导入redis
[root@151 src]# cat /tmp/hmset.txt |redis-cli
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
[root@151 src]# cat /tmp/hmset.txt |redis-cli -a 123456
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK

#10.进入rdis,验证
[root@151 src]# redis-cli -a 123456
127.0.0.1:6379> keys *
 1) "city_1"
 2) "city_2"
 3) "city_3"
 4) "city_4"
 5) "city_5"
 6) "city_8"
 7) "city_9"
 8) "city_10"
 9) "city_6"
10) "a"
11) "city_7"
127.0.0.1:6379> hmget city_1 id
1) "1"
127.0.0.1:6379> hmget city_1 id name countrycode population
1) "1"
2) "Kabul"
3) "AFG"
4) "1780000"

#11.查看类型
127.0.0.1:6379> type city_1
hash

#mysql数据导入redis成功!!!

6. list类型

#1.应用场景
    消息队列系统
    微信朋友圈、网站最新动态、最新歌曲

#2.模拟微信朋友圈
    lpush wechat "today is 1"
    lpush wechat "today is 2"
    lpush wechat "today is 3"
    lpush wechat "today is 4"
    lpush wechat "today is 5"

#3.查看微信朋友圈
    lrange wechat 0 0   #查看最新的一条
    lrange wechat 0 1   #2条
    lrange wechat 0 2   #3条
    lrange wechat 0 -1  #所有
    lrange wechat 0 -2  #除了最后一条的所有
#1.写入列表数据
127.0.0.1:6379> lpush wechat "today is 1"
(integer) 1
127.0.0.1:6379> lpush wechat "today is 2"
(integer) 2
127.0.0.1:6379> lpush wechat "today is 3"
(integer) 3
127.0.0.1:6379> lpush wechat "today is 4"
(integer) 4
127.0.0.1:6379> lpush wechat "today is 5"
(integer) 5

#2.查看类型
127.0.0.1:6379> type wechat
list

#3.查看列表数据
127.0.0.1:6379> lrange wechat 0 0
1) "today is 5"
127.0.0.1:6379> lrange wechat 0 -1
1) "today is 5"
2) "today is 4"
3) "today is 3"
4) "today is 2"
5) "today is 1"
127.0.0.1:6379>
127.0.0.1:6379> lrange wechat 0 2
1) "today is 5"
2) "today is 4"
3) "today is 3"

7. 集合

#1.应用场景
    比如微博中,可以将一个用户所有的关注人存于一个集合中

#2.创建集合
    sadd cp a b c d e
    sadd wy e f g h i

#3.查看集合
    smembers cp
    smembers wy

#4.并集
    sunion cp wy

#5.交集
    sinter cp wy

#6.差集
    sdiff cp wy
#1.创建集合
127.0.0.1:6379> sadd chupeng a b c d e
(integer) 5
127.0.0.1:6379> sadd wangyao e f g h i
(integer) 5
127.0.0.1:6379> type chupeng
set
127.0.0.1:6379> type wangyao
set

#2.查看集合内容
127.0.0.1:6379> smembers chupeng
1) "d"
2) "c"
3) "b"
4) "a"
5) "e"
127.0.0.1:6379> smembers wangyao
1) "h"
2) "g"
3) "f"
4) "e"
5) "i"

#3.查看集合的并集、差集、交集
#并集
127.0.0.1:6379> sunion chupeng wangyao
1) "e"
2) "b"
3) "a"
4) "f"
5) "h"
6) "d"
7) "c"
8) "g"
9) "i"

#交集
127.0.0.1:6379> sinter chupeng wangyao
1) "e"

#差集
127.0.0.1:6379> sdiff chupeng wangyao
1) "c"
2) "d"
3) "a"
4) "b"

8. 有序集合

#1.应用场景
    排行榜应用,取top n操作

#2.模拟歌曲排行榜
    zadd topn 0 a 0 b 0 c 0 d

#3.设置每个键的值
    zincrby topn 100 a
    zincrby topn 90  b
    zincrby topn 50  c
    zincrby topn 1   d

#4.查看排行榜前三(不带数值)
    zrevrange topn 0 2

#5.查看带数值的前三
    zrevrange topn 0 2 withscores

#6.查看所有
    zrevrange topn 0 -1 withscores
#1.创建有序集合
127.0.0.1:6379> zadd topn 0 a 0 b 0 c 0 d
(integer) 4
127.0.0.1:6379> type topn
zset

#2.查看有序集合的键
127.0.0.1:6379> ZREVRANGE topn 0 -1
1) "d"
2) "c"
3) "b"
4) "a"

#3.查看有序集合的键及其值
127.0.0.1:6379> zrevrange topn 0 -1 withscores
1) "d"
2) "0"
3) "c"
4) "0"
5) "b"
6) "0"
7) "a"

#4.给每个键设置值
127.0.0.1:6379> zincrby topn 100 a
"100"
127.0.0.1:6379> zrevrange topn 0 -1 withscores
1) "a"
2) "100"
3) "d"
4) "0"
5) "c"
6) "0"
7) "b"
8) "0"
127.0.0.1:6379> zincrby topn 200 b
"200"
127.0.0.1:6379> zincrby topn 300 c
"300"
127.0.0.1:6379> zincrby topn 400 d
"400"
127.0.0.1:6379> zrevrange topn 0 -1 withscores
1) "d"
2) "400"
3) "c"
4) "300"
5) "b"
6) "200"
7) "a"
8) "100"

最后更新: 2022-02-20 08:44:07