哈尔滨理工大学
软件与微电子学院
实 验 报 告
(2020-2021第二学期)
课程名称: | NoSQL数据库技术(双语) |
班 级: | 软件18- 1 班 |
学 号: | 1814010130 |
姓 名: | 张立辉 |
哈尔滨理工大学软件与微电子学院
实验名称: | 实验一 键值数据库的增删改查 | 专 业 | 软件工程 | |||
---|---|---|---|---|---|---|
姓 名 | 张立辉 | 学 号 | 1814010130 | 班 级 | 软件18-1 |
一、实验目的:
1.理解键的含义。
2.掌握键的设计方法。
3.掌握键值对的增删改查方法。
二、实验内容:
分析项目中数据存储的需求,给出键的设计方案,并应用Java客户端实现增删改查。按照下列要求,设计并实现一种在线购物商城的用户信息管理模块。其中,用户信息表包括:用户ID(userid)、用户名(username)、密码(password)、生日(birthday)、图像(userico)。
(1)任务一:定义并实现如下接口
(2)任务二:以文件形式存储用户图像(选择),并实现如下形式的java方法。
Key的形式是User:ID:ICO,ID是一个用户的主键。
三、实验过程:
实验环境:
阿里云ECS服务器下的Redis6.2.1
Windows10操作系统下的IntelliJ IDEA 2020.3.3 (Ultimate Edition)
功能实现:
UserDao.Java
package com.oracle.dao;
import com.oracle.pojo.UserVO;
import java.util.List;
/**
* @author spondere
* @version 1.8
*/
public interface UserDao {
public List<UserVO> queryAll ();
public List<UserVO> queryByName (String name);
public UserVO queryById (String userId);
public void insertByHash(UserVO user);
public void insertByList(UserVO user);
public void insertByString(UserVO user);
public void updateByHash(UserVO user);
public void updateByList(UserVO user);
public void delete(UserVO user);
}
UserDaoRedisImpl.Java
package com.oracle.dao;
import com.oracle.pojo.UserVO;
import redis.clients.jedis.Jedis;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author spondere
* @version 1.8
*/
public class UserDaoRedisImpl implements UserDao{
private Jedis jedis = new Jedis("121.89.197.4",6379);
@Override
public List<UserVO> queryAll() {
jedis.auth("123");
String userlist = jedis.get(UserVO.class.getName());
String[] userarr = userlist.split(",");
List<UserVO> userList = new ArrayList<UserVO>();
for (int i = 0;i<userarr.length;i++){
UserVO v = new UserVO();
String key = userarr[i];
String value = jedis.get(key);
String[] info = value.split(",");
v.setUserId(key);
v.setUserName(info[0]);
v.setPassword(info[1]);
v.setBirthday(info[2]);
v.setUserico(info[3]);
userList.add(v);
}
return userList;
}
@Override
public List<UserVO> queryByName(String name) {
jedis.auth("123");
List<UserVO> user_list = new ArrayList<UserVO>();
String keystr = jedis.get(name);
String[] keyarr = keystr.split(",");
UserVO v = new UserVO();
for (int i=0; i<keyarr.length; i++){
String key = keyarr[i];
String value = jedis.get(key);
if (value.equals(" ") == false){
String[] info = value.split(",");
v.setUserId(key);
v.setUserName(info[0]);
v.setPassword(info[1]);
v.setBirthday(info[2]);
v.setUserico(info[3]);
user_list.add(v);
}
}
return user_list;
}
@Override
public UserVO queryById(String userId) {
jedis.auth("123");
String user_string_temp = jedis.get(userId);
if (user_string_temp == null){
return null;
}else{
String[] strarr = user_string_temp.split(",");
UserVO v = new UserVO();
v.setUserId(userId);
v.setUserName(strarr[0]);
v.setPassword(strarr[1]);
v.setBirthday(strarr[2]);
v.setUserico(strarr[3]);
return v;
}
}
@Override
public void insertByHash(UserVO user) {
jedis.auth("123");
String key = user.getUserId();
Map<String, String> userMap = new HashMap();
userMap.put("username", user.getUserName());
userMap.put("password", user.getPassword());
userMap.put("birthday", user.getBirthday());
userMap.put("userico", user.getUserico());
jedis.hmset(key, userMap);
String nameid = user.getUserName();
String namevalue = jedis.get(nameid);
if (namevalue == null){
jedis.set(nameid, key);
}else{
namevalue = namevalue + "," + key;
jedis.set(nameid, namevalue);
}
String allid = jedis.get(UserVO.class.getName());
if (allid == null){
jedis.set(UserVO.class.getName(), user.getUserId());
}else{
allid = allid + "," + user.getUserId() ;
jedis.set(UserVO.class.getName(), allid);
}
}
@Override
public void insertByList(UserVO user) {
jedis.auth("123");
String key = user.getUserId();
jedis.rpush(key,
user.getUserName(),
user.getPassword(),
user.getBirthday(),
user.getUserico());
String nameid = user.getUserName();
String namevalue = jedis.get(nameid);
if (namevalue == null){
jedis.set(nameid, key);
}else{
namevalue = namevalue + "," + key;
jedis.set(nameid, namevalue);
}
String allid = jedis.get(UserVO.class.getName());
if (allid == null){
jedis.set(UserVO.class.getName(), user.getUserId());
}else{
allid = allid + "," + user.getUserId() ;
jedis.set(UserVO.class.getName(), allid);
}
}
@Override
public void insertByString(UserVO user) {
jedis.auth("123");
String key = user.getUserId();
jedis.set(key,JSON.toJSONString(user));
String nameid = user.getUserName();
String namevalue = jedis.get(nameid);
if (namevalue == null){
jedis.set(nameid, key);
}else{
namevalue = namevalue + "," + key;
jedis.set(nameid, namevalue);
}
String allid = jedis.get(UserVO.class.getName());
if (allid == null){
jedis.set(UserVO.class.getName(), user.getUserId());
}else{
allid = allid + "," + user.getUserId() ;
jedis.set(UserVO.class.getName(), allid);
}
}
@Override
public void updateByHash(UserVO user) {
jedis.auth("123");
Map<String, String> userMap = new HashMap();
userMap.put("username", user.getUserName());
userMap.put("password", user.getPassword());
userMap.put("birthday", user.getBirthday());
userMap.put("userico", user.getUserico());
jedis.hmset(user.getUserId(), userMap);
}
@Override
public void updateByList(UserVO user) {
jedis.auth("123");
String key = user.getUserId();
jedis.del(key);
jedis.rpush(key,
user.getUserName(),
user.getPassword(),
user.getBirthday(),
user.getUserico());
}
@Override
public void delete(UserVO user) {
jedis.auth("123");
jedis.del(user.getUserId());
}
}
UserVO.Java
package com.oracle.pojo;
/**
* @author spondere
* @version 1.8
*/
public class UserVO {
private String userName;
private String password;
private String userId;
private String birthday;
private String userico;
public String getUserico() {
return userico;
}
public void setUserico(String userico) {
this.userico = userico;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
Test1.Java
package com.oracle;
import com.oracle.dao.UserDao;
import com.oracle.dao.UserDaoRedisImpl;
import com.oracle.pojo.UserVO;
import java.util.Iterator;
import java.util.List;
/**
* @author spondere
* @version 1.8
*/
public class Test1 {
public static void main(String[] args) {
UserVO t = new UserVO();
t.setUserName("test");
t.setPassword("123");
t.setUserId("1814010130");
t.setBirthday("2021-04-01");
t.setUserico("C:\\Users\\zlh\\Desktop\\图片\\Snipaste_2021-04-06_17-22-17.png");
UserDao userDao = new UserDaoRedisImpl();
userDao.insertByString(t); //插入数据
UserVO v = new UserVO();
v = userDao.queryById("1814010130"); //按 id 查找
System.out.println(v.getPassword());
System.out.println(v.getUserName());
System.out.println(v.getBirthday());
System.out.println(v.getUserico());
}
}
Test2.Java
package com.oracle;
import com.oracle.dao.UserDao;
import com.oracle.dao.UserDaoRedisImpl;
import com.oracle.pojo.UserVO;
import java.util.Iterator;
import java.util.List;
/**
* @author spondere
* @version 1.8
*/
public class Test2 {
public static void main(String[] args) {
UserVO t2 = new UserVO();
t2.setUserName("test2");
t2.setPassword("888999");
t2.setUserId("1814010130");
t2.setBirthday("2021-04-10");
t2.setUserico("C:\\Users\\zlh\\Desktop\\图片\\Snipaste_2021-04-06_17-22-17.png");
UserDao userDao = new UserDaoRedisImpl();
userDao.insertByString(t2); //插入数据
List<UserVO> q = userDao.queryByName("test2");
Iterator<UserVO> it = q.iterator();
while (it.hasNext()) {
UserVO v = it.next();
System.out.println(v.getUserId());
System.out.println(v.getPassword());
System.out.println(v.getUserName());
System.out.println(v.getBirthday());
System.out.println(v.getUserico());
}
}
}
Test3.java
package com.oracle;
import redis.clients.jedis.Jedis;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class Test3 {
static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
private static Jedis jedis = new Jedis("121.89.197.4",6379);
public static void main(String[] args) {
String path1="C:\\Users\\zlh\\Desktop\\图片\\Snipaste_2021-04-06_17-22-17.png";
String path2="C:\\Users\\zlh\\Desktop\\图片\\test.png";
String key="图片";
System.out.println(uploadFile(key, path1));
downloadFile(key,path2);
}
/**
* 将图片转换成二进制
*
* @return
*/
static String uploadFile(String key, String path) {
jedis.auth("123");
File f = new File(path);
BufferedImage bi;
try {
bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos); //经测试转换的图片是格式这里就什么格式,否则会失真
byte[] bytes = baos.toByteArray();
jedis.set(key, encoder.encodeBuffer(bytes).trim());
return encoder.encodeBuffer(bytes).trim();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 将二进制转换为图片
*
* @param key,path
*/
static void downloadFile(String key, String path) {
try {
String base64String = jedis.get(key);
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 = ImageIO.read(bais);
File w2 = new File(path);// 可以是jpg,png,gif格式
ImageIO.write(bi1, "jpg", w2);// 不管输出什么格式图片,此处不需改动
} catch (IOException e) {
e.printStackTrace();
}
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>RedistProject</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.23</version>
</dependency>
</dependencies>
</project>
四、实验结果及总结:
Test1.java运行结果:
Test1.java运行后redis数据库内容:
Test2.java运行结果:
Test2.java运行后redis数据库内容:
Test3.java运行结果:
此时可以看见文件夹里面多了一个test.png(查询字符串后保存出来的,用于验证方法可行性)
Test3.java运行后redis数据库内容:
Test.py运行结果:
此时可以看见文件夹里面多了一个jd.jpeg(查询字符串后保存出来的,用于验证方法可行性)
Test.py运行后redis数据库内容: