NoSQL和關係資料庫的操作比較
實驗環境
1、作業系統:Linux(建議Ubuntu16.04);
2、Hadoop版本:2.7.1;
3、MySQL版本:5.6;
4、HBase版本:1.1.2;
5、Redis版本:3.0.6;
6、MongoDB版本:3.2.6;
7、JDK版本:1.7或以上版本;
8、Java IDE:Eclipse;
實驗目的:
1、理解四種資料庫(MySQL、HBase、Redis和MongoDB)的概念以及不同點;
2、熟練使用四種資料庫操作常用的Shell命令;
3、熟悉四種資料庫操作常用的Java API。
實驗步驟:
(一) MySQL資料庫操作
學生表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。
根據上面已經設計出的Student表,使用MySQL的JAVA客戶端程式設計實現以下操作:
(1)向Student表中新增如下所示的一條記錄:
scofield |
45 |
89 |
100 |
(2)獲取scofield的English成績資訊
package com.mysql;
import java.sql.*;
public class MysqlTest {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DB = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8&useSSL=false";
static final String USER = "root";
static final String PASSWD = "hadoop";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = 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!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
(二)HBase資料庫操作
學生表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 |
(2)獲取scofield的English成績資訊。
public class HbaseTest {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args) {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/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) {
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();
}
}
}
(三)Redis資料庫操作
Student鍵值對如下:
zhangsan:{ English: 69 Math: 86 Computer: 77 } lisi:{ English: 55 Math: 100 Computer: 88 } |
1. 根據上面給出的鍵值對,完成如下操作:
(1)用Redis的雜湊結構設計出學生表Student(鍵值可以用student.zhangsan和student.lisi來表示兩個鍵值屬於同一個表);
(3)用hgetall命令分別輸出zhangsan和lisi的成績資訊;
(4)用hget命令查詢zhangsan的Computer成績;
(5)修改lisi的Math成績,改為95。
2.根據上面已經設計出的學生表Student,用Redis的JAVA客戶端程式設計(jedis),實現如下操作:
(1)新增資料:English:45 Math:89 Computer:100
該資料對應的鍵值對形式如下:
scofield:{ English: 45 Math: 89 Computer: 100 } |
import java.util.Map;
import redis.clients.jedis.Jedis;
public class test {
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());
}
}
}
(2)獲取scofield的English成績資訊
import redis.clients.jedis.Jedis;
class RedisTest2 {
public static void main(String[] args) {
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集合;
(3)用find()方法輸出兩個學生的資訊;
(4)用find()方法查詢zhangsan的所有成績(只顯示score列);
(5)修改lisi的Math成績,改為95。
2.根據上面已經設計出的Student集合,用MongoDB的Java客戶端程式設計,實現如下操作:
(1)新增資料:English:45 Math:89 Computer:100
與上述資料對應的文件形式如下:
{ “name”: “scofield”, “score”: { “English”: 45, “Math”: 89, “Computer”: 100 } } |
public class MongoTest {
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的所有成績成績資訊(只顯示score列)
public class MongoTest2 {
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")).
projection(new Document("score",1).append("_id", 0)).iterator();
while(cursor.hasNext())
System.out.println(cursor.next().toJson());
}
}