.實驗內容與完成情況:(實驗具體步驟和實驗截圖說明) (一)編程實現以下指定功能,並用 Hadoop 提供的 HBase Shell 命令完成相同任務: (1) 列出 HBase 所有的表的相關信息,例如表名; 列出所有數據表: 列出表的結構 查詢表是否存在 查詢表是否可用
packagecn.wl.edu.hbase; import java.io.IOException; public class ListTables { public static Configuration configuration; public static Connection connection; public static Admin admin; public static void listTables() throws IOException { init(); HTableDescriptor[] hTableDescriptors = admin.listTables(); for (HTableDescriptor hTableDescriptor : hTableDescriptors) { System.out.println("table name:" + hTableDescriptor.getNameAsString()); } close(); } 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 (connection != null) { connection.close(); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { try { listTables(); } catch (IOException e) { e.printStackTrace(); } } } (2)在終端打印出指定的表的所有記錄數據;
源代碼: package cn.wl.edu.hbase; 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.*; import java.io.IOException; public class ListTableData { public static Configuration configuration; public static Connection connection; public static Admin admin; 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){ printRecoder(result); } System.out.println(“finish!”); close(); } public static void printRecoder(Result result)throws IOException{ for(Cell cell:result.rawCells()){ System.out.println("行鍵:"+new String(CellUtil.cloneRow(cell))); System.out.print("列簇: " + new String(CellUtil.cloneFamily(cell))); System.out.print(" 列: " + new String(CellUtil.cloneQualifier(cell))); System.out.print(" 值: " + new String(CellUtil.cloneValue(cell))); System.out.println("時間戳: " + cell.getTimestamp()); } } 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 (connection != null) { connection.close(); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { try { getData("person"); } catch (IOException e) { e.printStackTrace(); } } } (3) 向已經創建好的表添加和刪除指定的列族或列; 添加數據
源代碼: package cn.wl.edu.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 InsertRow { public static Configuration configuration; public static Connection connection; public static Admin admin; 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); System.out.println("insert finish!"); table.close(); } 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 (connection != null) { connection.close(); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { try { insertRow("student","s002","score","math","100"); } catch (IOException e) { e.printStackTrace(); } } } 刪除數據
源代碼: package cn.wl.edu.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.*; import org.apache.hadoop.hbase.util.Bytes; public class DeleteRow { public static Configuration configuration; public static Connection connection; public static Admin admin; 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()); delete.addFamily(Bytes.toBytes(colFamily)); delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col)); table.delete(delete); System.out.println("delete successful!"); table.close(); close(); } 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 main(String[] args) { try { deleteRow("student", "s002", "score", "math"); } catch (IOException e) { e.printStackTrace(); } } } (3)清空指定的表的所有記錄數據;
源代碼: package cn.wl.edu.hbase; 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 java.io.IOException; public class TruncateTable { public static Configuration configuration; public static Connection connection; public static Admin admin; public static void clearRows(String tableName) throws IOException { init(); TableName tablename = TableName.valueOf(tableName); admin.disableTable(tablename); admin.truncateTable(tablename, false); System.out.println("delete table successful!"); close(); } 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 main(String[] args) { try { clearRows("student"); } catch (IOException e) { e.printStackTrace(); } } } (5) 統計表的行數。
源程序: package cn.wl.edu.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import java.io.IOException; public class CountTable { public static Configuration configuration; public static Connection connection; public static Admin admin; public static void countTable(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(); } 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 main(String[] args) { try { countTable("student"); } catch (IOException e) { e.printStackTrace(); } } } (二) 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

