Redis-数据类型-Hash

文章目录

1、查看redis是否启动

bash 复制代码
[root@localhost ~]# ps -ef | grep redis
root       4270      1  0 05:51 ?        00:00:27 /usr/local/redis/bin/redis-server *:6379
root       5458   5407  0 07:33 pts/1    00:00:00 /usr/local/redis/bin/redis-cli -p 6379
root       7851   6858  0 10:59 pts/0    00:00:00 grep --color=auto redis
[root@localhost ~]# 

2、通过客户端连接redis

bash 复制代码
[root@localhost ~]# /usr/local/redis/bin/redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

3、切换到db3数据库

bash 复制代码
127.0.0.1:6379> select 3
OK
127.0.0.1:6379[3]> 

4、插入新数据返回1

bash 复制代码
127.0.0.1:6379[3]> hset hm01 s01 jim
(integer) 1
127.0.0.1:6379[3]> keys *
1) "hm01"
127.0.0.1:6379[3]> hset hm01 s02 tom
(integer) 1
127.0.0.1:6379[3]> keys *
1) "hm01"
127.0.0.1:6379[3]> 

5、获取指定哈希(hash)对象的所有字段(field)名

HKEYS key 命令返回存储在指定哈希对象中的所有字段名。

bash 复制代码
127.0.0.1:6379[3]> keys *
1) "hm01"
127.0.0.1:6379[3]> hkeys hm01
1) "s01"
2) "s02"
127.0.0.1:6379[3]> 

6、获取存储在指定哈希(hash)对象中的所有字段(field)的值

HVALS key 命令返回与指定哈希对象关联的所有字段的值。

bash 复制代码
127.0.0.1:6379[3]> keys *
1) "hm01"
127.0.0.1:6379[3]> hkeys hm01
1) "s01"
2) "s02"
127.0.0.1:6379[3]> hvals hm01
1) "jim"
2) "tom"
127.0.0.1:6379[3]> 

7、获取存储在哈希(hash)数据类型中指定字段的值

bash 复制代码
127.0.0.1:6379[3]> hget hm01 s01
"jim"
127.0.0.1:6379[3]> 

8、获取存储在哈希(Hash)数据结构中指定键(key)的所有字段(field)和值(value)

bash 复制代码
127.0.0.1:6379[3]> hgetall hm01
1) "s01"
2) "jim"
3) "s02"
4) "tom"
127.0.0.1:6379[3]> 

9、获取存储在哈希(Hash)数据结构中指定键(key)的字段数量

bash 复制代码
127.0.0.1:6379[3]> hlen hm01
(integer) 2
127.0.0.1:6379[3]> 

10、检查哈希(Hash)数据结构中指定的字段(field)是否存在

bash 复制代码
127.0.0.1:6379[3]> hkeys hm01
1) "s01"
2) "s02"
127.0.0.1:6379[3]> hexists hm01 s03
(integer) 0
127.0.0.1:6379[3]> hexists hm01 s02
(integer) 1
127.0.0.1:6379[3]> 

11、删除哈希表(Hash)中的一个或多个指定字段

bash 复制代码
127.0.0.1:6379[3]> hkeys hm01
1) "s01"
2) "s02"
127.0.0.1:6379[3]> hdel hm01 s02 s03 s04 s05
(integer) 1
127.0.0.1:6379[3]> hgetall hm01
1) "s01"
2) "jim"
127.0.0.1:6379[3]> 

12、对哈希表中的字段值进行增加操作

bash 复制代码
127.0.0.1:6379[3]> hset hm01 age 25
(integer) 1
127.0.0.1:6379[3]> hgetall hm01
1) "s01"
2) "jim"
3) "age"
4) "25"
127.0.0.1:6379[3]> hincrby hm01 age 5
(integer) 30
127.0.0.1:6379[3]> hgetall hm01
1) "s01"
2) "jim"
3) "age"
4) "30"
127.0.0.1:6379[3]> 

13、返回哈希表中一个或多个给定字段的值(hash muli get)

  1. h 代表哈希(Hash)数据结构
  2. mget 是"multi get"的缩写
bash 复制代码
127.0.0.1:6379[3]> keys *
1) "hm01"
127.0.0.1:6379[3]> hkeys hm01
1) "s01"
2) "age"
127.0.0.1:6379[3]> hmget hm01 s01 age
1) "jim"
2) "30"
127.0.0.1:6379[3]> 

14、设置哈希表中多个字段和值

hmset(现已被hset命令的多个字段-值对版本所取代)是一个用于设置哈希表中多个字段和值的命令。

bash 复制代码
127.0.0.1:6379[3]> hmset hm01 s02 tom age 25 address USA
OK
127.0.0.1:6379[3]> hgetall hm01
1) "s01"
2) "jim"
3) "age"
4) "25"
5) "s02"
6) "tom"
7) "address"
8) "USA"
127.0.0.1:6379[3]> 


相关推荐
昌原的儿子LEO1 小时前
Linux IO多路复用与SQLite数据库核心知识点总结
linux·数据库·oracle
坐吃山猪2 小时前
【多线程】Semaphore使用
java·数据库·oracle·多线程
姜太小白3 小时前
【Oracle】记排查sh脚本调用SQL执行卡顿的排查总结
数据库·sql·oracle
人生苦短1285 小时前
Oracle RAC 日常管理+排错命令大全
数据库·oracle
JavaPub-rodert6 小时前
Redis 和 MySQL 如何保证数据一致性?从业务方案到底层原理完整讲解
数据库·redis·mysql
额额额对了8 小时前
Linux 数据库开发实战:使用 C 语言与 SQLite3 构建轻量级终端词典
linux·数据库·oracle
影视飓风TIM8 小时前
C++哈希表深度剖析:冲突处理、rehash、迭代器与unordered容器封装
c++·哈希算法·散列表
qq_246839758 小时前
postgresql xxhash64 哈希函数实现
数据库·postgresql·哈希算法
养生技术人10 小时前
Oracle OCP认证考试题目详解082系列第17题
运维·数据库·sql·oracle·ocp
xx~t10 小时前
嵌入式——Linux软件编程——数据库
linux·c语言·数据库·oracle·sqlite