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

本文详细介绍了如何使用HBase和Redis数据库进行表创建、数据操作(如添加、查询和修改)以及相应的Java编程实现。包括了使用HBaseAPI插入数据、查询特定字段值和修改记录,以及使用Redis的哈希结构存储学生信息并执行相关操作。
摘要由CSDN通过智能技术生成
        if(conn!=null)
            try {
                conn.close();
            } catch (SQLException e) {

                e.printStackTrace();

运行结果:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/40538ff8dcfe4df899c9aba5e8c700f1.png#pic_center)


##### HBase 数据库操作


Student 表如表 A-5 所示。




| name | English | Math | Computer |
| --- | --- | --- | --- |
| zhangsan | 60 | 86 | 77 |
| lisi | 55 | 100 | 88 |


###### 根据上面给出的学生表 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)添加数据




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

}


运行结果  
 ![在这里插入图片描述](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) {

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

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

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

ain {

public static void main(String[] args) {

[外链图片转存中…(img-lC8pCNHx-1714762037457)]
[外链图片转存中…(img-ZwnGNrP5-1714762037457)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

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

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 15
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值