大數據hadoop學習【10】-----通過學生關係型數據庫表,分別通過HBase Shell命令和JAVA編程設計並創建適宜的HBase學生信息數據表


每一種數據庫的學習,都要學會怎麼樣對關係數據庫表進行聯繫創建,當然,Hbase數據庫也一樣。但是Hbase數據庫也有不同的地方,那就是HBase數據庫是建立在列的基礎上建立的關係,其他關係型數據庫基本都是建立在行上面建立的關係,這就是不同的地方,加入給大家幾張有關係的表,讓我們建立起他們的聯繫,這時候就需要我們對錶進行規劃了,然後創建出他們的關係,本次博客,林君學長主要帶大家瞭解如何通過Hbase Shell命令,進行關係數據庫表的創建

  • 操作系統:ubuntuKylin-16.04
  • hadoop版本:hadoop-2.7.7
  • hbase版本:hbase-1.4.13

一、Hbase關係數據庫表

題目:根據以下關係型數據庫表,使用HBase Shell設計並創建適宜的HBase數據表

1、學生信息表

學號(S_No) 姓名(S_Name) 性別(S_Sex) 年齡(S_Age)
2015001 ZhangSan male 23
2015002 Mary female 22
2015003 LiSi male 24

2、課程信息表

課程號(C_No) 課程名(C_Name) 學分(C_Credit)
123001 Math 2.0
123002 Computer Science 5.0
123003 English 3.0

3、選課信息表

學號(SC_Sno) 課程號(SC_Cno) 成績(SC_Score)
2015001 123001 86
2015001 123003 69
2015002 123002 77
2015002 123003 99
2015003 123001 98
2015003 123002 95

二、對Hbase關係數據庫表進行分析

對於上面的學生信息以及學生選課信息,關係數據庫表已經給大家給出,那麼我們如何進行關係模擬來將學生和課程產生關係,使得我們在查詢學生的時候,能夠自動的也他選擇的課程也一併顯示出現,這就需要我們構建!在很多時候,不是一看到表就直接進行創建,然後進行聯繫,Hbase關係型數據庫和其他數據庫不一樣,所以需要大家進行關係表的規劃,自由明白他們之間的關係,我們才能創建出合適這個關係的表格!

1、規劃思路

1)、上面我們可以看出,學生的選課直接和學生信息和課程信息有關,而直接相關的就是學生學號課程號,第三章選課表也就是根據學生信息和課程信息的綜合而來,學號和課程號是直接的關係紐帶!

2、規劃結果

1)、通過以上關係,那麼我們可以進行綜合,直接將學生信息和他選擇的課程信息進行綜合,進而聯繫在一起,形成下面的綜合關係表;將學生信息表、課程信息表、選課信息表綜合在一起,然後進行建表的規劃:
在這裏插入圖片描述

3、結果分析

1)、通過上面的表格規劃,那麼我們就可以來進行建表,建表的思路就和上面的表格思路一樣,我們需要建立的表格就是學生選課信息表,從中不僅可以查詢學生信息,更可以查詢課程信息,以及每個學生選課的成績!
2)、所有的關係整個爲一個學生信息資源表,studentInformation,該表中包含三個列族名:學習信息、數學選課信息、計算機選課信息、英選課信息,而每個列族名下面包括幾個子列名,例如:
學生信息的子列是:學生學號、學生姓名、性別、年齡
選課信息的子列是:課程號、課程名、學分、成績
由此,我們便可以通過以上關係進行建表了!

三、通過Shell命令創建Hbase關係數據表

1、終端運行hbase數據庫,並進入hbase的shell界面

1)、運行SSH

ssh localhost

在這裏插入圖片描述
2)、運行hadoop

start-dfs.sh

在這裏插入圖片描述
3)、運行hbase

start-hbase.sh

在這裏插入圖片描述
4)、查看是否成功啓動

jps

在這裏插入圖片描述
出現以上7個節點,則表明hbase成功啓動!前提是hbase和hadoop是進行的僞分佈配置哦!

2、創建綜合關係表

1)、創建綜合關係表

create 'studentInformation','Student','Math','ComputerScience','English'

