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

这篇具有很好参考价值的文章主要介绍了大数据实验 实验四:NoSQL 和关系数据库的操作比较。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

NoSQL 和关系数据库的操作比较

实验目的

  1. 理解四种数据库(MySQL、HBase、Redis 和 MongoDB)的概念以及不同点;
  2. 熟练使用四种数据库操作常用的 Shell 命令;
  3. 熟悉四种数据库操作常用的 Java API。

实验平台

  1. 操作系统:centos7
  2. Hadoop 版本:3.3;
  3. MySQL 版本:8.0.22;
  4. HBase 版本:2.4.11;
  5. Redis 版本:5.0.5;
  6. MongoDB 版本:5.0;
  7. JDK 版本:1.8;
  8. Java IDE:IDEA;

实验步骤

(一)MySQL 数据库操作

Student 表如表 A-4 所示:

Name English Math Computer
zhangsan 69 86 77
lisi 55 100 88
根据上面给出的 Student 表,在 MySQL 数据库中完成如下操作:

(1)在 MySQL 中创建 Student 表,并录入数;
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库
(2)用 SQL 语句输出 Student 表中的所有记录
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库
(3)查询 zhangsan 的 Computer 成绩
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库
(4)修改 lisi 的 Math 成绩,改为 95。

实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

根据上面已经设计出的 Student 表,使用 MySQL 的 JAVA 客户端编程实现以下操作:

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

package Main;

import java.sql.*;

import com.mysql.jdbc.Driver;
public class main{
    static final String  DRIVER="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();
                }
        }
    }
}

运行结果
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

(2) 获取 scofield 的 English 成绩信息

package Main;

import java.sql.*;
import com.mysql.jdbc.Driver;
public class main {
    static final String  DRIVER="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();

运行结果:
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

HBase 数据库操作

Student 表如表 A-5 所示。

name English Math Computer
zhangsan 60 86 77
lisi 55 100 88
根据上面给出的学生表 Student 的信息,执行如下操作:

(1)用 Hbase Shell 命令创建学生表 Student
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库(2)用 scan 指令浏览 Student 表的相关信息
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库
(3)查询 zhangsan 的 Computer 成绩

实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库
(4)修改 lisi 的 Math 成绩,改为 95
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

根据上面已经设计出的 Student 表,用 HBase API 编程实现以下操作:

(1)添加数据

scofield 45 89 100
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();
        }
    }
}

运行结果
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

(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();
        }
    }
}

运行结果

实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

Redis 数据库操作

Student 键值对如下:

zhangsan:{
English: 69
Math: 86
Computer: 77

lisi:{
English: 55
Math: 100
Computer: 88
}

根据上面给出的键值对,完成如下操作:

(1)用 Redis 的哈希结构设计出学生表 Student(键值可以用 student.zhangsan 和student.lisi 来表示两个键值属于同一个表);
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库
(2)用 hgetall 命令分别输出 zhangsan 和 lisi 的成绩信息;

实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库
(3)用 hget 命令查询 zhangsan 的 Computer 成绩;
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库
(4)修改 lisi 的 Math 成绩,改为 95
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

根据上面已经设计出的学生表 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 集合;
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

(2)用 find()方法输出两个学生的信息;

实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

(3)用 find 函数查询 zhangsan 的所有成绩(只显示 score 列)
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库
(4)修改 lisi 的 Math 成绩,改为 95。
实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

根据上面已经设计出的 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("文档插入成功");
    }
}

实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库

(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) {

        MongoClient  mongoClient=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());
    }
}

实验四nosql和关系数据库的操作比较实验报告,大数据实验,大数据,nosql,数据库文章来源地址https://www.toymoban.com/news/detail-853266.html

