Big Data Technology Principles and Application Experiment 3 - Comparison of NoSQL and Relational Database Operations

1. Purpose of the experiment

(1) Understand the concepts and differences of the four databases (MySQL, HBase, Redis, and MongoDB);
(2) Proficiency in using the Shell commands commonly used in the four database operations;
(3) Familiar with the Java API commonly used in the four database operations.

2. Experimental environment

(1) Linux operating system (CentOS7.5)
(2) VMware Workstation Pro 15.5
(3) Remote terminal tool Xshell7
(4) Xftp7 transmission tool
(5) Hadoop version: 3.1.3;
(6) HBase version: 2.2.2 ;
(7) JDK version: 1.8;
(8) Java IDE: Idea.
(9) MySQL version: 5.7;
(10) Redis version: 2.8.9;
(11) MongoDB version: 4.2.0;
(12) JDK version: 1.8;

3. Experimental content

(1) MySQL database operation

The student table is shown in 14-7.
insert image description here

1. According to the Student table given above, complete the following operations in the MySQL database:

(1) Create the Student table in MySQL and enter the data;
the SQL statement to create the Student table is as follows:

CREATE TABLE student(
    NAME VARCHAR(30) NOT NULL,
    English TINYINT UNSIGNED NOT NULL,
    Math TINYINT UNSIGNED NOT NULL,
    Computer TINYINT UNSIGNED NOT NULL
);

The SQL statement to insert two records into the Student table is as follows:

insert into student values("zhangsan",69,86,77);
insert into student values("lisi",55,100,88);

(2) Use SQL statements to output all records in the Student table;
the SQL statement to output all records in the Student table is as follows:

select * from student;

The screenshot of the result after the execution of the above SQL statement is shown in the figure.
insert image description here

(3) Query zhangsan's computer score;
the SQL statement to query zhangsan's computer score is as follows:

select name , Computer from student where name = "zhangsan";

The screenshot of the result after the above statement is executed is shown in the figure.
insert image description here

(4) Modify lisi's Math score to 95.
The SQL statement to modify the Math result of lisi is as follows:

update student set Math=95 where name="lisi";

The screenshot of the execution result of the above SQL statement is shown in the figure.
insert image description here

2. Use MySQL's JAVA client programming to achieve the following operations:

(1) Add a record as shown below to the Student table:
insert image description here
The Java code for adding the above record to the Student table is as follows:

package com.xusheng.nosql.mysql;
import java.sql.*;
public class mysql_test {
    
    

    /**
     * @param xusheng
     */
    //JDBC DRIVER and DB
    static final String  DRIVER="com.mysql.jdbc.Driver";
    static final String DB="jdbc:mysql://localhost/student?useSSL=false";
    //Database auth
    static final String USER="root";
    static final String PASSWD="root";