在這裏插入圖片描述
上圖可以看到,綜合關係表我們已經創建成功,接下來,我們需要進行的就是寫入相關數據啦!

3、添加學生信息

1)、添加學號爲2015001的學生信息,學號作爲行鍵

put 'studentInformation','2015001','Student:S_No','2015001'
put 'studentInformation','2015001','Student:S_Name','ZhangSan'
put 'studentInformation','2015001','Student:S_Sex','male'
put 'studentInformation','2015001','Student:S_Age','23'

在這裏插入圖片描述
2)、添加學號爲2015002的學生信息,學號作爲行鍵

put 'studentInformation','2015002','Student:S_No','2015002'
put 'studentInformation','2015002','Student:S_Name','Mary'
put 'studentInformation','2015002','Student:S_Sex','female'
put 'studentInformation','2015002','Student:S_Age','22'

在這裏插入圖片描述
3)、添加學號爲2015003的學生信息,學號作爲行鍵

put 'studentInformation','2015003','Student:S_No','2015003'
put 'studentInformation','2015003','Student:S_Name','LiSi'
put 'studentInformation','2015003','Student:S_Sex','male'
put 'studentInformation','2015003','Student:S_Age','24'

在這裏插入圖片描述

4、添加學生選擇課程的基本信息

1)、添加學號爲:2015001學生的選課信息

由上面總結的綜合關係表可以看出,學號爲2015001的同學選擇了數學和英語課程,因此,我們需要添加的也就是這兩門課程的信息

①.添加該同學選擇的數學課程的基本信息

put 'studentInformation','2015001','Math:C_No','123001'
put 'studentInformation','2015001','Math:C_Name','Math'
put 'studentInformation','2015001','Math:C_Credit','2.0'

在這裏插入圖片描述
②.添加該同學選擇的英語課程的基本信息

put 'studentInformation','2015001','English:C_No','123002'
put 'studentInformation','2015001','English:C_Name','English'
put 'studentInformation','2015001','English:C_Credit','3.0'

在這裏插入圖片描述
2)、添加學號爲:2015002學生的選課信息

由上面總結的綜合關係表可以看出,學號爲2015002的同學選擇了英語和計算機科學課程,因此,我們需要添加的也就是這兩門課程的信息

①.添加該同學選擇的英語課程的基本信息

put 'studentInformation','2015002','English:C_No','123002'
put 'studentInformation','2015002','English:C_Name','English'
put 'studentInformation','2015002','English:C_Credit','3.0'

在這裏插入圖片描述
②.添加該同學選擇的計算機科學課程的基本信息

put 'studentInformation','2015002','ComputerScience:C_No','123003'
put 'studentInformation','2015002','ComputerScience:C_Name','ComputerScience'
put 'studentInformation','2015002','ComputerScience:C_Credit','5.0'

在這裏插入圖片描述
3)、添加學號爲:2015003學生的選課信息

由上面總結的綜合關係表可以看出,學號爲2015003的同學選擇了數學和計算機科學課程,因此,我們需要添加的也就是這兩門課程的信息

①.添加該同學選擇的數學課程的基本信息

put 'studentInformation','2015003','Math:C_No','123001'
put 'studentInformation','2015003','Math:C_Name','Math'
put 'studentInformation','2015003','Math:C_Credit','2.0'

在這裏插入圖片描述
②.添加該同學選擇的計算機科學課程的基本信息

put 'studentInformation','2015003','ComputerScience:C_No','123003'
put 'studentInformation','2015003','ComputerScience:C_Name','ComputerScience'
put 'studentInformation','2015003','ComputerScience:C_Credit','5.0'

在這裏插入圖片描述
現在我們學生所選擇課程的基本信息我們已經添加完成,接下來當然是添加的學生所選擇課程的成績啦!

5、添加學生所選課程的成績

1)、添加學號爲:2015001學生所選課的成績

學號爲2015001的同學選擇了數學和英語課程,所以他只有這兩門課程會有成績

put 'studentInformation','2015001','Math:SC_Score','86'
put 'studentInformation','2015001','English:SC_Score','69'

