大数据实验 实验四:NoSQL 和关系数据库的操作比力

没腿的鸟  金牌会员 | 2024-7-13 10:01:34 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 529|帖子 529|积分 1587

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 表中添加如下所示的一条记录:
  1. package Main;
  2. import java.sql.*;
  3. import com.mysql.jdbc.Driver;
  4. public class main{
  5.     static final String  DRIVER="com.mysql.jdbc.Driver";
  6.     static final String DB="jdbc:mysql://localhost/student?useSSL=false";
  7.     static final String USER="root";
  8.     static final String PASSWD="123456";
  9.     public static void main(String[] args) {
  10.         Connection conn=null;
  11.         Statement stmt=null;
  12.         try {
  13.             //加载驱动程序
  14.             Class.forName(DRIVER);
  15.             System.out.println("Connecting to a selected database...");
  16.             //打开一个连接
  17.             conn=DriverManager.getConnection(DB, USER, PASSWD);
  18.             //执行一个查询
  19.             stmt=conn.createStatement();
  20.             String sql="insert into student values('scofield',45,89,100)";
  21.             stmt.executeUpdate(sql);
  22.             System.out.println("Inserting records into the table successfully!");
  23.         } catch (ClassNotFoundException e) {
  24.             e.printStackTrace();
  25.         }catch (SQLException e) {
  26.             e.printStackTrace();
  27.         }finally
  28.         {
  29.             if(stmt!=null)
  30.                 try {
  31.                     stmt.close();
  32.                 } catch (SQLException e) {
  33.                     e.printStackTrace();
  34.                 }
  35.             if(conn!=null)
  36.                 try {
  37.                     conn.close();
  38.                 } catch (SQLException e) {
  39.                     e.printStackTrace();
  40.                 }
  41.         }
  42.     }
  43. }
复制代码
运行结果