到了这里,关于大数据实验 实验四:NoSQL 和关系数据库的操作比较的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 常见数据库介绍对比之NoSQL非关系型数据库

    常见的非关系型数据库(NoSQL)包括以下几种: MongoDB: MongoDB是最受欢迎的文档型数据库之一。它使用BSON(二进制JSON)格式存储数据,并提供灵活的数据模型和复杂的查询功能。MongoDB支持水平扩展和高可用性,并具有丰富的生态系统和工具支持。 CouchDB: CouchDB是另一个流行的

    2024年02月09日
    浏览(34)
  • 看这篇就明白大数据实时数仓、离线数仓、数据湖之间的关系

      20世纪70年代,MIT(麻省理工)的研究员致力于研究一种优化的技术架构,该架构试图将业务处理系统和分析系统分开,即将业务处理和分析处理分为不同层次,针对各自的特点采取不同的架构设计原则,MIT的研究员认为这两种信息处理的方式具有显著差别,以至于必须采取完

    2024年02月08日
    浏览(35)
  • Redis基于内存的key-value结构化NOSQL(非关系型)数据库

    Redis基于内存的key-value结构的NOSQL(非关系型)数据库 非关系型数据库:表与表之间没有复杂的关系 基于内存存储,读写性能高 – Redis读的速度是110000次/S 适合存储热点数据(商品、新闻资讯) 它存储的value类型比较丰富,也称为结构化NoSQL数据库 直接解压windows版压缩包就

    2024年02月11日
    浏览(49)
  • 分布式数据库NoSQL(二)——MongoDB 数据库基本操作

    MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。 MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似 json 的

    2024年02月06日
    浏览(36)
  • 几个常用的nosql数据库的操作方式

    dynamoDB 键 partition key:分区键 定义:分区键是用于分布数据存储的主键,每个项(Item)在表中都必须有一个唯一的分区键值。 特点: 唯一性:每个分区键值在表中必须是唯一的,这是因为分区键决定了数据在物理存储中的位置。 数据分布:选择一个良好的分区键可以确保数

    2024年02月08日
    浏览(26)
  • Python办公自动化 – 操作NoSQL数据库和自动化图像识别

    以下是往期的文章目录,需要可以查看哦。 Python办公自动化 – Excel和Word的操作运用 Python办公自动化 – Python发送电子邮件和Outlook的集成 Python办公自动化 – 对PDF文档和PPT文档的处理 Python办公自动化 – 对Excel文档和数据库的操作运用、设置计划任务 Python办公自动化 – 对

    2024年02月02日
    浏览(43)
  • 大数据实验 实验六:Spark初级编程实践

    实验环境:Windows 10 Oracle VM VirtualBox 虚拟机:cnetos 7 Hadoop 3.3 因为Hadoop版本为3.3所以在官网选择支持3.3的spark安装包 解压安装包到指定文件夹 配置spark-env.sh 启动成功 (1) 在spark-shell中读取Linux系统本地文件“/home/hadoop/test.txt”,然后统计出文件的行数; (2) 在spark-shell中读

    2024年02月04日
    浏览(69)
  • 实验一 关系数据库标准语言SQL 课后习题/头歌

    任务要求 建立demo数据库 并显示所有数据库 第2关:创建表 任务要求 设有一个demo数据库,包括S,P,J,SPJ四个关系模式: S(SNO,SNAME,STATUS,CITY) P(PNO,PNAME,COLOR,WEIGHT) J(JNO,JNAME,CITY) SPJ(SNO,PNO,JNO,QTY) 供应商表S由供应商代码(SNO)、供应商姓名(SNAME)、供应商状态(STATUS)、供应商所在城市(CI

    2024年02月05日
    浏览(46)
  • 数据库关系操作集合

    传统集合运算包括 联合(UNION),差集(EXCEPT 或 MINUS 或 LEFT JOINIS NULL),交集(INTERSECT或INNER JOIN),笛卡尔积(JOIN) 。 需要注意的是,不同数据库语法可能会有些不同,不过大体概念即是该段所讲内容。 1:联合(UNION) 联合作用:删除重复的行。 它会分别对比两个表的所

    2024年02月07日
    浏览(23)
  • 【数据库】实验 1:数据库定义与操作语言实验

    本篇文章相当于是一个简单的SQL语言归纳总结,包括数据库定义和一些基本的数据操作。值得注意的是,不同的数据库系统有着自己的特点,语法不完全相同,在使用具体系统是可以查阅各产品的用户手册。 提示:以下是本篇文章正文内容,下面案例可供参考 本实验所使用

    2024年02月02日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包