5月12日
实验4
NoSQL和关系数据库的操作比较
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数据库操作
学生表如14-7所示。
表14-7 学生表Student
|
Name |
English |
Math |
Computer |
|
zhangsan |
69 |
86 |
77 |
|
lisi |
55 |
100 |
88 |
- 根据上面给出的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成绩信息
(二)HBase数据库操作
学生表Student如表14-8所示。
表14-8 学生表Student
|
name |
score |
|||
|
English |
Math |
Computer |
||
|
zhangsan |
69 |
86 |
77 |
|
|
lisi |
55 |
100 |
88 |
|
- 根据上面给出的学生表Student的信息,执行如下操作:
(1)用Hbase Shell命令创建学生表Student;
(2)用scan命令浏览Student表的相关信息;
(3)查询zhangsan的Computer成绩;
(4)修改lisi的Math成绩,改为95。
2.根据上面已经设计出的Student表,用HBase API编程实现以下操作:
(1)添加数据:English:45 Math:89 Computer:100
|
scofield |
45 |
89 |
100 |
package com.xusheng.nosql.hbase;
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 hbase_insert {
/**
* @param xusheng
*/
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://localhost:9000/hbase");
//configuration.set("hbase.rootdir","hdfs://node1:8020/HBase");
configuration.set("hbase.zookeeper.quorum","node1,node2,node3");
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 com.xusheng.nosql.hbase;
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 hbase_query {
/**
* @param args
*/
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.zookeeper.quorum","node1,node2,node3");
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. 根据上面给出的键值对,完成如下操作:
(1)用Redis的哈希结构设计出学生表Student(键值可以用student.zhangsan和student.lisi来表示两个键值属于同一个表);
(2)用hgetall命令分别输出zhangsan和lisi的成绩信息;
(3)用hget命令查询zhangsan的Computer成绩;
(4)修改lisi的Math成绩,改为95。
2.根据上面已经设计出的学生表Student,用Redis的JAVA客户端编程(jedis),实现如下操作:
(1)添加数据:English:45 Math:89 Computer:100
该数据对应的键值对形式如下:
|
scofield:{ English: 45 Math: 89 Computer: 100 } |
package com.xusheng.nosql.redis;
import java.util.Map;
import redis.clients.jedis.Jedis;
public class jedis_test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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());
}
}
}
(2)获取scofield的English成绩信息
package com.xusheng.nosql.redis;
import java.util.Map;
import redis.clients.jedis.Jedis;
public class jedis_query {
/**
* @param xusheng
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Jedis jedis = new Jedis("localhost");
String 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 } } |
1.根据上面给出的文档,完成如下操作:
(1)用MongoDB Shell设计出student集合;
(2)用find()方法输出两个学生的信息;
(3)用find()方法查询zhangsan的所有成绩(只显示score列);
(4)修改lisi的Math成绩,改为95。
2.根据上面已经设计出的Student集合,用MongoDB的Java客户端编程,实现如下操作:
(1)添加数据:English:45 Math:89 Computer:100
与上述数据对应的文档形式如下:
|
{ “name”: “scofield”, “score”: { “English”: 45, “Math”: 89, “Computer”: 100 } } |
package com.xusheng.nosql.MongoDB;
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 mongo_insert {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//实例化一个mongo客户端
MongoClient mongoClient = new MongoClient("localhost", 27017);
//实例化一个mongo数据库
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的所有成绩成绩信息(只显示score列)
package com.xusheng.nosql.MongoDB;
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 mongo_query {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//实例化一个mongo客户端
MongoClient mongoClient=new MongoClient("localhost",27017);
//实例化一个mongo数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("student");
//获取数据库中某个集合
MongoCollection<Document> collection = mongoDatabase.getCollection("student");
//进行数据查找,查询条件为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());
}
}
4.实验报告
|
题目: |
NoSQL和关系数据库的操作比较 |
姓名 |
陈志峰 |
日期 |
|
实验环境:Hadoop版本:3.1.3 |
||||
|
实验内容与完成情况:全部完成 |
||||
|
出现的问题:无 |
||||
|
解决方案(列出遇到的问题和解决办法,列出没有解决的问题):无 |
||||

浙公网安备 33010602011771号