    public static void main(String[] args) {
    
    
        // TODO Auto-generated method stub
        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) {
    
    
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (SQLException e) {
    
    
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
    
    
            if(stmt!=null)
                try {
    
    
                    stmt.close();
                } catch (SQLException e) {
    
    
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if(conn!=null)
                try {
    
    
                    conn.close();
                } catch (SQLException e) {
    
    
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
}

insert image description here
insert image description here

(2) Get the English score information of scofield
The Java code to get the English score information of scofield is as follows:

package com.xusheng.nosql.mysql;
import java.sql.*;
public class mysql_qurty {
    
    

    /**
     * @param args
     */
    //JDBC DRIVER and DB
    static final String  DRIVER="com.mysql.jdbc.Driver";
    static final String DB="jdbc:mysql://localhost/student?useSSL=false";
    //Database auth
    static final String USER="root";
    static final String PASSWD="root";

    public static void main(String[] args) {
    
    
        // TODO Auto-generated method stub
        Connection 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="select name,English from student where name='scofield' ";
            //获得结果集
            rs=stmt.executeQuery(sql);
            System.out.println("name"+"\t\t"+"English");
            while(rs.next())
            {
    
    
                System.out.print(rs.getString(1)+"\t\t");
                System.out.println(rs.getInt(2));
            }
        } catch (ClassNotFoundException e) {
    
    
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (SQLException e) {
    
    
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
    
    
            if(rs!=null)
                try {
    
    
                    rs.close();
                } catch (SQLException e1) {
    
    
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            if(stmt!=null)
                try {
    
    
                    stmt.close();
                } catch (SQLException e) {
    
    
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if(conn!=null)
                try {
    
    
                    conn.close();
                } catch (SQLException e) {
    
    
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
}

insert image description here

(2) HBase database operation

The student table Student is shown in Table 14-8.
insert image description here

1. According to the information in the student table Student given above, perform the following operations:

(1) Use the Hbase Shell command to create the student table Student;
the command to create the Student table is as follows:

create 'student','score'

The command to insert the above table data into the Student table is as follows:

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'

The screenshot of the execution result of the above command is shown in the figure.
insert image description here

(2) Use the scan command to browse the relevant information of the Student table;
the command to browse the relevant information of the Student table with the scan command is as follows:

scan 'student'

The screenshot of the execution result of the above command is shown in the figure.
insert image description here
(3) Query zhangsan's computer score;
the command to query zhangsan's computer score is as follows:

get 'student','zhangsan','score:Computer'

The screenshot of the execution result of the above command is shown in the figure.
insert image description here

(4) Modify lisi's Math score to 95.
The command to modify the Math score of lisi is as follows:

put 'student','lisi','score:Math','95'

The screenshot of the execution result of the above command is shown in the figure.
insert image description here

2. Use HBase API programming to achieve the following operations:

(1) Add data: English: 45 Math: 89 Computer: 100
insert image description here
The Java code for adding data is as follows:

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://hadoop102:8020/HBase");
        configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");

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

After executing the above code, you can use the scan command to output the database data to check whether the insertion is successful. The screenshot of the execution result is shown in the figure.
insert image description here

(2) Obtain the English score information of scofield.
The Java code is as follows:

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.rootdir","hdfs://localhost:9000/hbase");
        //configuration.set("hbase.rootdir","hdfs://hadoop102:8020/HBase");
        configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");

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

The above code can be executed in IDEA, and the following information will be output in the console:
insert image description here

(3) Redis database operation

The Student key-value pair is as follows:

zhangsan:{
			English: 69
			Math: 86
			Computer: 77
lisi:{
			English: 55
			Math: 100
			Computer: 88

1. According to the key-value pair given above, complete the following operations:

(1) Use the hash structure of Redis to design the student table Student (the key value can use student.zhangsan and student.lisi to indicate that the two key values ​​belong to the same table); the command to
insert the above key-value pair is as follows:

hset student.zhangsan English 69
hset student.zhangsan Math 86
hset student.zhangsan Computer 77
hset student.lisi English 55
hset student.lisi Math 100
hset student.lisi Computer 88 

(2) Use the hgetall command to output the score information of zhangsan and lisi respectively;
the command to query the score information of zhangsan is as follows:

hgetall student.zhangsan

The screenshot of the result of executing the command is shown in the figure
insert image description here

The command to query lisi score information is as follows:

hgetall student.lisi

The screenshot of the result of executing this command is shown in the figure:
insert image description here

(3) Use the hget command to query zhangsan's Computer score;
the command to query zhangsan's Computer score is as follows:

hget student.zhangsan Computer

The screenshot of the result of executing this command is as follows:
insert image description here

(4) Modify lisi's Math score to 95.
The command to modify the Math score of lisi is as follows:

hset student.lisi Math 95

The screenshot of the result of executing this command is shown in the figure:
insert image description here

2. Use Redis's JAVA client programming (jedis) to implement the following operations:

(1) Add data: English: 45 Math: 89 Computer: 100
The key-value pairs corresponding to the data are as follows:

scofield:{
			English: 45
			Math: 89
			Computer: 100

The Java code to complete the operation of adding data is as follows:

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

When the program is executed in idea, the screenshot of the information output on the idea console is shown in the figure:
insert image description here

(2) Get the English score information of scofield
The Java code to get the English score information of scofield is as follows:

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

After executing the above code in idea, a screenshot of the information output on the idea console is shown in the figure:
insert image description here

(4) MongoDB database operation

The Student document is as follows:

{
    
    
	“name”: “zhangsan”,
	“score”: {
    
    
				“English”: 69,
				“Math”: 86,
				“Computer”: 77
	}
}
{
    
    
	“name”: “lisi”,
	“score”: {
    
    
				“English”: 55,
				“Math”: 100,
				“Computer”: 88
	}
}

1. According to the document given above, complete the following operations:

(1) Design the student collection with MongoDB Shell;
first, switch to the student collection, the command is as follows:

use student

Second, define an array containing the above two documents, the command is as follows:

var stus=[
{
    
    "name":"zhangsan","scores":{
    
    "English":69,"Math":86,"Computer":77}},                    {
    
    "name":"lisi","score":{
    
    "English":55,"Math":100,"Computer":88}} ]

Finally, call the following command to insert into the database:

db.student.insert(stus)

The screenshots of the above commands and their execution results are shown in the figure:
insert image description here
(2) Use the find() method to output the information of two students;
the command to use the find() method to output the information of two students is as follows:

db.student.find().pretty()

A screenshot of the above command and its execution result is shown in the figure:
insert image description here

(3) Use the find() method to query all the scores of zhangsan (only the score column is displayed); the
command to query all the scores of zhangsan with the find function is as follows:

db.student.find({
    
    "name":"zhangsan"},{
    
    "_id":0,"name":0})

A screenshot of the above command and its execution result is shown in the figure:
insert image description here

(4) Modify lisi's Math score to 95.
The command to modify the Math score of lisi is as follows:

db.student.update({
    
    "name":"lisi"}, {
    
    "$set":{
    
    "score.Math":95}} )

A screenshot of the above command and its execution result is shown in the figure:
insert image description here

2. Use Java client programming of MongoDB to realize the following operations:

(1) Add data: English: 45 Math: 89 Computer: 100
The document format corresponding to the above data is as follows:

{
    
    
		“name”: “scofield”,
		“score”: {
    
    
					“English”: 45,
					“Math”: 89,
					“Computer”: 100
		}
}

The Java code is as follows:

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("文档插入成功");
    }
}

insert image description here
You can use the find() method to verify whether the data has been successfully inserted into the MongoDB database. The screenshot of the specific command and its execution result is shown in the figure:
insert image description here

(2) Obtain all the score information of scofield (only the score column is displayed)
Java code is as follows:

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

insert image description here

Four. Experience

HBase Definition
HBase is a distributed, scalable NoSQL database that supports massive data storage.
HBase data model
Logically, the HBase data model is very similar to a relational database. Data is stored in a table with rows and columns.
But from the perspective of HBase's underlying physical storage structure (KV), HBase is more like a multi-dimensional map.

Guess you like

Origin blog.csdn.net/m0_52435951/article/details/124896446
Recommended