大数据实验 实验四:NoSQL 和关系数据库的操作比较_实验四nosql和关系数据库的操作比较实验报告(2)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

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


运行结果
在这里插入图片描述

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


运行结果

在这里插入图片描述

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

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 集合;
在这里插入图片描述

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

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("文档插入成功");
    }
}


在这里插入图片描述

(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")).


![img](https://img-blog.csdnimg.cn/img_convert/72bb1e8ce724963b5667f262e0fbe703.png)
![img](https://img-blog.csdnimg.cn/img_convert/9ae134de16bbf32dda02e392bac31695.png)
![img](https://img-blog.csdnimg.cn/img_convert/8383e171400ee5d457b8f75a3431943c.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

ocument("name","scofield")).


[外链图片转存中...(img-v1Nr1TMw-1715414206964)]
[外链图片转存中...(img-54snSWXJ-1715414206965)]
[外链图片转存中...(img-i4KLCw5Q-1715414206965)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验比较NoSQL关系数据库操作。 首先,NoSQL数据库相对于关系数据库来说更加灵活和扩展性强。NoSQL数据库可以支持非结构化和半结构化数据,而关系数据库只支持结构化数据。这意味着在NoSQL数据库中可以存储更加复杂和多样化的数据类型,比如文档、图形和键值对等。 其次,NoSQL数据库的存储模型和数据结构灵活性更大。NoSQL数据库采用了不同的存储模型,比如键值对、列族、文档型和图形数据库等。每种存储模型都有自己的优势和适用场景,可以根据具体需求选择最合适的模型。而关系数据库只支持表格结构,需要预先定义表的模式和字段。 另外,NoSQL数据库在读写性能方面通常表现更好。NoSQL数据库可以水平扩展,即通过增加服务器节点来提高读写吞吐量,而关系数据库通常只能通过垂直扩展,即增加服务器的硬件性能来提高吞吐量。因此,在处理大规模数据和高并发的场景下,NoSQL数据库的性能更好。 然而,相对于关系数据库NoSQL数据库的一致性和事务支持通常较弱。由于NoSQL数据库通常采用分布式架构,数据的一致性和并发控制可能存在较大挑战,需要应用程序自己处理。而关系数据库通常具有强一致性和严格的事务支持。 综上所述,NoSQL关系数据库操作有一些重要的区别。NoSQL数据库更加灵活和可扩展,适用于非结构化和半结构化数据的存储;关系数据库则更加适合结构化数据,并具有更强的一致性和事务支持。根据具体需求,可以选择合适的数据库类型来处理不同类型的数据和场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值