在這裏插入圖片描述
2)、添加學號爲:2015002學生所選課的成績

學號爲2015002的同學選擇了英語和計算機科學課程,所以他只有這兩門課程會有成績

put 'studentInformation','2015002','English:SC_Score','77'
put 'studentInformation','2015002','ComputerScience:SC_Score','99'

在這裏插入圖片描述
3)、添加學號爲:2015003學生所選課的成績

學號爲2015003的同學選擇了數學和計算機科學課程,所以他只有這兩門課程會有成績

put 'studentInformation','2015003','Math:SC_Score','98'
put 'studentInformation','2015003','ComputerScience:SC_Score','95'

在這裏插入圖片描述

6、學生選課信息查詢

1)、查詢學號爲:2015001同學的學生信息及選課信息和所得的成績

get 'studentInformation','2015001'

在這裏插入圖片描述

從查詢的結果可以看出該同學的基本信息,和選擇了兩門課程:英語、數學;分別的成績爲:69、86

2)、查詢學號爲:2015002同學的學生信息及選課信息和所得的成績

get 'studentInformation','2015002'

在這裏插入圖片描述

從查詢的結果可以看出該同學的基本信息,和選擇了兩門課程:英語、計算機科學;分別的成績爲:77、99

3)、查詢學號爲:2015003同學的學生信息及選課信息和所得的成績

get 'studentInformation','2015003'

在這裏插入圖片描述

從查詢的結果可以看出該同學的基本信息,和選擇了兩門課程:數學、計算機科學;分別的成績爲:98、95!

7、關係Hbase數據庫,結束該實驗

exit
stop-hbase.sh
stop-dfs.sh
exit

在這裏插入圖片描述
由上面的添加數據的操作過程,我們可以知道,通過hbase的shell命令進行數據庫表中數據的添加是一個非常痛苦的過程、因爲只能一列一列的添加,而且還只能添加一列中一個數據,也就是以單元格爲單位,一個一個單元格的添加! 啊,無情!

四、通過JAVA編程,實現學生選課信息表的創建及相關操作

上面我們也可以知道,通過HBase shell命令進行數據的數據不僅麻煩、而且費時,因此,在現實生活中一般是通過JAVA編程語言來實現對錶的創建以及表中數據的操作,接下來,林君學長就帶大家瞭解如何通過java語言實現對剛剛創建的表中數據的操作吧!

1、創建對Hbase數據庫的實現功能類和菜單類

1)、這裏就不給大家介紹怎麼創建了,直接看林君學長的截圖吧!
在這裏插入圖片描述
創建比較簡單,可以根據上面的項目目錄進行創建哦
2)、連接和關閉Hbase數據庫的JAVA代碼,在功能類裏面哦!

//建立連接
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //關閉連接
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

JAVA程序命令就是通過以上建立連接和關閉連接來進行訪問的哦,這是開啓JAVA訪問的第一步,也是必須的一步,每一步操作命令都必須先與Hbase數據庫建立連接,然後再關閉連接

2、編寫創建學生信息表的方法

1)、要求:createTable(String tableName, String[] fields)

創建表,參數tableName爲表的名稱,字符串數組fields爲存儲記錄各個域名稱的數組。要求當HBase已經存在名爲tableName的表的時候,先刪除原有的表,然後再創建新的表。

2)、java程序代碼:

    public void createTable(String myTableName,String[] colFamily) throws IOException {
    	@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
        init();
        TableName tableName = TableName.valueOf(myTableName);
        if(admin.tableExists(tableName)){
            System.out.println("該表已經存在!");
            System.out.println("是否刪除該表重新創建?");
            System.out.println("1、是");
            System.out.println("2、否");
            System.out.print("請輸入你的選擇:");
            int a=in.nextInt();
            switch(a){
            case 1:if (admin.tableExists(tableName)) {
                		admin.disableTable(tableName);
                		admin.deleteTable(tableName);
                		System.out.println("表:"+tableName+"已成功刪除");
            		}
            	   HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            	   for(String str:colFamily){
            		  HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
            		  hTableDescriptor.addFamily(hColumnDescriptor);
            	    }
            	   admin.createTable(hTableDescriptor);
            	   System.out.println("表創建成功!");break;
            case 2:break;
            }
        }else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for(String str:colFamily){
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
            System.out.println("表創建成功!");
        }
        close();
    }

