没腿的鸟 发表于 2024-7-13 10:01:34

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

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 表,并录入数;
https://img-blog.csdnimg.cn/950d57653ac445bcb86a20bb3eb58204.png#pic_center
(2)用 SQL 语句输出 Student 表中的所有记录
https://img-blog.csdnimg.cn/47e66669d2f245f880494acb650bd7e9.png#pic_center
(3)查询 zhangsan 的 Computer 结果
https://img-blog.csdnimg.cn/01d24a29dcdd4e898ed824d7cc43a55a.png#pic_center
(4)修改 lisi 的 Math 结果,改为 95。
https://img-blog.csdnimg.cn/ca3b6a2cd14344fb817028efee92b31c.png#pic_center
根据上面已经设计出的 Student 表,使用 MySQL 的 JAVA 客户端编程实现以下操作:

(1)向 Student 表中添加如下所示的一条记录:
package Main;

import java.sql.*;

import com.mysql.jdbc.Driver;
public class main{
    static final StringDRIVER="com.mysql.jdbc.Driver";
    static final String DB="jdbc:mysql://localhost/student?useSSL=false";
    static final String USER="root";
    static final String PASSWD="123456";

    public static void main(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();
                }
      }
    }
}

运行结果
https://img-blog.csdnimg.cn/2068976d8f1341c09fa2f69932a46c33.png#pic_center
(2) 获取 scofield 的 English 结果信息
package Main;

import java.sql.*;
import com.mysql.jdbc.Driver;
public class main {
    static final StringDRIVER="com.mysql.jdbc.Driver";
    static final String DB="jdbc:mysql://localhost/student?useSSL=false";
    //Database auth
    static final String USER="root";
    static final String PASSWD="root";

    public static void main(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();

运行结果:
https://img-blog.csdnimg.cn/40538ff8dcfe4df899c9aba5e8c700f1.png#pic_center
HBase 数据库操作

Student 表如表 A-5 所示。
nameEnglishMathComputerzhangsan608677lisi5510088 根据上面给出的门生表 Student 的信息,实行如下操作:

(1)用 Hbase Shell 命令创建门生表 Student
https://img-blog.csdnimg.cn/cfe7076c2aa84f388ba07d3d434159a0.png#pic_center(2)用 scan 指令浏览 Student 表的相关信息
https://img-blog.csdnimg.cn/4edfdc3e09ed473b9d5a71554936217c.png#pic_center
(3)查询 zhangsan 的 Computer 结果
https://img-blog.csdnimg.cn/c730a4ebf351483899b0fabad162a40b.png#pic_center
(4)修改 lisi 的 Math 结果,改为 95
https://img-blog.csdnimg.cn/33cee8a48fa44eae88ef583e897290ed.png#pic_center
根据上面已经设计出的 Student 表,用 HBase API 编程实现以下操作:

(1)添加数据
scofield4589100 package Main;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;

public class main {

    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    public static void main(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();
    }
    public static void insertRow(String tableName,String rowKey,String colFamily,
                                 String col,String val) throws IOException {
      Table table = connection.getTable(TableName.valueOf(tableName));
      Put put = new Put(rowKey.getBytes());
      put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
      table.put(put);
      table.close();
    }
    public static void close(){
      try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
      }catch (IOException e){
            e.printStackTrace();
      }
    }
}

运行结果
https://img-blog.csdnimg.cn/4be0ff4385044a969d16952f168999ff.png#pic_center
(2)获取 scofield 的 English 结果信息
package Main;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;

public class main {

    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;

    public static void main(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();
    }

    public static void getData(String tableName, String rowKey, String colFamily,
                               String col) throws IOException {
      Table table = connection.getTable(TableName.valueOf(tableName));
      Get get = new Get(rowKey.getBytes());
      get.addColumn(colFamily.getBytes(), col.getBytes());
      Result result = table.get(get);
      showCell(result);
      table.close();
    }

    public static void showCell(Result result) {
      Cell[] cells = result.rawCells();
      for (Cell cell : cells) {
            System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
            System.out.println("Timetamp:" + cell.getTimestamp() + " ");
            System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
            System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
            System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
      }
    }

