实验4 NoSQL和关系数据库的操作比较
实验4NoSQL和关系数据库的操作比较
1**.实验**目的
(1)理解四种数据库(MySQL、HBase、Redis和MongoDB)的概念以及不同点;
(2)熟练使用四种数据库操作常用的Shell命令;
(3)熟悉四种数据库操作常用的Java API。
2**.**实验平台
(1)操作系统:Linux(建议Ubuntu16.04或Ubuntu18.04);
(2)Hadoop版本:3.1.3;
(3)MySQL版本:5.6;
(4)HBase版本:2.2.2;
(5)Redis版本:5.0.5;
(6)MongoDB版本:4.0.16;
(7)JDK版本:1.8;
(8)Java IDE:Eclipse;
3**.实验步骤******
**(一)**MySQL数据库操作
学生表Student
Name
English
Math
Computer
zhangsan
69
86
77
lisi
55
100
88
1.根据上面给出的Student表,在MySQL数据库中完成如下操作:
(1)在MySQL中创建Student表,并录入数据;
(2)用SQL语句输出Student表中的所有记录;
(3)查询zhangsan的Computer成绩;
(4)修改lisi的Math成绩,改为95。
2.根据上面已经设计出的Student表,使用MySQL的JAVA客户端编程实现以下操作:
(1)向Student表中添加如下所示的一条记录:
scofield
45
89
100
(2)获取scofield的English成绩信息
import java.sql.*;
public class mysqlTest {/*** @param args*///JDBC DRIVER and DBstatic final String DRIVER="com.mysql.jdbc.Driver";static final String DB="jdbc:mysql://localhost/test";//Database authstatic final String USER="root";static final String PASSWD="123456";public static void main(String[] args) {// TODO Auto-generated method stubConnection 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="insert into student values('scofield',45,89,100)";stmt.executeUpdate(sql);System.out.println("Inserting records into the table successfully!");//执行一个查询
// stmt=conn.createStatement();sql="select name,English from student where name='scofield' ";获得结果集rs=stmt.executeQuery(sql);System.out.println("name"+" "+"English");while(rs.next()){System.out.print(rs.getString(1)+" ");System.out.println(rs.getInt(2));}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(stmt!=null)try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(conn!=null)try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
**(二)**HBase数据库操作
学生表Student
name
score
English
Math
Computer
zhangsan
69
86
77
lisi
55
100
88
根据上面给出的学生表Student的信息,执行如下操作:
- 用Hbase Shell命令创建学生表Student
- 用scan指令浏览Student表的相关信息
- 查询zhangsan的Computer成绩
- 修改lisi的Math成绩,改为95
根据上面已经设计出的Student表,用HBase API编程实现以下操作:
(1)添加数据:English:45 Math:89 Computer:100
scofield
45
89
100
(2)获取scofield的English成绩信息
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;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Get;public class hbaseTest {/*** @param args*/public static Configuration configuration;public static Connection connection;public static Admin admin;public static void main(String[] args) {// TODO Auto-generated method stubinit();try {//插入数据
// insertRow("student","scofield","score","English","45");
// insertRow("student","scofield","score","Math","89");
// insertRow("student","scofield","score","Computer","100");//查询数据getData("student","scofield","score","English");} catch (IOException e) {// TODO Auto-generated catch blocke.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 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 init() {configuration = HBaseConfiguration.create();configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");try{connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();}catch (IOException e){e.printStackTrace();}}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来表示两个键值属于同一个表);
- 用hgetall命令分别输出zhangsan和lisi的成绩信息;
- 用hget命令查询zhangsan的Computer成绩;
- 修改lisi的Math成绩,改为95。
2.根据上面已经设计出的学生表Student,用Redis的JAVA客户端编程(jedis),实现如下操作:
(1)添加数据:English:45 Math:89 Computer:100
该数据对应的键值对形式如下:
scofield:{
English: 45
Math: 89
Computer: 100
}
(2)获取scofield的English成绩信息
import java.util.Map;
import redis.clients.jedis.Jedis;public class redisTest {/*** @param args*/public static Jedis jedis;public static void main(String[] args) {// TODO Auto-generated method stubjedis = new Jedis("localhost");//插入数据
// test1();//查询数据test2();}public static void test1() {// TODO Auto-generated method stubjedis.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());}}public static void test2() {// TODO Auto-generated method stubString value=jedis.hget("student.scofield", "English");System.out.println("scofield's English score is: "+value);}
}
**(四)**MongoDB数据库操作
Student文档如下:
{
“name”: “zhangsan”,
“score”: {
“English”: 69,
“Math”: 86,
“Computer”: 77
}
}
{
“name”: “lisi”,
“score”: {
“English”: 55,
“Math”: 100,
“Computer”: 88
}
}
- 根据上面给出的文档,完成如下操作:
- 用MongoDBShell设计出student集合;
- 用find()方法输出两个学生的信息;
- 用find()方法查询zhangsan的所有成绩(只显示score列);
- 修改lisi的Math成绩,改为95。
2.根据上面已经设计出的Student集合,用MongoDB的Java客户端编程,实现如下操作:
(1)添加数据:English:45 Math:89 Computer:100
与上述数据对应的文档形式如下:
{
“name”: “scofield”,
“score”: {
“English”: 45,
“Math”: 89,
“Computer”: 100
}
}
(2)获取scofield的所有成绩成绩信息(只显示score列)
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;
import com.mongodb.client.MongoCursor;public class mongoTest {/*** @param args*/public static MongoClient mongoClient;public static MongoDatabase mongoDatabase;public static MongoCollection<Document> collection;public static void main(String[] args) {// TODO Auto-generated method stubinit();//插入数据
// test1();//查询数据test2();}public static void test1() {// TODO Auto-generated method stub//实例化一个文档,内嵌一个子文档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("文档插入成功"); }public static void test2() {// TODO Auto-generated method stub//进行数据查找,查询条件为name=scofield, 对获取的结果集只显示score这个域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());}public static void init() {// TODO Auto-generated method stub//实例化一个mongo客户端mongoClient=new MongoClient("localhost",27017);//实例化一个mongo数据库mongoDatabase = mongoClient.getDatabase("student");//获取数据库中某个集合collection = mongoDatabase.getCollection("student");}}
实验4 NoSQL和关系数据库的操作比较相关推荐
- NoSQL和关系数据库的操作比较
1. 实验目的和要求 1.1 实验目的 理解四种数据库(MySQL.HBase.Redis和MongoDB)的概念以及不同点: 熟练使用四种数据库操作常用的Shell命令: 熟悉四种数据库操作常 ...
- 数据库实验一:数据定义与操作语言实验
实验一 数据定义与操作语言实验 实验 1.1 数据库定义实验 1.实验目的 理解和掌握数据库DDL语言,能够熟练地使用SQL DDL语句创建.修改和删除数据库.模式和基本表. 2.实验内容和要求 理解 ...
- 大数据基础系列 5:Hadoop 实验——熟悉常用的 HDFS 目录操作和文件操作
文章目录 前言 一.实验目的 二.实验平台 三.实验内容和要求 3.1.HDFS 目录操作 3.1.1.创建用户目录 3.1.2.显示 HDFS 中与当前用户对应的目录内容 3.1.3.列出 HDFS ...
- hdfs的实验总结_实验2-熟悉常用的HDFS操作.doc
本文档下载自 文库下载网, /doc/d52aebffbb0d4a7302768e9951e79b896802689c.html 实验2-熟悉常用的HDFS操作 实验2熟悉常用的HDFS操作 1实验目 ...
- 实验一 熟悉常用的Linux操作,实验2-熟悉常用的HDFS操作
<实验2-熟悉常用的HDFS操作>由会员分享,可在线阅读,更多相关<实验2-熟悉常用的HDFS操作(5页珍藏版)>请在人人文库网上搜索. 1.实验2熟悉常用的HDFS操作1 实 ...
- mysql实验训练2 数据查询操作_实验训练2:数据查询操作
<实验训练2:数据查询操作>由会员分享,可在线阅读,更多相关<实验训练2:数据查询操作(6页珍藏版)>请在人人文库网上搜索. 1.实验训练2:数据查询操作请到电脑端查看实验目的 ...
- mysql实验训练2 数据查询操作_实验训练2:数据查询操作.doc
实验训练2:数据查询操作.doc 实验训练2数据查询操作请到电脑端查看实验目的基于实验1创建的汽车用品网上商城数据库Shopping,理解MySQL运算符.函数.谓词,练习Select语句的操作方法. ...
- 数据库实验4 SQL语言-SELECT查询操作
数据库实验4 SQL语言-SELECT查询操作 1.首先按照第三章的jxgl数据库的模板创建jxgl数据库并插入数据: 创建数据库jxgl: create database jxgl; 创建相应的表: ...
- 计算机组成原理r3寄存器,计算机组成原理实验报告-寄存器的原理及操作
<计算机组成原理实验报告-寄存器的原理及操作>由会员分享,可在线阅读,更多相关<计算机组成原理实验报告-寄存器的原理及操作(10页珍藏版)>请在装配图网上搜索. 1.成绩:实 ...
最新文章
- 一个完整的schema验证xml的样例
- AbstractQueuedSynchronizer 原理分析 - 独占/共享模式
- c++预处理命令 #line 用法
- Git之深入解析如何使用Git调试项目源码中的问题
- linux系统硬盘设置密码,LUKS:Linux下磁盘加密
- php现实的九九乘法,php趣味编程 - php 输出九九乘法
- 【C++】指针与引用的区别
- 免费复制百度文库的方法
- 控制项目进度的方法之一:里程碑式管理
- oracle数据库12cocp培训教程,OCA/OCP认证考试指南全册(第3版) Oracle Database 12c 中文pdf扫描版[164MB]...
- 计算机在课堂教学中的应用,计算机技术在课堂教学中的应用
- C语言实现文件的加密解密
- 真 彻底 Navicat导入Excel文件表时无法打开的四种解决办法
- Android 时光轴 -记录生活
- jquery简单赋值取值
- 老码农眼中的CRM 图解
- 3月第2周业务风控关注 |上海市网信办依法对“华尔街见闻”作出行政处罚
- C++ AMP 实战:绘制曼德勃罗特集图像
- php源码如何使用教程,php源码的使用方法是什么?
- Reloaded modules:在Spyder运行时错误
热门文章
- 大数据舆情分析软件实时监控,TOOM大数据处理与舆情监控简介
- Arduino+A4988+步进电机
- 【Jetpack】学穿:LiveData →
- 表头让你头疼?看这几招解决Pandas读取Excel表头的问题
- 爱签电子合同联合小五科技,解锁新媒体行业电子合同数字化变局
- 宽带电视显示无法解析服务器域名怎么办,域名解析到服务器后无法访问怎么解决?...
- wi-fi_Google语音正在测试Wi-Fi呼叫,无需呼叫转移
- docker-compose 部署prometheus+grafana+alertmanager+chronograf+prometheus-webhook-dingtalk+loki
- qtcreate添加资源文件之后该文件变成只读
- 移动游戏开发商50强(世界)