石家庄鐵道大學信息科學與技術學院
實驗報告
2018年----2019年 第一學期
題目: 熟悉常用的 HBase 操作
課程名稱: 大型數據庫應用技術
班 級: 信1605-2班
姓 名: XX 學號: XXXXXXXX
指導教師: XXX
一、實驗內容與完成情況:(實驗具體步驟和實驗截圖說明)
實驗說明:
1、 本次實驗是第三次上機,屬於驗證性實驗。實驗報告上交截止日期為2018年9月29日上午12點之前。
2、 實驗報告命名為:信1605-1班學號姓名實驗三.doc。
實驗環境:
1、 操作系統:Linux(建議Ubuntu16.04);
2、 Hadoop版本:2.7.1;
3、 JDK版本:1.7或以上版本;
4、 Java IDE:Eclipse。
實驗目的:
1、 理解HBase在Hadoop體系結構中的角色;
2、 熟練使用HBase操作常用的Shell命令;
3、 熟悉HBase操作常用的Java API。
實驗步驟:
(一)編程實現以下指定功能,並用 Hadoop 提供的 HBase Shell 命令完成相同任務:
l 列出 HBase 所有的表的相關信息,例如表名;
l 在終端打印出指定的表的所有記錄數據;
l 向已經創建好的表添加和刪除指定的列族或列;
l 清空指定的表的所有記錄數據;
l 統計表的行數。
1.列出 HBase 所有的表的相關信息,例如表名
HBase Shell:List
截圖:
Java API