2.請編程實現以下功能: (1) createTable(String tableName, String[] fields) 創建表,參數 tableName 為表的名稱,字符串數組 fields 為存儲記錄各個字段名稱的數組。 要求當 HBase 已經存在名為 tableName 的表的時候,先刪除原有的表,然后再創建新的表。 package cn.wl.edu.hbase; import org.apache.hadoop.conf.Configuration; 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 java.io.IOException; public class CreateTable { public static Configuration configuration; public static Connection connection; public static Admin admin; public static void createTable(String tableName, String[] fields) throws IOException { init(); TableName tablename = TableName.valueOf(tableName); if (admin.tableExists(tablename)) { System.out.println("table is exists!"); } HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename); for (String str : fields) { HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); close(); } 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 main(String[] args) { String[] fields = { "Score" }; try { createTable("person", fields); } catch (IOException e) { e.printStackTrace(); } } } (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 存儲這三門課的成績。 源代碼: package cn.wl.edu.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import java.io.IOException; public class addRecord { public static Configuration configuration; public static Connection connection; public static Admin admin; public static void addRecord(String tableName, String row, 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(row.getBytes()); String[] cols = fields[i].split(":"); put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes()); table.put(put); } table.close(); close(); } 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 main(String[] args) { String[] fields = {"Score:Math", "Score:Computer Science", "Score:English"}; String[] values = {"99", "80", "100"}; try { addRecord("person", "Score", fields, values); } catch (IOException e) { e.printStackTrace(); } System.out.println("finish!"); } } (3) scanColumn(String tableName, String column) 瀏覽表 tableName 某一列的數據,如果某一行記錄中該列數據不存在,則返回 null。要求 當參數 column 為某一列族名稱時,如果底下有若干個列限定符,則要列出每個列限定符代表 的列的數據;當參數 column 為某一列具體名稱(例如“Score:Math”)時,只需要列出該列的 數據。 源代碼: package cn.wl.edu.hbase; 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.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class ScanColumn { public static Configuration configuration; public static Connection connection; public static Admin admin; public static void scanColumn(String tableName, String column) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); scan.addFamily(Bytes.toBytes(column)); ResultScanner scanner = table.getScanner(scan); for (Result result = scanner.next(); result != null; result = scanner.next()) { showCell(result); } System.out.println("finish!"); table.close(); 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 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 main(String[] args) { try { scanColumn("person", "Score"); } catch (IOException e) { e.printStackTrace(); } } } (4) modifyData(String tableName, String row, String column) 修改表 tableName,行 row(可以用學生姓名 S_Name 表示),列 column 指定的單元格的 數據。 源代碼: package cn.wl.edu.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import java.io.IOException; public class ModifyData { public static long ts; public static Configuration configuration; public static Connection connection; public static Admin admin; public static void modifyData(String tableName, String row, String column, String val) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(row.getBytes()); Scan scan = new Scan(); ResultScanner resultScanner = table.getScanner(scan); for (Result r : resultScanner) { for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) { ts = cell.getTimestamp(); } } put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes()); table.put(put); System.out.println("modify successful!"); table.close(); close(); } 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 main(String[] args) { try { modifyData("person", "Score", "Math", "100"); } catch (IOException e) { e.printStackTrace(); } } } (5) deleteRow(String tableName, String row) 刪除表 tableName 中 row 指定的行的記錄。 源代碼: package cn.wl.edu.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.*; import org.apache.hadoop.hbase.util.Bytes; public class DeleteRow { public static Configuration configuration; public static Connection connection; public static Admin admin; 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()); delete.addFamily(Bytes.toBytes(colFamily)); delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col)); table.delete(delete); System.out.println("delete successful!"); table.close(); close(); } 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 main(String[] args) { try { deleteRow("student", "s002", "score", "math"); } catch (IOException e) { e.printStackTrace(); } } } 2.實驗中出現問題:(說明和截圖) Eclipse運行大多數程序,運行時間特別長,既不報錯也不顯示結果。 3.解決方案:(列出遇到的問題和解決辦法,列出沒有解決的問題): 再次對照查看hbase的安裝配置教程,發現自己少了一個步驟。 在hbase-env.sh文件中添加了這些路徑之后就好了。