3)、運行截圖:
在菜單函數中調用功能類中的上面的這個方法,進行學生Zhangsan選課信息表的創建:

createTable("StudentScore",new String[]{"student","Math","ComputerScience","English"})

在這裏插入圖片描述
再次創建,提示該表已經存在:
在這裏插入圖片描述

3、編寫添加學生選課信息的方法

1)、要求:addRecord(String tableName, String row, String[] fields, String[] values)

向表tableName、行row(用S_Name表示)和字符串數組files指定的單元格中添加對應的數據values。其中fields中每個元素如果對應的列族下還有相應的列限定符的話,用“columnFamily:column”表示。例如,同時向“Math”、“ComputerScience”、“English”三列添加成績時,字符串數組fields爲{“Score:Math”,”Score:ComputerScience”,”Score:English”},數組values存儲這三門課的成績

2)、java程序代碼:

    public void addRecord(String tableName,String rowKey,String []fields,String [] values) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        for (int i = 0; i < fields.length; i++) {
         Put put = new Put(rowKey.getBytes());
         String [] cols = fields[i].split(":");
         if(cols.length==1){
        	 put.addColumn(cols[0].getBytes(), "".getBytes(), values[i].getBytes());//因爲當輸入的是單列族,split僅讀出一個字符字符串,即cols僅有一個元素
         }
         else {
        	 put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
        }
         table.put(put);
        }
        System.out.println("數據插入成功");
        table.close();
        close();
       }

3)、運行截圖:
在菜單函數中調用功能類中的上面的這個方法,進行學生Zhangsan選課信息的錄入:

addRecord("StudentScore","Zhangsan",new String[]{"student:S_No","student:S_Name","student:S_Sex","student:S_Age","Math:C_No","Math:C_Name","Math:C_Credit","Math:SC_Score","English:C_No","English:C_Name","English:C_Credit","English:SC_Score"}, new String[]{"2015001","Zhangsan","male","23","123001","Math","2.0","86","123003","English","3.0","69"})	

在這裏插入圖片描述
查看是否插入成功:
在這裏插入圖片描述
可以看出,張三的個人信息和選課信息已經插入成功!

4、編寫瀏覽學生選課信息表的方法

1)、要求:scanColumn(String tableName, String column)

瀏覽表tableName某一列的數據,如果某一行記錄中該列數據不存在,則返回null。要求當參數column爲某一列族名稱時,如果底下有若干個列限定符,則要列出每個列限定符代表的列的數據;當參數column爲某一列具體名稱(例如“Score:Math”)時,只需要列出該列的數據。

2)、java程序代碼:

    public void scanColumn (String tableName,String column) throws IOException{
    	init();
    	Table table = connection.getTable(TableName.valueOf(tableName));
    	Scan scan = new Scan();
    	String [] cols = column.split(":");
    	if(cols.length==1){
    		scan.addFamily(Bytes.toBytes(column));
    		}
    	else {
    		scan.addColumn(Bytes.toBytes(cols[0]),Bytes.toBytes(cols[1]));
    		}
    	ResultScanner scanner = table.getScanner(scan);
    	for (Result result = scanner.next(); result !=null;result = scanner.next()) {
    		showCell(result);
    		}
    	table.close();
    	close();
    	}

3)、運行截圖:
在菜單函數中調用功能類中的上面的這個方法,進行學生Zhangsan數學課程信息的查詢:

scanColumn("StudentScore", "Math")

在這裏插入圖片描述
可以看出,張三選擇數學課程的所有信息已經全部列出來了哦!

5、編寫修改學生選課信息的方法

1)、要求:modifyData(String tableName, String row, String column)

修改表tableName,行row(可以用學生姓名S_Name表示),列column指定的單元格的數據。

