安装 Redis
1、下载源码,解压缩后编译源码。#
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar xzf redis-5.0.5.tar.gz
cd redis-5.0.5
make
make install
2、编译完成后,在Src目录下,有四个可执行文件redis-server、redis-benchmark、redis-cli和redis.conf。然后拷贝到一个目录下。#
复制代码
1
2
3 mkdir -p /usr/redis
4
5
6 cp redis-server /usr/redis
7
8 cp redis-benchmark /usr/redis
9
10 cp redis-cli /usr/redis
11
12 cp redis.conf /usr/redis
13
14
15 cd /usr/redis
查看进程
[hadoop@hadoop000 src]$ ps -ef | grep redis
root 11195 10944 0 11:16 pts/0 00:00:00 ./redis-server *:6379
hadoop 11415 11216 0 11:28 pts/1 00:00:00 grep --color=auto redis
杀掉进程
[root@hadoop000 ~]
初步尝试
启动服务端
[root@hadoop000 src]
启动客户端
[hadoop@hadoop000 src]$ ./redis-cli
退出客户端
127.0.0.1:6379> quit
3、 修改配置文件
vi redis.conf
<改动地方>
daemonize yes
logfile "/home/hadoop/app/tmp/redis/redis.log"
标准启动 .conf
[hadoop@hadoop000 redis-5.0.5]$ ./src/redis-server redis.conf
服务端启动 + 服务器地址 +服务器端口[6379]
[hadoop@hadoop000 redis-5.0.5]$ ./src/redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> set name pk
OK
127.0.0.1:6379> get name
"pk"
redis 数据库概念
1、默认最多支持16个数据库 (可以通过redis.conf 进行修改) 2、从 redis-cli 进来,默认选择的是0号数据库;可以通过 select id 来切换数据库 3、各个数据库之前是相互隔离,不想通的
切换数据库
hadoop000:6379> select 2
OK
hadoop000:6379[2]>
查询符合规则的key ====> + 表达式
hadoop000:6379[2]> keys user_?
1) "user_4"
2) "user_2"
3) "user_3"
判断某个key 是否存在
hadoop000:6379[2]> EXISTS user
(integer) 0
hadoop000:6379[2]> EXISTS user_4
(integer) 1
删除某个 key
hadoop000:6379[2]> del user_6
(integer) 0
hadoop000:6379[2]> del user_2
(integer) 1
从外部批量 查询与 删除keys
[hadoop@hadoop000 redis-5.0.5]$ ./src/redis-cli keys '*'
1) "su_2"
2) "su_4"
3) "su_3"
4) "su_1"
[hadoop@hadoop000 redis-5.0.5]$ ./src/redis-cli del 'su_2'
获取某个key 的类型
hadoop000:6379> type su_3
string
String 类型的操作
同时设置多个 key value(string)
hadoop000:6379> mset user_b bb user_c cc user_d dd user_e ee
OK
hadoop000:6379> keys user_
(empty list or set)
hadoop000:6379> keys user_*
1) "user_e"
2) "user_a"
3) "user_c"
4) "user_d"
5) "user_b"
同时获取多个key 对应的value值
hadoop000:6379> mget user_a user_c user_g
1) "aa"
2) "cc"
3) (nil)
当key存在时则不发生动作,当key不存在时新创建一个
hadoop000:6379> setnx user_a a1
(integer) 0
hadoop000:6379> setnx user_a1 a1
(integer) 1
hadoop000:6379> keys *
1) "user_c"
2) "user_b"
3) "user_a1"
4) "user_a"
5) "user_e"
6) "su_4"
7) "user_d"
8) "su_3"
9) "su_1"
—字符串拼接
hadoop000:6379> mget user_a user_b user_c
1) "aa"
2) "bb"
3) "cc"
hadoop000:6379> append user_a '-append'
(integer) 9
hadoop000:6379> get user_a
"aa-append"
—获取字符串长度
hadoop000:6379> get user_a
"aa-append"
hadoop000:6379> STRLEN user_a
(integer) 9
—获取字符串某个区间的内容
hadoop000:6379> GETRANGE user_a 0 3
"aa-a"
hadoop000:6379> GETRANGE user_a 0 5
"aa-app"
当为数值型时候
----INCR 递增1
hadoop000:6379> set money 100
OK
hadoop000:6379> get money
"100"
hadoop000:6379> INCR money
(integer) 101
hadoop000:6379> INCR money
(integer) 102
hadoop000:6379> INCR money
(integer) 103
-----数值递减
hadoop000:6379> DECR su_1
(integer) 221
hadoop000:6379> DECR su_1
(integer) 220
hadoop000:6379> DECR su_1
(integer) 219
----设定某个值 批量递增递减
hadoop000:6379> DECRBY su_1 200
(integer) 19
hadoop000:6379> DECRBY su_1 200
(integer) -181
hadoop000:6379> DECRBY su_1 200
(integer) -381
hadoop000:6379> INCRby su_2 500
(integer) 500
hadoop000:6379> INCRby su_2 500
(integer) 1000
hadoop000:6379> INCRby su_2 500
(integer) 1500
hadoop000:6379> INCRby su_aaaaa 500
(integer) 500
hadoop000:6379> INCRby su_aaaaa 500
(integer) 1000
hadoop000:6379> INCRby su_aaaaa 500
(integer) 1500
hadoop000:6379> INCRby su_aaaaa 500
(integer) 2000
---按照某个值递减float 类型的
hadoop000:6379> INCRBYFLOAT su_1 11.6
"-369.39999999999999999"
hadoop000:6379> INCRBYFLOAT su_1 11
"-358.39999999999999999"
hadoop000:6379> INCRBYFLOAT su_1 -11.2
"-369.60000000000000001"
---递增类型不为数值时,则报错
hadoop000:6379> INCR user_c
(error) ERR value is not an integer or out of range
=========================》》》》
LIST 类型的操作
hadoop000:6379> LPUSH list_1 a b c d
(integer) 4
hadoop000:6379> TYPE list_1
list
hadoop000:6379> rpush 1 2 3 6 5 4 9 8 7
(integer) 8
hadoop000:6379> lrange list_1 0 -1
获取某个索引下的值
hadoop000:6379> lindex list_1 0
"d"
hadoop000:6379> lset list_1 3 33333333333333
OK
hadoop000:6379> LRANGE list_1 0 -1
1) "d"
2) "c"
3) "b"
4) "33333333333333"
在某个位置插入值
hadoop000:6379> LINSERT list_1 before d ddddddddddddddddddddd
(integer) 5
hadoop000:6379> lrange list_1 0 -1
1) "ddddddddddddddddddddd"
2) "d"
3) "c"
4) "b"
5) "33333333333333"
###查看长度
hadoop000:6379> llen list_1
(integer) 4
=========================》》》》
Set 类型处理
添加元素
hadoop000:6379> SADD set_1 a c d p p e g t y b n m k j r [ 9 5 6 3 2 4 1 5
(integer) 18
查看包含的元素
hadoop000:6379> SMEMBERS set_1
1) "f"
2) "d"
3) "y"
4) "["
5) "a"
6) "m"
7) "g"
删除某个元素
hadoop000:6379> SREM set_1 1 2 3 4 5 6 7 8 9
(integer) 7
hadoop000:6379> SISMEMBER set_1 a
(integer) 1
hadoop000:6379> SCARD set_1
(integer) 16
随机 去N个个数
hadoop000:6379> SRANDMEMBER set_1 4
1) "j"
2) "b"
3) "k"
4) "a"
|