package homework; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import java.io.IOException; public class Test_1 { public static Configuration configuration; public static Connection connection; public static Admin admin; /** * @param args */ //建立連接 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(); } } /** * 查看已有表 * @throws IOException */ public static void listTables() throws IOException { init(); HTableDescriptor hTableDescriptors[] = admin.listTables(); for(HTableDescriptor hTableDescriptor :hTableDescriptors){ System.out.println(hTableDescriptor.getNameAsString()); } close(); } public static void main(String[] args) { // TODO Auto-generated method stub Test_1 t =new Test_1(); try { System.out.println("以下為Hbase 數據庫中所存的表信息"); t.listTables(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
截圖:
2.在終端打印出指定的表的所有記錄數據;
Hbase shell scan 'student'
截圖:
Java API:

package homework; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import java.io.IOException; import java.util.Scanner; public class Test_2 { public static Configuration configuration; public static Connection connection; public static Admin admin; /** * @param args */ //建立連接 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(); } } /** * 根據表名查找表信息 */ public static void getData(String tableName)throws IOException{ init(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for(Result result:scanner) { showCell((result)); } close(); } /** * 格式化輸出 * @param result */ 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("column Name(列名):"+new String(CellUtil.cloneQualifier(cell))+" "); System.out.println("value:(值)"+new String(CellUtil.cloneValue(cell))+" "); System.out.println(); } } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Test_2 t =new Test_2(); System.out.println("請輸入要查看的表名"); Scanner scan = new Scanner(System.in); String tableName=scan.nextLine(); System.out.println("信息如下:"); t.getData(tableName); } }
截圖:
③.(向已經創建好的表添加和刪除指定的列族或列)
Hbase shell(添加列族): put 'student','95003','Sname','wangjinxuan'
截圖 :
HBase Shell:(刪除列族)
delete 'student' ,’95003’,’Sname:123’
delete 'student' ,’95003’,’Sname’
student為表名,95003為行鍵,Sname為列族名,123為列名
(因為hbase中沒有刪除shell命令直接刪除指定行的列族信息(包括其中的列的信息),所以需要先將所有指定行的列族信息的所有信息,然后使用delete一個一個作刪除)
截圖:
刪除補充
刪除整個行記錄
Deleteall 'student' ,’95003’
student為表名,95003為行鍵
Java API

package homework; import java.io.IOException; import java.util.Scanner; 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.Delete; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; public class Test_3 { public static Configuration configuration; public static Connection connection; public static Admin admin; //建立連接 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(); } } /** * 向某一行的某一列插入數據 * @param tableName 表名 * @param rowKey 行鍵 * @param colFamily 列族名 * @param col 列名(如果其列族下沒有子列,此參數可為空) * @param val 值 * @throws IOException */ public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { init(); 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(); close(); } /** * 根據表名查找表信息 */ public static void getData(String tableName)throws IOException{ init(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for(Result result:scanner) { showCell((result)); } close(); } /** * 格式化輸出 * @param result */ 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("column Name(列名):"+new String(CellUtil.cloneQualifier(cell))+" "); System.out.println("value:(值)"+new String(CellUtil.cloneValue(cell))+" "); System.out.println(); } } /** * 刪除數據 * @param tableName 表名 * @param rowKey 行鍵 * @param colFamily 列族名 * @param col 列名 * @throws IOException */ public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(rowKey.getBytes()); boolean flag2 =true; while(flag2) { System.out.println("請輸入你的選擇 1-刪除列族的所有數據 2-指定列的數據"); Scanner scanner=new Scanner(System.in); String chooseString = scanner.nextLine(); switch (chooseString) { case "1": { //刪除指定列族的所有數據 delete.addFamily(colFamily.getBytes()); table.delete(delete); table.close(); close(); break; } case "2": { //刪除指定列的數據 delete.addColumn(colFamily.getBytes(), col.getBytes()); table.delete(delete); table.close(); close(); break; } default: { System.out.println(" 你的輸入有誤 !!! "); table.close(); close(); break; } } System.out.println(" 你要繼續操作嗎? 是-true 否-false "); flag2=scanner.nextBoolean(); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Test_3 t =new Test_3(); boolean flag =true; while(flag) { System.out.println("------------向已經創建好的表中添加和刪除指定的列簇或列--------------------"); System.out.println(" 請輸入您要進行的操作 1- 添加 2-刪除 "); Scanner scan = new Scanner(System.in); String choose1=scan.nextLine(); switch (choose1) { case "1": { System.out.println("請輸入要添加的表名"); String tableName=scan.nextLine(); System.out.println("請輸入要添加的表的行鍵"); String rowKey=scan.nextLine(); System.out.println("請輸入要添加的表的列簇"); String colFamily=scan.nextLine(); System.out.println("請輸入要添加的表的列名"); String col=scan.nextLine(); System.out.println("請輸入要添加的值"); String val=scan.nextLine(); try { t.insertRow(tableName, rowKey, colFamily, col, val); System.out.println("插入成功:"); t.getData(tableName); } catch (IOException e) { // TODO Auto-generated catch block e.getMessage(); } break; } case "2": { System.out.println("請輸入要刪除的表名"); String tableName=scan.nextLine(); System.out.println("請輸入要刪除的表的行鍵"); String rowKey=scan.nextLine(); System.out.println("請輸入要刪除的表的列簇"); String colFamily=scan.nextLine(); System.out.println("請輸入要刪除的表的列名"); String col=scan.nextLine(); try { System.out.println("----------------------表的原本信息如下---------------------"); t.getData(tableName); System.out.println("____________________________正在執行刪除操作........\n"); t.deleteRow(tableName, rowKey, colFamily, col); System.out.println("____________________________刪除成功_______________\n"); System.out.println("---------------------刪除后 表的信息如下---------------------"); t.getData(tableName); } catch (IOException e) { // TODO Auto-generated catch block e.getMessage(); } break; } default: { System.out.println(" 你的操作有誤 !!! "); break; } } System.out.println(" 你要繼續操作嗎? 是-true 否-false "); flag=scan.nextBoolean(); } System.out.println(" 程序已退出! "); } }
截圖 :
備注:
delete 'student’,’95003’,’Sname’僅能刪除Sname列下的數據,並不能刪除Sname列簇下的別的列名的數據,因為它默認刪除刪除Sname:空下的數據,作字符串相等,並不是包含。暫時依靠兩次刪除來刪除數據。
④.清空指定的表的所有記錄數據
Hbase Shell命令 truncate 'student'
截圖
Java API

package homework; import java.io.IOException; import java.util.Scanner; 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.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; 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.HBaseAdmin; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class Test_4 { public static Configuration configuration; public static Connection connection; public static Admin admin; /** * @param args */ //建立連接 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(); } } /** * 清空制定的表的所有記錄數據 * @param args * @throws IOException */ public static void clearRows(String tableName) throws IOException{ init(); HBaseAdmin admin1=new HBaseAdmin(configuration); HTableDescriptor tDescriptor =admin1.getTableDescriptor(Bytes.toBytes(tableName));//讀取了之前表的表名 列簇等信息,然后再進行刪除操作。 總思想是先將原表結構保留下來,然后進行刪除,再重新依據保存的信息重新創建表。 TableName tablename=TableName.valueOf(tableName); //刪除表 admin.disableTable(tablename); admin.deleteTable(tablename); //重新建表 admin.createTable(tDescriptor); close(); } /** * 根據表名查找表信息 */ public static void getData(String tableName)throws IOException{ init(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for(Result result:scanner) { showCell((result)); } close(); } /** * 格式化輸出 * @param result */ 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("column Name(列名):"+new String(CellUtil.cloneQualifier(cell))+" "); System.out.println("value:(值)"+new String(CellUtil.cloneValue(cell))+" "); System.out.println(); } } public static void main(String[] args) { // TODO Auto-generated method stub Test_4 test_4=new Test_4(); Scanner scan = new Scanner(System.in); System.out.println("請輸入要清空的表名"); String tableName=scan.nextLine(); try { System.out.println("表原來的信息:"); test_4.getData(tableName); test_4.clearRows(tableName); System.out.println("表已清空:"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
截圖:
⑤統計表的行數。
Hbase shell 命令
命令:count 'student'
截圖:
Java API

package homework; import java.io.IOException; import java.util.Scanner; 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.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; public class Test_5 { public static Configuration configuration; public static Connection connection; public static Admin admin; //建立連接 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(); } } public static void countRows (String tableName) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner scanner =table.getScanner(scan); int num = 0; for(Result result = scanner.next();result!=null;result=scanner.next()) { num++; } System.out.println("行數:"+num); scanner.close(); close(); } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Test_5 test_5=new Test_5(); Scanner scan = new Scanner(System.in); System.out.println("請輸入要統計行數的表名"); String tableName=scan.nextLine(); test_5.countRows(tableName); } }
(二)HBase 數據庫操作
(1) 現有以下關系型數據庫中的表和數據,要求將其轉換為適合於 HBase 存儲的表並插入數據:
學生表(Student)
學號(S_No) |
姓名(S_Name) |
性別(S_Sex) |
年齡(S_Age) |
2015001 |
Zhangsan |
male |
23 |
2015003 |
Mary |
female |
22 |
2015003 |
Lisi |
male |
24 |
課程表(Course)
課程號(C_No) |
課程名(C_Name) |
學分(C_Credit) |
123001 |
Math |
2.0 |
123002 |
Computer Science |
5.0 |
123003 |
English |
3.0 |
選課表(SC)
學號(SC_Sno) |
課程號(SC_Cno) |
成績(SC_Score) |
2015001 |
123001 |
86 |
2015001 |
123003 |
69 |
2015002 |
123002 |
77 |
2015002 |
123003 |
99 |
2015003 |
123001 |
98 |
2015003 |
123002 |
95 |
①學生Student表(主鍵的列名是隨機分配的,因此無需創建主鍵列)
創建表: create 'Student','S_No','S_Name','S_Sex','S_Age'
插入數據:
|
插入shell命令 |
第一行數據 |
put 'Student','s001','S_No','2015001' put 'Student','s001','S_Name','Zhangsan' put 'Student','s001','S_Sex','male' put 'Student','s001','S_Age','23' |
第二行數據 |
put 'Student','s002','S_No','2015002' put 'Student','s002','S_Name','Mary' put 'Student','s002','S_Sex','female' put 'Student','s002','S_Age','22' |
第三行數據 |
put 'Student','s003','S_No','2015003' put 'Student','s003','S_Name','Lisi' put 'Student','s003','S_Sex','male' put 'Student','s003','S_Age','24' |
添加數據並查看
添加3個學生
② 課程Course表
創建表:create 'Course','C_No','C_Name','C_Credit'
創建Course表
|
插入shell命令 |
第一行數據 |
put 'Course','c001','C_No','123001' put 'Course','c001','C_Name','Math' put 'Course','c001','C_Credit','2.0'
|
第二行數據 |
put 'Course','c001','C_No','123002' put 'Course','c001','C_Name','Computer Science' put 'Course','c001','C_Credit','5.0'
|
第三行數據 |
put 'Course','c001','C_No','123003' put 'Course','c001','C_Name','English'
put 'Course','c001','C_Credit','3.0'
|
添加數據
添加三個課程
③選課表
創建表:create 'SC','SC_Sno','SC_Cno','SC_Score'
創建表SC
插入數據:
|
插入shell命令 |
第一行數據 |
put 'SC','sc001','SC_Sno','2015001' put 'SC','sc001','SC_Cno','123001' put 'SC','sc001','SC_Score','86'
|
第二行數據 |
put 'SC','sc002','SC_Sno','2015001' put 'SC','sc002','SC_Cno','123003' put 'SC','sc002','SC_Score','69'
|
第三行數據 |
put 'SC','sc003','SC_Sno','2015002' put 'SC','sc003','SC_Cno','123002' put 'SC','sc003','SC_Score','77'
|
第四行數據 |
put 'SC','sc004','SC_Sno','2015002' put 'SC','sc004','SC_Cno','123003' put 'SC','sc004','SC_Score','99'
|
第五行數據 |
put 'SC','sc005','SC_Sno','2015003' put 'SC','sc005','SC_Cno','123001' put 'SC','sc005','SC_Score','98'
|
第六行數據 |
put 'SC','sc006','SC_Sno','2015003' put 'SC','sc006','SC_Cno','123002' put 'SC','sc006','SC_Score','95'
|
插入數據
數據顯示
二、按要求編程。
|
|
1.createTable(String tableName, String[] fields) 創建表,參數 tableName 為表的名稱,字符串數組 fields 為存儲記錄各個字段名稱的數組。要求當 HBase 已經存在名為 tableName 的表的時候,先刪除原有的表,然后再創建新的表。
2.addRecord(String tableName, String row, String[] fields, String[] values) 向表 tableName、行 row(用 S_Name 表示)和字符串數組 fields 指定的單元格中添加對應的數據 values。其中,fields 中每個元素如果對應的列族下還有相應的列限定符的話,用“columnFamily:column”表示。例如,同時向“Math”、“Computer Science”、“English”三列添加成績時,字符串數組 fields 為{“Score:Math”, ”Score:Computer Science”, ”Score:English”},數組 values 存儲這三門課的成績。
3.scanColumn(String tableName, String column) 瀏覽表 tableName 某一列的數據,如果某一行記錄中該列數據不存在,則返回 null。要求當參數 column 為某一列族名稱時,如果底下有若干個列限定符,則要列出每個列限定符代表的列的數據;當參數 column 為某一列具體名稱(例如“Score:Math”)時,只需要列出該列的數據。
4.modifyData(String tableName, String row, String column) 修改表 tableName,行 row(可以用學生姓名 S_Name 表示),列 column 指定的單元格的數據。 5.deleteRow(String tableName, String row) 刪除表 tableName 中 row 指定的行的記錄。
|
|
方式 |
代碼+截圖 |
Java API 命令 (集成了所有功能) |
代碼:
package homework; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; 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.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; 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.Delete; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class Test_Two { public static Configuration configuration; public static Connection connection; public static Admin admin; //建立連接 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(); } } /** * 建表。參數tableName為表的名稱,字符串數組fields為存儲記錄各個域名稱的數組。 * 要求當HBase已經存在名為tableName的表時,先刪除原有的表,然后再 * 創建新的表 field:列族 * @param myTableName 表名 * @param colFamily 列族名 * @throws IOException */ public static void createTable(String tableName,String[] fields) throws IOException { init(); TableName tablename = TableName.valueOf(tableName); if(admin.tableExists(tablename)){ System.out.println("表已存在,將執行刪除原表,重建新表!"); admin.disableTable(tablename); admin.deleteTable(tablename);//刪除原來的表 } HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); for(String str:fields){ HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); System.out.println("表已創建成功"); close(); } /** * 向表 tableName、行 row(用 S_Name 表示)和字符串數組 fields 指定的單元格中 * 添加對應的數據 values。 * 其中,fields 中每個元素如果對應的列族下還有相應的列限定符的話, * 用“columnFamily:column”表示。 * 例如,同時向“Math”、“Computer Science”、“English”三列添加成績時, * 字符串數組 fields 為{“Score:Math”, ”Score:Computer Science”, ”Score:English”}, * 數組values 存儲這三門課的成績。 */ public static 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); } table.close(); close(); } /** * 根據表名查找表信息 */ public static void getData(String tableName)throws IOException{ init(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for(Result result:scanner) { showCell((result)); } close(); } /** * 格式化輸出 * @param result */ 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("column Name(列名):"+new String(CellUtil.cloneQualifier(cell))+" "); System.out.println("value:(值)"+new String(CellUtil.cloneValue(cell))+" "); System.out.println(); } } /** * 瀏覽表 tableName 某一列的數據,如果某一行記錄中該列數據不存在,則返回 null。 * 要求當參數 column 為某一列族名稱時,如果底下有若干個列限定符,則要列出每個列限定符代表的列的數據; * 當參數 column 為某一列具體名稱(例如“Score:Math”)時,只需要列出該列的數據。 * @param tableName * @param column * @throws IOException */ public static 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(); } /** * 修改表 tableName,行 row(可以用學生姓名 S_Name 表示),列 column 指定的單元格的數據。 * @throws IOException */ public static 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:列族下的列名 } table.put(put); table.close(); close(); } /** * 刪除表 tableName 中 row 指定的行的記錄。 * @throws IOException */ public static 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); table.close(); close(); } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Test_Two test_Two = new Test_Two(); boolean flag =true; while(flag) { System.out.println("------------------------------------------------提供以下功能----------------------------------------------"); System.out.println(" 1- createTable(創建表 ,提供表名、列族名) "); System.out.println(" 2-addRecord (向已知表名、行鍵、列簇的表添加值) "); System.out.println(" 3- ScanColumn(瀏覽表 某一列的數據) "); System.out.println(" 4- modifyData(修改某表 某行,某一列,指定的單元格的數據) "); System.out.println(" 5- deleteRow(刪除 某表 某行的記錄) "); System.out.println("------------------------------------------------------------------------------------------------------------------"); Scanner scan = new Scanner(System.in); String choose1=scan.nextLine(); switch (choose1) { case "1": { System.out.println("請輸入要創建的表名"); String tableName=scan.nextLine(); System.out.println("請輸入要創建的表的列族個數"); int Num=scan.nextInt(); String [] fields = new String[Num]; System.out.println("請輸入要創建的表的列族"); /* Scanner scanner = new Scanner(System.in); scanner.next 如不是全局,即會記得上一次輸出。相同地址讀入值時*/ for(int i=0;i< fields.length;i++) { /*BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); fields[i] = in.readLine();*/ /*fields[i]=scan.next(); 因為之前沒有輸入過,所以可以讀入新值*/ scan = new Scanner(System.in); fields[i]=scan.nextLine(); } System.out.println("正在執行創建表的操作"); test_Two.createTable(tableName,fields); break; } case "2": { System.out.println("請輸入要添加數據的表名"); String tableName=scan.nextLine(); System.out.println("請輸入要添加數據的表的行鍵"); String rowKey=scan.nextLine(); System.out.println("請輸入要添加數據的表的列的個數"); int num =scan.nextInt(); String fields[]=new String[num]; System.out.println("請輸入要添加數據的表的列信息 共"+num+"條信息"); for(int i=0;i< fields.length;i++) { BufferedReader in3= new BufferedReader(new InputStreamReader(System.in)); fields[i] = in3.readLine(); /*fields[i]=scan.next(); 因為之前沒有輸入過,所以可以讀入新值*/ } System.out.println("請輸入要添加的數據信息 共"+num+"條信息"); String values[]=new String[num]; for(int i=0;i< values.length;i++) { BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in)); values[i] = in2.readLine(); } System.out.println("原表信息"); test_Two.getData(tableName); System.out.println("正在執行向表中添加數據的操作........\n"); test_Two.addRecord(tableName, rowKey, fields, values); System.out.println("\n添加后的表的信息........"); test_Two.getData(tableName); break; } case "3": { System.out.println("請輸入要查看數據的表名"); String tableName=scan.nextLine(); System.out.println("請輸入要查看數據的列名"); String column=scan.nextLine(); System.out.println("查看的信息如下:........\n"); test_Two.scanColumn(tableName, column); break; } case "4": { System.out.println("請輸入要修改數據的表名"); String tableName=scan.nextLine(); System.out.println("請輸入要修改數據的表的行鍵"); String rowKey=scan.nextLine(); System.out.println("請輸入要修改數據的列名"); String column=scan.nextLine(); System.out.println("請輸入要修改的數據信息 "); String value=scan.nextLine(); System.out.println("原表信息如下:........\n"); test_Two.getData(tableName); System.out.println("正在執行向表中修改數據的操作........\n"); test_Two.modifyData(tableName, rowKey, column, value); System.out.println("\n修改后的信息如下:........\n"); test_Two.getData(tableName); break; } case "5": { System.out.println("請輸入要刪除指定行的表名"); String tableName=scan.nextLine(); System.out.println("請輸入要刪除指定行的行鍵"); String rowKey=scan.nextLine(); System.out.println("原表信息如下:........\n"); test_Two.getData(tableName); System.out.println("正在執行向表中刪除數據的操作........\n"); test_Two.deleteRow(tableName, rowKey); System.out.println("\n刪除后的信息如下:........\n"); test_Two.getData(tableName); break; } default: { System.out.println(" 你的操作有誤 !!! "); break; } } System.out.println(" 你要繼續操作嗎? 是-true 否-false "); flag=scan.nextBoolean(); } System.out.println(" 程序已退出! "); } }
|
截圖1 (CreateTable) |
創建表&列族
查看創建結果
|
截圖2 addRecord(String tableName, String row, String[] fields, String[] values)
|
|
截圖3 scanColumn(String tableName, String column)
|
參數 column 為某一列族名稱時:
當參數 column 為某一列具體名稱
|
截圖4 modifyData(String tableName, String row, String column) |
|
截圖5 deleteRow(String tableName, String row) |
|
二 實驗中出現問題:(說明和截圖)
問題1 |
PleaseHoldException異常(Master is initializing)
|
原因 |
(由於正在操作Hbase時,電腦突然關機,未正常關閉hbase,故導致shell無法正常顯示)如下圖:
|
解決過程: |
先在網上百度了一個教程: https://blog.csdn.net/liulang12580/article/details/77841699 ,可能是因為出錯原因不同,所以使用教程並不能完全解決問題,還導致出了別的問題詳見以下問題。
然后解決以上問題后,又回到了最初的錯誤。
然后找到了和我一樣的情況的博客:https://blog.csdn.net/qq_41665356/article/details/80271562 參考它解決了問題。
① 檢查文件 : hdfs fsck / -locations -blocks -files
出現以下結果:
圖 1
圖 2(刪除一個文件后)
以上黃色的部分表明: 圖 1 有2個文件處於打開狀態,說明當時正在寫文件時斷電了,因此需要刪除這2個文件,才可正常啟動HBase。 圖 2 有1個文件處於打開狀態,說明當時正在寫文件時斷電了,因此需要刪除這1個文件,才可正常啟動HBase。(因為刪除了前一個文件的過程沒截圖,直接從這個文件開始解釋。)
② 查看哪些文件正在打開中: hdfs fsck / -openforwrite
如下圖所示:
③黃色部分即為打開的文件,執行刪除文件:(刪除的文件與你的上一步結果相對應) Hadoop dfs -rm /hbase/WALs/zhaoteng-virtualbox,16201,1539096368516/zhaoteng-virtualbox%2C16201%2C1539096368516.meta.1539096385044 再次檢查: hadoop fsck / -openforwrite
黃色部分沒有文件即可。 然后重啟hbase即解決問題。
|
建議: |
百度糾錯時,盡量找與自己出錯原因相近的教程解決問題,否則會給自己帶來很多麻煩。 |
問題1.1 |
Can't get master address from ZooKeeper; znode data == null
|
解決方法: |
http://www.aboutyun.com/thread-8691-1-1.html
|
問題1.2 |
(pid文件(在stop-all.sh stop-dfs.sh,stop-yarn.sh腳本,發現原理都是通過一個pid文件來停止集群的。這些進程的pid文件默認都是保存在系統的/tmp目錄下面,)不存在。
|
解決方法: |
參考教程: https://blog.csdn.net/YonJarLuo/article/details/78252637
有時也因為Hbase shell意外終止,會出現以上問題,重啟Hbase即可解決。
|
問題2 |
create 建表沒有傳列族的信息
|
解決方法: |
創建表時至少要有一個列簇。 Create創建時,傳1個以上的列族信息
|
補充內容:HBase 常用類介紹。
|
|
舉例說明 |
1.HBaseAdmin 關系:org.apache.hadoop.hbase.client.HBaseAdmin 作用:提供接口關系HBase 數據庫中的表信息 用法:HBaseAdmin admin = new HBaseAdmin(config); 2.HTableDescriptor 關系:org.apache.hadoop.hbase.HTableDescriptor 作用:HTableDescriptor 類包含了表的名字以及表的列族信息 用法:HTableDescriptor htd =new HTableDescriptor(tablename); Htd.addFamily(new HColumnDescriptor(“myFamily”));
詳細數據參考:http://mrpengpengda.iteye.com/blog/1832595
|
個人理解:HBase 關於單元格數據的存取
|
|
具體過程 |
1. 查看表列(單元格中不同版本(不同時間戳)的數據) 因為Hbase某列的VERSIONS是1,就是默認情況下,該列僅會存取一個版本的列數據,當再次插入時,后面的值會覆蓋前面的值。使用get或scan得到的都是最新的數據。在Hbase中對同一條數據的修改或插入都只是put操作,最終看到的都是最新的數據,其它的數據在不同的version中保存。 如何顯示多個版本的值,修改表的列結構,讓Hbase表支持3個VERSIONS的版本列數據,通過設置列簇的版本個數,即列族下面的列也是同樣的版本個數存儲。 alter ‘Score2’,{NAME = > ‘course’,VERSION=>3}
2. 再次查看表結構:
插入三條數據(在同一個單元格)
使用get命令來獲取這一行的數據,發現只返回了最新的一行數據。
獲取多行數據的方法:
查看列族時情況相同:
獲取多行數據的方法:
|
三、未解決問題:(列出沒有解決的問題)
使用以上命令后出現了hbase(main):行數:1 輸入exit,無法退出
Ctrl +SHift+Z:停止Hbase shell ,
具體問題並沒有搞清楚,猜想是過濾器的某處出問題,后續繼續修改。