2)、java程序代碼:

   public void modifyData(String tableName,String rowKey,String column,String value) throws IOException{
    	init();
    	Table table = connection.getTable(TableName.valueOf(tableName));
    	Put put = new Put(rowKey.getBytes());
    	String [] cols = column.split(":");
    	if(cols.length==1){
    		put.addColumn(column.getBytes(),"".getBytes() , value.getBytes());//qualifier:列族下的列名
    	}
    	else { 
    		put.addColumn(cols[0].getBytes(),cols[1].getBytes() , value.getBytes());//qualifier:列族下的列名
    	}
    	System.out.println("信息修改成功!");
    	table.put(put);
    	table.close();
    	close();
   }
   public 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))+" ");
        }
    }

3)、運行截圖:
在菜單函數中調用功能類中的上面的這個方法,進行學生Zhangsan數學成績的修改:

modifyData("StudentScore","Zhangsan","Math:SC_Score","89")

在這裏插入圖片描述
查看是否修改成功:
在這裏插入圖片描述
可以看出,已經修改成功,之前是86分,現在是89分哦!

6、編寫刪除學生選課信息的方法

1)、要求:deleteRow(String tableName, String row)

刪除表tableName中row指定的行的記錄。

2)、java程序代碼:

public void deleteRow(String tableName,String rowKey) throws IOException{
    	init();
    	Table table = connection.getTable(TableName.valueOf(tableName));
    	Delete delete = new Delete(rowKey.getBytes());
    	table.delete(delete);
    	System.out.println("刪除成功!");
    	table.close();
    	close();
    }

3)、運行截圖:
在菜單函數中調用功能類中的上面的這個方法,進行學生Zhangsan一行的信息的刪除:

deleteRow("StudentScore","Zhangsan")

在這裏插入圖片描述
查看是否刪除成功:
在這裏插入圖片描述
同學張三的信息已經被全部刪除了,由於只有一條信息,所以刪除之後就沒有信息啦!小夥伴們可以將上面三個同學的數據都添加進去哦!

7、菜單函數

1)、java程序代碼:

import java.io.IOException;
import java.util.Scanner;
import studentHbaseForJava.StudentForJava;
public class Menu1 {
	public static void main(String[] args) throws IOException{
		StudentForJava h1=new StudentForJava();
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		while(true){
			System.out.println("**********************基於JAVA的Hbase數據庫表及表的基本操作**********************");
			System.out.println("1、創建學生選課表");
			System.out.println("2、向創建的表中添加學生信息");
			System.out.println("3、瀏覽創建的選課表中指定列的數據");
			System.out.println("4、修改指定單元格的數據");
			System.out.println("5、刪除指定行的數據");
			System.out.println("0、退出!");
			System.out.print("請輸入你的選擇:");
			int a=in.nextInt();
			switch(a){
			case 1: h1.createTable("StudentScore",new String[]{"student","Math","ComputerScience","English"});break;
			case 2: h1.addRecord("StudentScore","Zhangsan",new String[]{"student:S_No","student:S_Name","student:S_Sex","student:S_Age","Math:C_No","Math:C_Name","Math:C_Credit","Math:SC_Score","English:C_No","English:C_Name","English:C_Credit","English:SC_Score"}, new String[]{"2015001","Zhangsan","male","23","123001","Math","2.0","86","123003","English","3.0","69"});break;
			case 3: h1.scanColumn("StudentScore", "Math");break;
			case 4: h1.modifyData("StudentScore","Zhangsan","Math:SC_Score","89");break;
			case 5: h1.deleteRow("StudentScore","Zhangsan");break;
			case 0: return;
			}
		}
	}
}

2)、運行截圖:
在這裏插入圖片描述
通過以上的關係數據庫的分析步驟,林君學長就將Hbase關係數據庫的分析分享給大家了,希望大家可以花時間下去消化一下,假如不是學生與課程的關係數據庫變爲教師與授課的關係數據庫表又應該怎麼分析;假如關係表不只有這個三個,而是有更過的關係表,我們又應該怎麼進行分析,這都可以幫助自己加強自己的思維哦!
對以上分析有不懂的小夥伴,記得評論區留言,林君學長看到會爲大家解答的,這個學長不太冷!
陳一月的又一天編程歲月^ _ ^

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章