大数据练习四
实验4:NoSQL和关系数据库的操作比较
实验环境
操作系统:Ubuntu 18.04
Hadoop版本:3.1.3
MySQL版本:5.6.47
HBase版本:2.2.2
Redis版本:5.0.5
MongoDB版本:4.0.16
JDK版本:1.8.0_212
实验内容与完成情况
(一)MySQL数据库操作
-
MySQL Shell命令操作
序号 操作 命令 执行结果
1.1 连接MySQL,创建Student表 mysql -u root -p
CREATE DATABASE hadoop_experiment;
USE hadoop_experiment;
CREATE TABLE Student (Name VARCHAR(20), English INT, Math INT, Computer INT); 成功创建数据库和表
1.2 录入数据 INSERT INTO Student VALUES ('zhangsan', 69, 86, 77);
INSERT INTO Student VALUES ('lisi', 55, 100, 88); 成功插入两条记录
1.3 输出Student表中的所有记录 SELECT * FROM Student; 显示所有记录
1.4 查询zhangsan的Computer成绩 SELECT Computer FROM Student WHERE Name='zhangsan'; 显示zhangsan的Computer成绩为77
1.5 修改lisi的Math成绩为95 UPDATE Student SET Math=95 WHERE Name='lisi';
SELECT * FROM Student WHERE Name='lisi'; 成功修改,显示lisi的Math成绩为95 -
MySQL Java客户端编程实现
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MySQLJavaClient {
private static final String URL = "jdbc:mysql://localhost:3306/hadoop_experiment?useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "your_password";
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
// (1)向Student表中添加记录:scofield 45 89 100
String insertSQL = "INSERT INTO Student (Name, English, Math, Computer) VALUES (?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(insertSQL)) {
pstmt.setString(1, "scofield");
pstmt.setInt(2, 45);
pstmt.setInt(3, 89);
pstmt.setInt(4, 100);
pstmt.executeUpdate();
System.out.println("成功添加记录:scofield 45 89 100");
}
// (2)获取scofield的English成绩信息
String selectSQL = "SELECT English FROM Student WHERE Name=?";
try (PreparedStatement pstmt = conn.prepareStatement(selectSQL)) {
pstmt.setString(1, "scofield");
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
int englishScore = rs.getInt("English");
System.out.println("scofield的English成绩:" + englishScore);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
编译和运行命令:
javac -cp mysql-connector-java-5.1.47.jar MySQLJavaClient.java
java -cp .:mysql-connector-java-5.1.47.jar MySQLJavaClient
执行结果:
成功添加记录:scofield 45 89 100
scofield的English成绩:45
(二)HBase数据库操作
-
HBase Shell命令操作
序号 操作 命令 执行结果
2.1 创建学生表Student create 'Student', 'score' 成功创建表
2.2 录入数据 put 'Student', 'zhangsan', 'score:English', '69'
put 'Student', 'zhangsan', 'score:Math', '86'
put 'Student', 'zhangsan', 'score:Computer', '77'
put 'Student', 'lisi', 'score:English', '55'
put 'Student', 'lisi', 'score:Math', '100'
put 'Student', 'lisi', 'score:Computer', '88' 成功插入数据
2.3 用scan命令浏览Student表 scan 'Student' 显示所有记录
2.4 查询zhangsan的Computer成绩 get 'Student', 'zhangsan', 'score:Computer' 显示zhangsan的Computer成绩为77
2.5 修改lisi的Math成绩为95 put 'Student', 'lisi', 'score:Math', '95'
get 'Student', 'lisi', 'score:Math' 成功修改,显示lisi的Math成绩为95 -
HBase Java API编程实现
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.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;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseStudentAPI {
private static Configuration conf = HBaseConfiguration.create();
public static void main(String[] args) throws IOException {
try (Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("Student"))) {
// (1)添加数据:scofield English:45 Math:89 Computer:100
Put put = new Put(Bytes.toBytes("scofield"));
put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("English"), Bytes.toBytes("45"));
put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("Math"), Bytes.toBytes("89"));
put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("Computer"), Bytes.toBytes("100"));
table.put(put);
System.out.println("成功添加scofield的成绩记录");
// (2)获取scofield的English成绩信息
Get get = new Get(Bytes.toBytes("scofield"));
get.addColumn(Bytes.toBytes("score"), Bytes.toBytes("English"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("score"), Bytes.toBytes("English"));
System.out.println("scofield的English成绩:" + Bytes.toString(value));
}
}
}
编译和运行命令:
javac -cp $(hbase classpath) HBaseStudentAPI.java
java -cp $(hbase classpath):. HBaseStudentAPI
执行结果:
成功添加scofield的成绩记录
scofield的English成绩:45
(三)Redis数据库操作
-
Redis Shell命令操作
序号 操作 命令 执行结果
3.1 连接Redis,设置学生成绩键值对 redis-cli
hset student.zhangsan English 69 Math 86 Computer 77
hset student.lisi English 55 Math 100 Computer 88 成功设置键值对
3.2 用hgetall命令分别输出zhangsan和lisi的成绩信息 hgetall student.zhangsan
hgetall student.lisi 显示两人的所有成绩
3.3 查询zhangsan的Computer成绩 hget student.zhangsan Computer 显示zhangsan的Computer成绩为77
3.4 修改lisi的Math成绩为95 hset student.lisi Math 95
hget student.lisi Math 成功修改,显示lisi的Math成绩为95 -
Redis Java客户端编程实现(Jedis)
import redis.clients.jedis.Jedis;
public class RedisStudentAPI {
public static void main(String[] args) {
// 连接Redis服务器
Jedis jedis = new Jedis("localhost", 6379);
// (1)添加数据:scofield English:45 Math:89 Computer:100
jedis.hset("student.scofield", "English", "45");
jedis.hset("student.scofield", "Math", "89");
jedis.hset("student.scofield", "Computer", "100");
System.out.println("成功添加scofield的成绩记录");
// (2)获取scofield的English成绩信息
String englishScore = jedis.hget("student.scofield", "English");
System.out.println("scofield的English成绩:" + englishScore);
// 关闭连接
jedis.close();
}
}
编译和运行命令:
javac -cp jedis-3.1.0.jar RedisStudentAPI.java
java -cp .:jedis-3.1.0.jar:commons-pool2-2.7.0.jar RedisStudentAPI
执行结果:
成功添加scofield的成绩记录
scofield的English成绩:45
(四)MongoDB数据库操作
-
MongoDB Shell命令操作
序号 操作 命令 执行结果
4.1 连接MongoDB,创建student集合 mongo
use hadoop_experiment
db.student.insertMany([{"name": "zhangsan", "score": {"English": 69, "Math": 86, "Computer": 77}}, {"name": "lisi", "score": {"English": 55, "Math": 100, "Computer": 88}}]) 成功创建集合并插入数据
4.2 用find()方法输出两个学生的信息 db.student.find() 显示所有学生记录
4.3 查询zhangsan的所有成绩(只显示score列) db.student.find({"name": "zhangsan"}, {"score": 1, "_id": 0}) 显示zhangsan的成绩信息
4.4 修改lisi的Math成绩为95 db.student.updateOne({"name": "lisi"}, {$set: {"score.Math": 95}})
db.student.find({"name": "lisi"}) 成功修改,显示lisi的Math成绩为95 -
MongoDB Java客户端编程实现
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.bson.Document;
public class MongoDBStudentAPI {
public static void main(String[] args) {
// 连接MongoDB服务器
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
MongoDatabase database = mongoClient.getDatabase("hadoop_experiment");
MongoCollection
// (1)添加数据:scofield English:45 Math:89 Computer:100
Document scofieldDoc = new Document("name", "scofield")
.append("score", new Document("English", 45)
.append("Math", 89)
.append("Computer", 100));
collection.insertOne(scofieldDoc);
System.out.println("成功添加scofield的成绩记录");
// (2)获取scofield的所有成绩信息(只显示score列)
Document result = collection.find(Filters.eq("name", "scofield"))
.projection(new Document("score", 1).append("_id", 0))
.first();
System.out.println("scofield的成绩信息:" + result.toJson());
// 关闭连接
mongoClient.close();
}
}
编译和运行命令:
javac -cp mongodb-driver-sync-4.0.5.jar:bson-4.0.5.jar:mongodb-driver-core-4.0.5.jar MongoDBStudentAPI.java
java -cp .:mongodb-driver-sync-4.0.5.jar:bson-4.0.5.jar:mongodb-driver-core-4.0.5.jar MongoDBStudentAPI
执行结果:
成功添加scofield的成绩记录
scofield的成绩信息:{"score": {"English": 45, "Math": 89, "Computer": 100}}
出现的问题及解决方案
数据库 问题 解决方案
MySQL 连接失败 检查MySQL服务是否启动,用户密码是否正确
HBase 创建表失败 确保Hadoop和ZooKeeper已启动,HBase配置正确
Redis 连接失败 检查Redis服务是否启动,端口是否正确
MongoDB 插入数据失败 检查MongoDB服务是否启动,数据库权限是否正确
Java API 编译错误 确保导入了正确的驱动包,classpath设置正确
Java API 运行时错误 检查数据库服务是否正常运行,连接参数是否正确

浙公网安备 33010602011771号