Operation command of hash type in redis

Crown 2020-11-10 15:10:44
operation command hash type redis


One 、Redis Hash (Hash)

Redis hash It's a string Type of field( Field ) and value( value ) Mapping table ,hash Ideal for storing objects .

Redis Each of them hash Can be stored 232 - 1 Key value pair (40 More than ).

image.png

A simple example :

127.0.0.1:6379> HMSET runoobkey name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000 OK 127.0.0.1:6379> HGETALL runoobkey 1) "name" 2) "redis tutorial" 3) "description" 4) "redis basic commands for caching" 5) "likes" 6) "20" 7) "visitors" 8) "23000"

Two 、Hash Type of operation command

1、hset

hset: Set the value . The format is :hset hash Of key Term key Item value

The operation is as follows :
image.png

java Sample code :

import redis.clients.jedis.Jedis;
/**
* redis For in the hash Type of : hset The operation sample
* hset: Set the value . The format is :hset hash Of key Term key Item value
*/
public class Hash_hset_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
/**
* Example 1: hset hash Of key Term key Item value
*/
jedis.hset("myhash","id","3");
jedis.hset("myhash","name","xiaohong");
jedis.hset("myhash","age","13");
}
}

2、hmset

hmset: Set multiple pairs of values at the same time . The format is :hmset hash Of key Term key Item value .( Term key The values of the and terms can be many pairs of )

The operation is as follows :
image.png

java Sample code :

import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;
/**
* redis For in the hash Type of : hmset The operation sample
* hmset: Set multiple pairs of values at the same time . The format is :hmset hash Of key Term key Item value .( Term key The values of the and terms can be many pairs of )
*/
public class Hash_hmset_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
/**
* Example 1: hmset hash Of key Term key Item value .( Term key The values of the and terms can be many pairs of )
*/
Map<String,String> map = new HashMap<>();
map.put("id","1");
map.put("name","dongdong");
map.put("age","3");
jedis.hmset("myhash",map);
}
}

3、hget

hget: Get value . The format is :hget hash Of key Term key
The operation is as follows :
image.png

java Sample code :

import redis.clients.jedis.Jedis;
/**
* redis For in the hash Type of : hget The operation sample
* hget: Get value . The format is :hget hash Of key Term key
*/
public class Hash_hget_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
/**
* Example 1: hget hash Of key Term key
*/
String id = jedis.hget("myhash", "id");
String name = jedis.hget("myhash", "name");
String age = jedis.hget("myhash", "age");
System.out.println(String.format("id = %s, name = %s, age = %s",id,name,age));
}
}

4、hmget

hmget: Get multiple pairs of values at the same time . The format is :hmget hash Of key Term key.( Term key There can be multiple )

The operation is as follows :
image.png

java Sample code :

import redis.clients.jedis.Jedis;
import java.util.List;
/**
* redis For in the hash Type of : hmget The operation sample
* hmget: Get multiple pairs of values at the same time . The format is :hmget hash Of key Term key.( Term key There can be multiple )
*/
public class Hash_hmget_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
/**
* Example 1: hmget hash Of key Term key.( Term key There can be multiple )
*/
List<String> hmget = jedis.hmget("myhash", "id", "name", "age");
System.out.println("hmget = " + hmget);
}
}

5、hgetall

hgetall: Get the key Under all the values . The format is :hgetall hash Of key

The operation is as follows :
image.png

java Sample code :

import redis.clients.jedis.Jedis;
import java.util.Map;
/**
* redis For in the hash Type of : hgetall The operation sample
* hgetall: Get the key Under all the values . The format is :hgetall hash Of key
*/
public class Hash_hgetall_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
/**
* Example 1: hgetall hash Of key
*/
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
}
}

6、hdel

hdel: Delete an item . The format is :hdel hash Of key Term key

The operation is as follows :
image.png

java Sample code :

import redis.clients.jedis.Jedis;
import java.util.Map;
/**
* redis For in the hash Type of : hdel The operation sample
* hdel: Delete an item . The format is :hdel hash Of key Term key
*/
public class Hash_hdel_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
/**
* Example 1: hdel hash Of key Term key
*/
Long hdel = jedis.hdel("myhash", "age");
System.out.println("hdel = " + hdel);
Map<String, String> myhash1 = jedis.hgetAll("myhash");
System.out.println("myhash1 = " + myhash1);
}
}

7、hlen

hlen: obtain key The number of key value pairs inside . The format is :hlen hash Of key
The operation is as follows :
image.png

java Sample code

import redis.clients.jedis.Jedis;
import java.util.Map;
/**
* redis For in the hash Type of : hlen The operation sample
* hlen: obtain key The number of key value pairs inside . The format is :hlen hash Of key
*/
public class Hash_hlen_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
/**
* Example 1: hlen hash Of key
*/
Long hlen = jedis.hlen("myhash");
System.out.println("hlen = " + hlen);
}
}

8、hexists

hexists: Determine whether the key value exists . The format is :hexists hash Of key Term key

The operation is as follows :
image.png

java Sample code :

import redis.clients.jedis.Jedis;
import java.util.Map;
/**
* redis For in the hash Type of : hexists The operation sample
* hexists: Determine whether the key value exists . The format is :hexists hash Of key Term key
*/
public class Hash_hexists_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
/**
* Example 1: hexists hash Of key Term key
*/
Boolean hexists = jedis.hexists("myhash", "id");
System.out.println("hexists = " + hexists);
}
}

9、hkeys

hkeys: Get all item Of key, The format is :hkeys hash Of key

The operation is as follows :

image.png

java Sample code :

import redis.clients.jedis.Jedis;
import java.util.Map;
import java.util.Set;
/**
* redis For in the hash Type of : hexists The operation sample
* hkeys: Get all item Of key, The format is :hkeys hash Of key
*/
public class Hash_hkeys_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
/**
* Example 1: hkeys hash Of key
*/
Set<String> hkeys = jedis.hkeys("myhash");
System.out.println("hkeys = " + hkeys);
}
}

10、hvals

hvals: Get all item Value , The format is :hvals hash Of key

The operation is as follows :
image.png

java Sample code :

import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.Map;
/**
* redis For in the hash Type of : hexists The operation sample
* hvals: Get all item Value , The format is :hvals hash Of key
*/
public class Hash_hvals_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
/**
* Example 1: hvals hash Of key
*/
List<String> hvals = jedis.hvals("myhash");
System.out.println("hvals = " + hvals);
}
}

Related articles :
Redis Hash (Hash) Relevant command
Java in Redis Yes Hash Type of operation command

版权声明
本文为[Crown]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云