1. 程式人生 > >linux中mysql,mongodb,redis,hbase數據庫操作

linux中mysql,mongodb,redis,hbase數據庫操作

tdi into nero nts sqlt tst ktr iterator list

1.實驗內容與完成情況:(實驗具體步驟和實驗截圖說明)
(一) 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

源代碼:
package com.mysql;

import java.sql.*;

public class MysqlTest {
   static final String driver="com.mysql.jdbc.Driver";
   static final String DB="jdbc:mysql://localhost/test1";
   static
final String user="root"; static final String password="wangli"; public static void main(String[] args) { Connection conn=null; Statement stmt=null; try { Class.forName(driver); conn=DriverManager.getConnection(DB,user,password); stmt=conn.createStatement(); String sql
="insert into Student values(‘scofied‘,45,89,100)"; stmt.executeUpdate(sql); System.out.println("插入成功!"); } catch (SQLException | ClassNotFoundException 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) { e.printStackTrace(); } } } } } 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/test1"; static final String user="root"; static final String password="wangli"; public static void main(String[] args) { Connection conn=null; Statement stmt=null; ResultSet rs=null; try { Class.forName(driver); conn=DriverManager.getConnection(DB,user,password); stmt=conn.createStatement(); String sql="select Name,English from Student where Name=‘scofied‘ "; 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.getString(2)); } System.out.println("輸出完成!"); } catch (SQLException | ClassNotFoundException 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) { 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

源代碼:
package hbase_test;
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 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();
            insertRow("Student","scofield","score","English","45");
            insertRow("Student","scofield","score","Math","89");
            insertRow("Student","scofield","score","Computer","100");
            System.out.println("插入成功!");
        } 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) {
        try {
            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();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void close() {

        try {
            if (admin != null) {
                admin.close();
            }
            if(null!=connection) {
                connection.close();
            }    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
2)獲取 scofield 的 English 成績信息。
源代碼:
package hbase_test;

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 HBaseTest2 {
    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();
            getData("Student","scofield","score","English");
            System.out.println("輸出完成!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        close();
    }

    public static void getData(String tableName, String rowKey, String colFamily, String col) {
        try {
            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();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private 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.cloneValue(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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

(三) Redis 數據庫操作
Student 鍵值對如下:
zhangsan:{
English: 69
Math: 86
Computer: 77
}
lisi:{
English: 55
Math: 100
Computer: 881. 根據上面給出的鍵值對, 完成如下操作:
(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
(四) 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 列);
(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.mongo;

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 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 列) 
源代碼:

package com.mongo;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

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

linux中mysql,mongodb,redis,hbase數據庫操作