实验四 NoMysql和关系数据库操作 Redis篇(3)
实验环境:
1、操作系统:Windows10;
2、Hadoop版本:3.2.2;
5、Redis版本:3.2.0;
6、MongoDB版本:4.4.0;
7、JDK版本:1.8或以上版本;
8、Java IDE:idea;
(三)Redis数据库操作
Student键值对如下:
zhangsan:{ English: 69 Math: 86 Computer: 77 } lisi:{ English: 55 Math: 100 Computer: 88 } |
1. 根据上面给出的键值对,完成如下操作:
(1)用Redis的哈希结构设计出学生表Student(键值可以用student.zhangsan和student.lisi来表示两个键值属于同一个表);
HMSET Student.zhangsan English 69 Math 86 Computer 77 HMSET Student.lisi English 55 Math 100 Computer 88
(2)用hgetall命令分别输出zhangsan和lisi的成绩信息;
hgetall Student.zhangsan
hgetall Student.lisi
(3)用hget命令查询zhangsan的Computer成绩;
HGET Student.zhangsan Computer
(4)修改lisi的Math成绩,改为95。
hset Student.list Math 95
hgetall Student.list
2.根据上面已经设计出的学生表Student,用Redis的JAVA客户端编程(jedis),实现如下操作:
(1)添加数据:English:45 Math:89 Computer:100
该数据对应的键值对形式如下:
scofield:{ English: 45 Math: 89 Computer: 100 } |
(2)获取scofield的English成绩信息
Jedis jedis = new Jedis("localhost"); Mapmap = new HashMap<>(); map.put("English","45"); map.put("Math","89"); map.put("Computer","100"); String string= jedis.hmset("Student.scofield",map); System.out.println(string); Map map2=jedis.hgetAll("Student.scofield"); System.out.println(map2.toString()); String string2=jedis.hget("Student.scofield","English"); System.out.println(string2);