    public static void close() {
      try {
            if (admin != null) {
                admin.close();
            }
            if (null != connection) {
                connection.close();
            }
      } catch (IOException e) {
            e.printStackTrace();
      }
    }
}

运行结果
https://img-blog.csdnimg.cn/42144a4759bf4921935db8519419cfa3.png#pic_center
Redis 数据库操作

Student 键值对如下:
zhangsan:{
English: 69
Math: 86
Computer: 77

lisi:{
English: 55
Math: 100
Computer: 88
}
根据上面给出的键值对,完成如下操作:

(1)用 Redis 的哈希布局设计出门生表 Student(键值可以用 student.zhangsan 和student.lisi 来表现两个键值属于同一个表);
https://img-blog.csdnimg.cn/5868c853a37d447b9a5c19f9064e1211.png#pic_center
(2)用 hgetall 命令分别输出 zhangsan 和 lisi 的结果信息;
https://img-blog.csdnimg.cn/2220276ac45340cbb8e8870807c155c6.png#pic_center
(3)用 hget 命令查询 zhangsan 的 Computer 结果;
https://img-blog.csdnimg.cn/721676fa83014699b6db82f7b169ee51.png#pic_center
(4)修改 lisi 的 Math 结果,改为 95
https://img-blog.csdnimg.cn/282a1465800f4409a63d75662dcf51fb.png#pic_center
根据上面已经设计出的门生表 Student,用 Redis 的 JAVA 客户端编程(jedis),实现如下操作:

(1)添加数据:English:69 Math:86 Computer:77
scofield:{
English: 69
Math: 86
Computer: 77

package Main;

import java.util.Map;
import redis.clients.jedis.Jedis;

public class main {

    /**
   * @param args
   */
    public static void main(String[] args) {
      Jedis jedis = new Jedis("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 成绩信息
package Main;

import java.util.Map;
import redis.clients.jedis.Jedis;

public class main {

    /**
   * @param args
   */
    public static void main(String[] args) {
      Jedis jedis = new Jedis("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 集合;
https://img-blog.csdnimg.cn/988422fa1a7447bfa36d9d2840408262.png#pic_center
(2)用 find()方法输出两个门生的信息;
https://img-blog.csdnimg.cn/2dabe913f90f413da17820e30b63ca0b.png#pic_center
(3)用 find 函数查询 zhangsan 的所有结果(只显示 score 列)
https://img-blog.csdnimg.cn/77a2ccc809a1460aaa1146fa3dc331a5.png#pic_center
(4)修改 lisi 的 Math 结果,改为 95。
https://img-blog.csdnimg.cn/0407e19756334e66ba0a06d9dfef347c.png#pic_center
根据上面已经设计出的 Student 集合,用 MongoDB 的 Java 客户端编程,实现如下操作:

(1)添加数据:English:45 Math:89 Computer:100
与上述数据对应的文档情势如下:
“name”: “scofield”,
“score”: {“English”: 45,“Math”: 89, “Computer”: 100}
package Main;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class main {

    public static void main(String[] args) {

      MongoClient mongoClient = new MongoClient("localhost", 27017);

      MongoDatabase mongoDatabase = mongoClient.getDatabase("student");

      MongoCollection<Document> collection = mongoDatabase.getCollection("student");

      Document document = new Document("name", "scofield").
                append("score", new Document("English", 45).
                        append("Math", 89).
                        append("Computer", 100));
      List<Document> documents = new ArrayList<Document>();
      documents.add(document);
      collection.insertMany(documents);
      System.out.println("文档插入成功");
    }
}

https://img-blog.csdnimg.cn/23d6467fccbf4a16a6404c8a01740124.png#pic_center
(2)获取 scofield 的所有结果结果信息(只显示core)
package Main;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class main {

    public static void main(String[] args) {

      MongoClientmongoClient=new MongoClient("localhost",27017);

      MongoDatabase mongoDatabase = mongoClient.getDatabase("student");

      MongoCollection<Document> collection = mongoDatabase.getCollection("student");

      MongoCursor<Document>cursor=collection.find( new Document("name","scofield")).
                projection(new Document("score",1).append("_id", 0)).iterator();
      while(cursor.hasNext())
            System.out.println(cursor.next().toJson());
    }
}

https://img-blog.csdnimg.cn/4f581ec2489b4417a34bad1fab17cef6.png#pic_center

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 大数据实验 实验四:NoSQL 和关系数据库的操作比力