redis客户端

3、Redis客户端
3.1 Redis自带的客户端

带密码进入客户端

[root@qianfeng01 redis-4.0.14]# src/redis-cli -h 192.168.10.101 -p 6379 -a root
Warning: Using a password with '-a' option on the command line interface may not be safe.
192.168.10.101:6379> keys *
(empty list or set)

带密码并选择数据库进入命令行

[root@qianfeng01 redis-4.0.14]# src/redis-cli -h 192.168.10.101 -p 6379 -a root -n 1

切换数据库

192.168.10.101:6379> select 2
OK
192.168.10.101:6379[2]>
3.3 桌面客户端

下载:Release 0.8.8 · RedisInsight/RedisDesktopManager · GitHub

安装:下载完成后双击打开,选择安装路径即可

连接Redis服务器

redis客户端_第1张图片

3.5 Redis命令行操作
3.5.1 String类型
Set
129.0.0.1Redis.assets6379> set str1 angelababy
OK
​
Get
129.0.0.1Redis.assets6379> get str1
" angelababy"
​
Del
129.0.0.1Redis.assets6379> del str1
(integer) 1
​
自增
必须value为数字类型
129.0.0.1Redis.assets6379> set s1 1
OK
129.0.0.1Redis.assets6379> incr s1
(integer) 2
129.0.0.1Redis.assets6379> incr s1
(integer) 3
129.0.0.1Redis.assets6379> incr s1
(integer) 4
129.0.0.1Redis.assets6379> incr s1
(integer) 5
​
自减
129.0.0.1Redis.assets6379> decr s1
(integer) 4
129.0.0.1Redis.assets6379> decr s1
(integer) 3
129.0.0.1Redis.assets6379> decr s1
(integer) 2
129.0.0.1Redis.assets6379> decr s1
​
自增自减指定数值
129.0.0.1Redis.assets6379> incrby s1 3
(integer) 4
129.0.0.1Redis.assets6379> decrby s1 3
(integer) 1
​
设置或者获取多个key/value
129.0.0.1Redis.assets6379> mset s1 v1 s2 v2 
OK
129.0.0.1Redis.assets6379> mget s1 s2
1) "v1"
2) "v2"
3.5.2 Hash命令
hash叫散列类型,它提供了字段和字段值的映射。字段值只能是字符串类型,不支持散列类型、集合类型等其它类型。如下:

redis客户端_第2张图片

  • hset 在redis中,命令是不区分大小写,但是key区分大小写

129.0.0.1Redis.assets6379> hset m1 k1 v1
(integer) 1
129.0.0.1Redis.assets6379> HSET m1 k1 v1
(integer) 0
  • hget

129.0.0.1Redis.assets6379> hget m1 k1
"v1"
129.0.0.1Redis.assets6379> hget m1 
(error) ERR wrong number of arguments for 'hget' command
129.0.0.1Redis.assets6379>
  • hdel

129.0.0.1Redis.assets6379> hdel m1
(error) ERR wrong number of arguments for 'hdel' command
129.0.0.1Redis.assets6379> hdel m1 k1
(integer) 1
  • 批量操作

129.0.0.1Redis.assets6379> hmset m1 k1 v1 k2 v2
OK
129.0.0.1Redis.assets6379> hmget m1 k1 k2
1) "v1"
2) "v2"
129.0.0.1Redis.assets6379> hmget m1 k1 k2 k3
1) "v1"
2) "v2"
3) (nil)
  • 增加数值

129.0.0.1Redis.assets6379> hincrby m2 k1
(error) ERR wrong number of arguments for 'hincrby' command
129.0.0.1Redis.assets6379> hincrby m2 k1 2
(integer) 3
  • 判断字段是否存在 HEXISTS key field

129.0.0.1Redis.assets6379> hexists user age     查看user中是否有age字段
(integer) 1
129.0.0.1Redis.assets6379> hexists user name    查看user中是否有name字段
(integer) 0
3.5.3 List列表
192.168.10.101:6379> LPUSH l1 1 2 6 5 9 8
(integer) 6
192.168.10.101:6379> LRANGE l1 0 -1
1) "8"
2) "9"
3) "5"
4) "6"
5) "2"
6) "1"
192.168.10.101:6379> LRANGE l1 0 2
1) "8"
2) "9"
3) "5"
192.168.10.101:6379> LRANGE l1 0 1
1) "8"
2) "9"
192.168.10.101:6379> LPOP l1
"8"
192.168.10.101:6379> LRANGE l1 0 1
1) "9"
2) "5"
192.168.10.101:6379> LPOP l1
"9"
192.168.10.101:6379> LPOP l1
"5"
192.168.10.101:6379> LPOP l1
"6"
192.168.10.101:6379> LRANGE l1 0 -1
1) "2"
2) "1"

其它列表List、集合Set和有序集合ZSet类型的命令我们不再赘述,大家有时间再学习。

3.6 Java客户端连接
3.6.1 JedisAPI连接
Jedis就是Redis的客户端连接对象,也就是创建的实例连接的Connection对象,类似于MySQL的JDBC-Driver驱动。
3.7.2 单机连接

创建Maven工程,导入Pom依赖



  redis.clients
  jedis
  3.0.0

代码实现

package com.qianfeng.sparkstreaming
import redis.clients.jedis.Jedis

/**
 * redis测试
 */
object Demo01_RedisTest {
  def main(args: Array[String]): Unit = {
    // 创建Jedis连接实例对象// 创建Jedis连接实例对象
    val jedis = new Jedis("192.168.10.101", 6379)
    // 密码验证
    jedis.auth("root")
    jedis.select(index=2) //选择数据库
    // 测试连接
    println(jedis.ping())
    //字符串插入
    jedis.set("k1","vv")
    println(jedis.get( "k1"))
        
    //数组
	jedis.lpush( key = "l1" ,strings = "1","3","8", "6")
	println(jedis.lrange( key = "l1",start = 0,stop = 2))//查询top3
        
    //关闭jedis对象
    jedis.close()
  }
}

Guff_hys_python数据结构,大数据开发学习,python实训项目-CSDN博客

你可能感兴趣的:(oracle,数据库,spark,后端,大数据,分布式,开发语言)