12.27
实验4
NoSQL和关系数据库的操作比较
1.实验目的
(1)理解四种数据库(MySQL、HBase、Redis和MongoDB)的概念以及不同点;
(2)熟练使用四种数据库操作常用的Shell命令;
(3)熟悉四种数据库操作常用的Java API。
2.实验平台
(1)操作系统:Linux(建议Ubuntu16.04或Ubuntu18.04);
(2)Hadoop版本:3.1.3;
(3)MySQL版本:5.6;
(4)HBase版本:2.2.2;
(5)Redis版本:5.0.5;
(6)MongoDB版本:4.0.16;
(7)JDK版本:1.8;
(8)Java IDE:Eclipse;
3.实验步骤
(一) MySQL数据库操作
学生表如14-7所示。
表14-7 学生表Student
|
Name |
English |
Math |
Computer |
|
zhangsan |
69 |
86 |
77 |
|
lisi |
55 |
100 |
88 |
- 根据上面给出的Student表,在MySQL数据库中完成如下操作:
(1)在MySQL中创建Student表,并录入数据;
(2)用SQL语句输出Student表中的所有记录;
(3)查询zhangsan的Computer成绩;
(4)修改lisi的Math成绩,改为95。
2.根据上面已经设计出的Student表,使用MySQL的JAVA客户端编程实现以下操作:
(1)向Student表中添加如下所示的一条记录:
|
scofield |
45 |
89 |
100 |
(2)获取scofield的English成绩信息
(二)HBase数据库操作
学生表Student如表14-8所示。
表14-8 学生表Student
|
name |
score |
|||
|
English |
Math |
Computer |
||
|
zhangsan |
69 |
86 |
77 |
|
|
lisi |
55 |
100 |
88 |
|
- 根据上面给出的学生表Student的信息,执行如下操作:
(1)用Hbase Shell命令创建学生表Student;
(2)用scan命令浏览Student表的相关信息;
(3)查询zhangsan的Computer成绩;
(4)修改lisi的Math成绩,改为95。
2.根据上面已经设计出的Student表,用HBase API编程实现以下操作:
(1)添加数据:English:45 Math:89 Computer:100
|
scofield |
45 |
89 |
100 |
(2)获取scofield的English成绩信息。
(三)Redis数据库操作
Student键值对如下:
|
zhangsan:{ English: 69 Math: 86 Computer: 77 } lisi:{ English: 55 Math: 100 Computer: 88 } |
1. 根据上面给出的键值对,完成如下操作:
(1)用Redis的哈希结构设计出学生表Student(键值可以用student.zhangsan和student.lisi来表示两个键值属于同一个表);
(2)用hgetall命令分别输出zhangsan和lisi的成绩信息;
(3)用hget命令查询zhangsan的Computer成绩;
(4)修改lisi的Math成绩,改为95。
2.根据上面已经设计出的学生表Student,用Redis的JAVA客户端编程(jedis),实现如下操作:
(1)添加数据:English:45 Math:89 Computer:100
该数据对应的键值对形式如下:
|
scofield:{ English: 45 Math: 89 Computer: 100 } |
(2)获取scofield的English成绩信息
(四)MongoDB数据库操作
Student文档如下:
|
{ “name”: “zhangsan”, “score”: { “English”: 69, “Math”: 86, “Computer”: 77 } } { “name”: “lisi”, “score”: { “English”: 55, “Math”: 100, “Computer”: 88 } } |
1.根据上面给出的文档,完成如下操作:
(1)用MongoDB Shell设计出student集合;
(2)用find()方法输出两个学生的信息;
(3)用find()方法查询zhangsan的所有成绩(只显示score列);
(4)修改lisi的Math成绩,改为95。
2.根据上面已经设计出的Student集合,用MongoDB的Java客户端编程,实现如下操作:
(1)添加数据:English:45 Math:89 Computer:100
与上述数据对应的文档形式如下:
|
{ “name”: “scofield”, “score”: { “English”: 45, “Math”: 89, “Computer”: 100 } } |
import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import org.bson.Document; public class AddStudentData { public static void main(String[] args) { // 创建MongoDB客户端 MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017"); // 获取或创建数据库 MongoDatabase database = mongoClient.getDatabase("SchoolDB"); // 获取或创建集合 MongoCollection<Document> collection = database.getCollection("Student"); // 创建文档 Document scoreDocument = new Document("English", 45) .append("Math", 89) .append("Computer", 100); Document studentDocument = new Document("name", "scofield") .append("score", scoreDocument); // 插入文档到集合 collection.insertOne(studentDocument); System.out.println("Inserted document for scofield."); // 关闭客户端 mongoClient.close(); } }
(2)获取scofield的所有成绩成绩信息(只显示score列)
import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import com.mongodb.client.FindIterable; import org.bson.Document; import org.bson.conversions.Bson; import java.util.Iterator; import static com.mongodb.client.model.Projections.*; public class GetStudentScores { public static void main(String[] args) { // 创建MongoDB客户端 MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017"); // 获取或创建数据库 MongoDatabase database = mongoClient.getDatabase("SchoolDB"); // 获取或创建集合 MongoCollection<Document> collection = database.getCollection("Student"); // 构建查询条件和投影 Bson filter = eq("name", "scofield"); Bson projection = fields(include("score"), excludeId()); // 执行查询 FindIterable<Document> documents = collection.find(filter).projection(projection); // 输出结果 Iterator<Document> iterator = documents.iterator(); while (iterator.hasNext()) { Document doc = iterator.next(); System.out.println(doc.toJson()); } // 关闭客户端 mongoClient.close(); } }
4.实验报告
|
题目: |
实验4 NoSQL和关系数据库的操作比较 |
姓名
|
胡铁丞 |
日期 2024-11-25日 |
|
实验环境:(1)操作系统:Linux;
(2)Hadoop版本:3.1.0。
(3)虚拟机:VMware。
(4)工具:xshell。
(5)JDK版本:1.8;
(6)Java IDE:IDEA。
(7)HBase版本:2.4;
(9)MySQL版本:5.7;
(10)Redis版本:6.0.3;
(11)MongoDB版本:4.2.0; |
||||
|
实验内容与完成情况:完成 |
||||
|
出现的问题: (1) IDEA中MongoDB的连接失败
(2) IDEA中连接redis一直失败 |
||||
|
解决方案(列出遇到的问题和解决办法,列出没有解决的问题): (1) 使用最新连接
(2) MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
(3) edis jedis = new Jedis("192.168.10.102",6379);其中IP地址为虚拟机的IP地址,并且要对redis.config进行相应设置 |
||||
浙公网安备 33010602011771号