NoSQL 和关系数据库的操作比较
实验目的
- 理解四种数据库(MySQL、HBase、Redis 和 MongoDB)的概念以及不同点;
- 熟练使用四种数据库操作常用的 Shell 命令;
- 熟悉四种数据库操作常用的 Java API。
实验平台
- 操作系统:centos7
- Hadoop 版本:3.3;
- MySQL 版本:8.0.22;
- HBase 版本:2.4.11;
- Redis 版本:5.0.5;
- MongoDB 版本:5.0;
- JDK 版本:1.8;
- Java IDE:IDEA;
实验步骤
(一)MySQL 数据库操作
Student 表如表 A-4 所示:
NameEnglishMathComputerzhangsan698677lisi5510088
根据上面给出的 Student 表,在 MySQL 数据库中完成如下操作:
(1)在 MySQL 中创建 Student 表,并录入数;
(2)用 SQL 语句输出 Student 表中的所有记录
(3)查询 zhangsan 的 Computer 成绩
(4)修改 lisi 的 Math 成绩,改为 95。
根据上面已经设计出的 Student 表,使用 MySQL 的 JAVA 客户端编程实现以下操作:
(1)向 Student 表中添加如下所示的一条记录:
packageMain;importjava.sql.*;importcom.mysql.jdbc.Driver;publicclass main{staticfinalStringDRIVER="com.mysql.jdbc.Driver";staticfinalStringDB="jdbc:mysql://localhost/student?useSSL=false";staticfinalStringUSER="root";staticfinalStringPASSWD="123456";publicstaticvoidmain(String[] args){Connection conn=null;Statement stmt=null;try{//加载驱动程序Class.forName(DRIVER);System.out.println("Connecting to a selected database...");//打开一个连接
conn=DriverManager.getConnection(DB,USER,PASSWD);//执行一个查询
stmt=conn.createStatement();String sql="insert into student values('scofield',45,89,100)";
stmt.executeUpdate(sql);System.out.println("Inserting records into the table successfully!");}catch(ClassNotFoundException e){
e.printStackTrace();}catch(SQLException e){
e.printStackTrace();}finally{if(stmt!=null)try{
stmt.close();}catch(SQLException e){
e.printStackTrace();}if(conn!=null)try{
conn.close();}catch(SQLException e){
e.printStackTrace();}}}}
运行结果
(2) 获取 scofield 的 English 成绩信息
packageMain;importjava.sql.*;importcom.mysql.jdbc.Driver;publicclass main {staticfinalStringDRIVER="com.mysql.jdbc.Driver";staticfinalStringDB="jdbc:mysql://localhost/student?useSSL=false";//Database authstaticfinalStringUSER="root";staticfinalStringPASSWD="root";publicstaticvoidmain(String[] args){Connection conn=null;Statement stmt=null;ResultSet rs=null;try{Class.forName(DRIVER);System.out.println("Connecting to a selected database...");
conn=DriverManager.getConnection(DB,USER,PASSWD);
stmt=conn.createStatement();String sql="select name,English from student where name='scofield' ";
rs=stmt.executeQuery(sql);System.out.println("name"+"\t\t"+"English");while(rs.next()){System.out.print(rs.getString(1)+"\t\t");System.out.println(rs.getInt(2));}}catch(ClassNotFoundException e){
e.printStackTrace();}catch(SQLException e){
e.printStackTrace();}finally{if(rs!=null)try{
rs.close();}catch(SQLException e1){
e1.printStackTrace();}if(stmt!=null)try{
stmt.close();}catch(SQLException e){
e.printStackTrace();}if(conn!=null)try{
conn.close();}catch(SQLException e){
e.printStackTrace();
运行结果:
HBase 数据库操作
Student 表如表 A-5 所示。
nameEnglishMathComputerzhangsan608677lisi5510088
根据上面给出的学生表 Student 的信息,执行如下操作:
(1)用 Hbase Shell 命令创建学生表 Student
(2)用 scan 指令浏览 Student 表的相关信息
(3)查询 zhangsan 的 Computer 成绩
(4)修改 lisi 的 Math 成绩,改为 95
根据上面已经设计出的 Student 表,用 HBase API 编程实现以下操作:
(1)添加数据
scofield4589100
packageMain;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.client.Admin;importorg.apache.hadoop.hbase.client.Connection;importorg.apache.hadoop.hbase.client.ConnectionFactory;importorg.apache.hadoop.hbase.client.Put;importorg.apache.hadoop.hbase.client.Table;publicclass main {publicstaticConfiguration configuration;publicstaticConnection connection;publicstaticAdmin admin;publicstaticvoidmain(String[] args){// TODO Auto-generated method stub
configuration =HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://127.0.0.1:8020/hbase");try{
connection =ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();}catch(IOException e){
e.printStackTrace();}try{insertRow("student","scofield","score","English","45");insertRow("student","scofield","score","Math","89");insertRow("student","scofield","score","Computer","100");}catch(IOException e){// TODO Auto-generated catch block
e.printStackTrace();}close();}publicstaticvoidinsertRow(String tableName,String rowKey,String colFamily,String col,String val)throwsIOException{Table table = connection.getTable(TableName.valueOf(tableName));Put put =newPut(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();}publicstaticvoidclose(){try{if(admin !=null){
admin.close();}if(null!= connection){
connection.close();}}catch(IOException e){
e.printStackTrace();}}}
运行结果
(2)获取 scofield 的 English 成绩信息
packageMain;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.Cell;importorg.apache.hadoop.hbase.CellUtil;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.client.Admin;importorg.apache.hadoop.hbase.client.Connection;importorg.apache.hadoop.hbase.client.ConnectionFactory;importorg.apache.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.Put;importorg.apache.hadoop.hbase.client.Result;importorg.apache.hadoop.hbase.client.Table;publicclass main {publicstaticConfiguration configuration;publicstaticConnection connection;publicstaticAdmin admin;publicstaticvoidmain(String[] args){// TODO Auto-generated method stub
configuration =HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://127.0.0.1:8020/hbase");try{
connection =ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();}catch(IOException e){
e.printStackTrace();}try{getData("student","scofield","score","English");}catch(IOException e){// TODO Auto-generated catch block
e.printStackTrace();}close();}publicstaticvoidgetData(String tableName,String rowKey,String colFamily,String col)throwsIOException{Table table = connection.getTable(TableName.valueOf(tableName));Get get =newGet(rowKey.getBytes());
get.addColumn(colFamily.getBytes(), col.getBytes());Result result = table.get(get);showCell(result);
table.close();}publicstaticvoidshowCell(Result result){Cell[] cells = result.rawCells();for(Cell cell : cells){System.out.println("RowName:"+newString(CellUtil.cloneRow(cell))+" ");System.out.println("Timetamp:"+ cell.getTimestamp()+" ");System.out.println("column Family:"+newString(CellUtil.cloneFamily(cell))+" ");System.out.println("row Name:"+newString(CellUtil.cloneQualifier(cell))+" ");System.out.println("value:"+newString(CellUtil.cloneValue(cell))+" ");}}publicstaticvoidclose(){try{if(admin !=null){
admin.close();}if(null!= connection){
connection.close();}}catch(IOException e){
e.printStackTrace();}}}
运行结果
Redis 数据库操作
Student 键值对如下:
zhangsan:{
English: 69
Math: 86
Computer: 77
}
lisi:{
English: 55
Math: 100
Computer: 88
}
根据上面给出的键值对,完成如下操作:
(1)用 Redis 的哈希结构设计出学生表 Student(键值可以用 student.zhangsan 和student.lisi 来表示两个键值属于同一个表);
(2)用 hgetall 命令分别输出 zhangsan 和 lisi 的成绩信息;
(3)用 hget 命令查询 zhangsan 的 Computer 成绩;
(4)修改 lisi 的 Math 成绩,改为 95
根据上面已经设计出的学生表 Student,用 Redis 的 JAVA 客户端编程(jedis),实现如下操作:
(1)添加数据:English:69 Math:86 Computer:77
scofield:{
English: 69
Math: 86
Computer: 77
}
packageMain;importjava.util.Map;importredis.clients.jedis.Jedis;publicclass main {/**
* @param args
*/publicstaticvoidmain(String[] args){Jedis jedis =newJedis("127.0.0.1:6379");
jedis.hset("student.scofield","English","45");
jedis.hset("student.scofield","Math","89");
jedis.hset("student.scofield","Computer","100");Map<String,String> value = jedis.hgetAll("student.scofield");for(Map.Entry<String,String> entry:value.entrySet()){System.out.println(entry.getKey()+":"+entry.getValue());}}}
(2)获取 scofield 的 English 成绩信息
packageMain;importjava.util.Map;importredis.clients.jedis.Jedis;publicclass main {/**
* @param args
*/publicstaticvoidmain(String[] args){Jedis jedis =newJedis("localhost");
jedis.hset("student.scofield","English","45");
jedis.hset("student.scofield","Math","89");
jedis.hset("student.scofield","Computer","100");Map<String,String> value = jedis.hgetAll("student.scofield");for(Map.Entry<String,String> entry:value.entrySet()){System.out.println(entry.getKey()+":"+entry.getValue());}}}
(四)MongoDB 数据库操作
Student 文档如下:
{
“name”: “zhangsan”, “score”: {“English”: 69, “Math”: 86, “Computer”: 77}
}
{
“name”: “lisi”, “score”: {“English”: 55, “Math”: 100,“Computer”: 88}
}
根据上面给出的文档,完成如下操作:
(1)用 MongoDB Shell 设计出 student 集合;
(2)用 find()方法输出两个学生的信息;
(3)用 find 函数查询 zhangsan 的所有成绩(只显示 score 列)
(4)修改 lisi 的 Math 成绩,改为 95。
根据上面已经设计出的 Student 集合,用 MongoDB 的 Java 客户端编程,实现如下操作:
(1)添加数据:English:45 Math:89 Computer:100
与上述数据对应的文档形式如下:
“name”: “scofield”,
“score”: {“English”: 45,“Math”: 89, “Computer”: 100}
packageMain;importjava.util.ArrayList;importjava.util.List;importorg.bson.Document;importcom.mongodb.MongoClient;importcom.mongodb.client.MongoCollection;importcom.mongodb.client.MongoDatabase;publicclass main {publicstaticvoidmain(String[] args){MongoClient mongoClient =newMongoClient("localhost",27017);MongoDatabase mongoDatabase = mongoClient.getDatabase("student");MongoCollection<Document> collection = mongoDatabase.getCollection("student");Document document =newDocument("name","scofield").append("score",newDocument("English",45).append("Math",89).append("Computer",100));List<Document> documents =newArrayList<Document>();
documents.add(document);
collection.insertMany(documents);System.out.println("文档插入成功");}}
(2)获取 scofield 的所有成绩成绩信息(只显示core)
packageMain;importcom.mongodb.MongoClient;importcom.mongodb.client.MongoCollection;importcom.mongodb.client.MongoCursor;importcom.mongodb.client.MongoDatabase;importorg.bson.Document;publicclass main {publicstaticvoidmain(String[] args){MongoClient mongoClient=newMongoClient("localhost",27017);MongoDatabase mongoDatabase = mongoClient.getDatabase("student");MongoCollection<Document> collection = mongoDatabase.getCollection("student");MongoCursor<Document> cursor=collection.find(newDocument("name","scofield")).projection(newDocument("score",1).append("_id",0)).iterator();while(cursor.hasNext())System.out.println(cursor.next().toJson());}}
版权归原作者 ADBOEX 所有, 如有侵权,请联系我们删除。