(2) 获取 scofield 的 English 结果信息
  1. package Main;
  2. import java.sql.*;
  3. import com.mysql.jdbc.Driver;
  4. public class main {
  5.     static final String  DRIVER="com.mysql.jdbc.Driver";
  6.     static final String DB="jdbc:mysql://localhost/student?useSSL=false";
  7.     //Database auth
  8.     static final String USER="root";
  9.     static final String PASSWD="root";
  10.     public static void main(String[] args) {
  11.         Connection conn=null;
  12.         Statement stmt=null;
  13.         ResultSet rs=null;
  14.         try {
  15.             Class.forName(DRIVER);
  16.             System.out.println("Connecting to a selected database...");
  17.             conn=DriverManager.getConnection(DB, USER, PASSWD);
  18.             stmt=conn.createStatement();
  19.             String sql="select name,English from student where name='scofield' ";
  20.             rs=stmt.executeQuery(sql);
  21.             System.out.println("name"+"\t\t"+"English");
  22.             while(rs.next())
  23.             {
  24.                 System.out.print(rs.getString(1)+"\t\t");
  25.                 System.out.println(rs.getInt(2));
  26.             }
  27.         } catch (ClassNotFoundException e) {
  28.             e.printStackTrace();
  29.         }catch (SQLException e) {
  30.             e.printStackTrace();
  31.         }finally
  32.         {
  33.             if(rs!=null)
  34.                 try {
  35.                     rs.close();
  36.                 } catch (SQLException e1) {
  37.                     e1.printStackTrace();
  38.                 }
  39.             if(stmt!=null)
  40.                 try {
  41.                     stmt.close();
  42.                 } catch (SQLException e) {
  43.                     e.printStackTrace();
  44.                 }
  45.             if(conn!=null)
  46.                 try {
  47.                     conn.close();
  48.                 } catch (SQLException e) {
  49.                     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
  1. package Main;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.HBaseConfiguration;
  5. import org.apache.hadoop.hbase.TableName;
  6. import org.apache.hadoop.hbase.client.Admin;
  7. import org.apache.hadoop.hbase.client.Connection;
  8. import org.apache.hadoop.hbase.client.ConnectionFactory;
  9. import org.apache.hadoop.hbase.client.Put;
  10. import org.apache.hadoop.hbase.client.Table;
  11. public class main {
  12.     public static Configuration configuration;
  13.     public static Connection connection;
  14.     public static Admin admin;
  15.     public static void main(String[] args) {
  16.         // TODO Auto-generated method stub
  17.         configuration  = HBaseConfiguration.create();
  18.         configuration.set("hbase.rootdir","hdfs://127.0.0.1:8020/hbase");
  19.         try{
  20.             connection = ConnectionFactory.createConnection(configuration);
  21.             admin = connection.getAdmin();
  22.         }catch (IOException e){
  23.             e.printStackTrace();
  24.         }
  25.         try {
  26.             insertRow("student","scofield","score","English","45");
  27.             insertRow("student","scofield","score","Math","89");
  28.             insertRow("student","scofield","score","Computer","100");
  29.         } catch (IOException e) {
  30.             // TODO Auto-generated catch block
  31.             e.printStackTrace();
  32.         }
  33.         close();
  34.     }
  35.     public static void insertRow(String tableName,String rowKey,String colFamily,
  36.                                  String col,String val) throws IOException {
  37.         Table table = connection.getTable(TableName.valueOf(tableName));
  38.         Put put = new Put(rowKey.getBytes());
  39.         put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
  40.         table.put(put);
  41.         table.close();
  42.     }
  43.     public static void close(){
  44.         try{
  45.             if(admin != null){
  46.                 admin.close();
  47.             }
  48.             if(null != connection){
  49.                 connection.close();
  50.             }
  51.         }catch (IOException e){
  52.             e.printStackTrace();
  53.         }
  54.     }
  55. }
复制代码
运行结果

(2)获取 scofield 的 English 结果信息
  1. package Main;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.Cell;
  5. import org.apache.hadoop.hbase.CellUtil;
  6. import org.apache.hadoop.hbase.HBaseConfiguration;
  7. import org.apache.hadoop.hbase.TableName;
  8. import org.apache.hadoop.hbase.client.Admin;
  9. import org.apache.hadoop.hbase.client.Connection;
  10. import org.apache.hadoop.hbase.client.ConnectionFactory;
  11. import org.apache.hadoop.hbase.client.Get;
  12. import org.apache.hadoop.hbase.client.Put;
  13. import org.apache.hadoop.hbase.client.Result;
  14. import org.apache.hadoop.hbase.client.Table;
  15. public class main {
  16.     public static Configuration configuration;
  17.     public static Connection connection;
  18.     public static Admin admin;
  19.     public static void main(String[] args) {
  20.         // TODO Auto-generated method stub
  21.         configuration = HBaseConfiguration.create();
  22.         configuration.set("hbase.rootdir", "hdfs://127.0.0.1:8020/hbase");
  23.         try {
  24.             connection = ConnectionFactory.createConnection(configuration);
  25.             admin = connection.getAdmin();
  26.         } catch (IOException e) {
  27.             e.printStackTrace();
  28.         }
  29.         try {
  30.             getData("student", "scofield", "score", "English");
  31.         } catch (IOException e) {
  32.             // TODO Auto-generated catch block
  33.             e.printStackTrace();
  34.         }
  35.         close();
  36.     }
  37.     public static void getData(String tableName, String rowKey, String colFamily,
  38.                                String col) throws IOException {
  39.         Table table = connection.getTable(TableName.valueOf(tableName));
  40.         Get get = new Get(rowKey.getBytes());
  41.         get.addColumn(colFamily.getBytes(), col.getBytes());
  42.         Result result = table.get(get);
  43.         showCell(result);
  44.         table.close();
  45.     }
  46.     public static void showCell(Result result) {
  47.         Cell[] cells = result.rawCells();
  48.         for (Cell cell : cells) {
  49.             System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
  50.             System.out.println("Timetamp:" + cell.getTimestamp() + " ");
  51.             System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
  52.             System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
  53.             System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
  54.         }
  55.     }
  56.     public static void close() {
  57.         try {
  58.             if (admin != null) {
  59.                 admin.close();
  60.             }
  61.             if (null != connection) {
  62.                 connection.close();
  63.             }
  64.         } catch (IOException e) {
  65.             e.printStackTrace();
  66.         }
  67.     }
  68. }
复制代码
运行结果

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

  1. package Main;
  2. import java.util.Map;
  3. import redis.clients.jedis.Jedis;
  4. public class main {
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         Jedis jedis = new Jedis("127.0.0.1:6379");
  10.         jedis.hset("student.scofield", "English","45");
  11.         jedis.hset("student.scofield", "Math","89");
  12.         jedis.hset("student.scofield", "Computer","100");
  13.         Map<String,String>  value = jedis.hgetAll("student.scofield");
  14.         for(Map.Entry<String, String> entry:value.entrySet())
  15.         {
  16.             System.out.println(entry.getKey()+":"+entry.getValue());
  17.         }
  18.     }
  19. }
  20. (2)获取 scofield 的 English 成绩信息
  21. package Main;
  22. import java.util.Map;
  23. import redis.clients.jedis.Jedis;
  24. public class main {
  25.     /**
  26.      * @param args
  27.      */
  28.     public static void main(String[] args) {
  29.         Jedis jedis = new Jedis("localhost");
  30.         jedis.hset("student.scofield", "English","45");
  31.         jedis.hset("student.scofield", "Math","89");
  32.         jedis.hset("student.scofield", "Computer","100");
  33.         Map<String,String>  value = jedis.hgetAll("student.scofield");
  34.         for(Map.Entry<String, String> entry:value.entrySet())
  35.         {
  36.             System.out.println(entry.getKey()+":"+entry.getValue());
  37.         }
  38.     }
  39. }
复制代码
(四)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}
  1. package Main;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.bson.Document;
  5. import com.mongodb.MongoClient;
  6. import com.mongodb.client.MongoCollection;
  7. import com.mongodb.client.MongoDatabase;
  8. public class main {
  9.     public static void main(String[] args) {
  10.         MongoClient mongoClient = new MongoClient("localhost", 27017);
  11.         MongoDatabase mongoDatabase = mongoClient.getDatabase("student");
  12.         MongoCollection<Document> collection = mongoDatabase.getCollection("student");
  13.         Document document = new Document("name", "scofield").
  14.                 append("score", new Document("English", 45).
  15.                         append("Math", 89).
  16.                         append("Computer", 100));
  17.         List<Document> documents = new ArrayList<Document>();
  18.         documents.add(document);
  19.         collection.insertMany(documents);
  20.         System.out.println("文档插入成功");
  21.     }
  22. }
复制代码

(2)获取 scofield 的所有结果结果信息(只显示core)
  1. package Main;
  2. import com.mongodb.MongoClient;
  3. import com.mongodb.client.MongoCollection;
  4. import com.mongodb.client.MongoCursor;
  5. import com.mongodb.client.MongoDatabase;
  6. import org.bson.Document;
  7. public class main {
  8.     public static void main(String[] args) {
  9.         MongoClient  mongoClient=new MongoClient("localhost",27017);
  10.         MongoDatabase mongoDatabase = mongoClient.getDatabase("student");
  11.         MongoCollection<Document> collection = mongoDatabase.getCollection("student");
  12.         MongoCursor<Document>  cursor=collection.find( new Document("name","scofield")).
  13.                 projection(new Document("score",1).append("_id", 0)).iterator();
  14.         while(cursor.hasNext())
  15.             System.out.println(cursor.next().toJson());
  16.     }
  17. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

没腿的鸟

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表