實(shí)驗(yàn)4熟悉常用的HBase操作_第1頁(yè)
實(shí)驗(yàn)4熟悉常用的HBase操作_第2頁(yè)
實(shí)驗(yàn)4熟悉常用的HBase操作_第3頁(yè)
實(shí)驗(yàn)4熟悉常用的HBase操作_第4頁(yè)
實(shí)驗(yàn)4熟悉常用的HBase操作_第5頁(yè)
已閱讀5頁(yè),還剩13頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上實(shí)驗(yàn)4熟悉常用的HBase操作姓名:包生友 專(zhuān)業(yè)年級(jí):軟件143 學(xué)號(hào):1. 實(shí)驗(yàn)?zāi)康?. 理解HBase在Hadoop體系結(jié)構(gòu)中的角色;2. 熟練使用HBase操作常用的Shell命令;3. 熟悉HBase操作常用的Java API。2. 實(shí)驗(yàn)環(huán)境操作系統(tǒng):LinuxHadoop版本:2.6.0或以上版本HBase版本:1.1.2或以上版本JDK版本:1.6或以上版本Java IDE:Eclipse3. 實(shí)驗(yàn)內(nèi)容和完成情況1. 編程實(shí)現(xiàn)以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任務(wù):(完整可執(zhí)行代碼見(jiàn) 代碼/QuestionOne.jav

2、a)(1)列出HBase所有的表的相關(guān)信息,例如表名;Shell:List圖1 列出HBase所有表的相關(guān)信息編程:/(1)列出HBase所有的表的相關(guān)信息,例如表名、創(chuàng)建時(shí)間等public static void listTables() throws IOException init();/建立連接 HTableDescriptor hTableDescriptors = admin.listTables(); for(HTableDescriptor hTableDescriptor :hTableDescriptors) System.out.println("表名:&quo

3、t;+hTableDescriptor.getNameAsString(); close();/關(guān)閉連接(2)在終端打印出指定的表的所有記錄數(shù)據(jù);Shell:scan 's1'圖2 打印指定表的所有記錄數(shù)據(jù)編程:/(2)在終端打印出指定的表的所有記錄數(shù)據(jù)public static void getData(String tableName)throws IOException init(); Table table = connection.getTable(TableName.valueOf(tableName); Scan scan = new Scan(); Result

4、Scanner scanner = table.getScanner(scan); for (Result result:scanner) printRecoder(result); close();/打印一條記錄的詳情public static void printRecoder(Result result)throws IOException for(Cell cell:result.rawCells() System.out.print("行健: "+new String(CellUtil.cloneRow(cell); System.out.print("

5、列簇: "+new String(CellUtil.cloneFamily(cell); System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell); System.out.print(" 值: "+new String(CellUtil.cloneValue(cell); System.out.println("時(shí)間戳: "+cell.getTimestamp(); (3)向已經(jīng)創(chuàng)建好的表添加和刪除指定的列族或列;p.s:此題請(qǐng)先在Shell中創(chuàng)建s1作

6、為示例表: create 's1','score'a)在s1表,添加數(shù)據(jù):Shell: put 's1','zhangsan','score:Math','69'圖3 給s1添加數(shù)據(jù)編程:/向表添加數(shù)據(jù)public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException init(); Table table = connection

7、.getTable(TableName.valueOf(tableName); Put put = new Put(rowKey.getBytes(); put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes(); table.put(put); table.close(); close();insertRow("s1",'zhangsan','score','Math','69')b)在s1表,刪除指定的列:Shell:delete &

8、#39;s1','zhangsan','score:Math'圖4 刪除數(shù)據(jù)編程:/刪除數(shù)據(jù)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(); /刪除指定列族

9、delete.addFamily(Bytes.toBytes(colFamily); /刪除指定列 delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col); table.delete(delete); table.close(); close();deleteRow("s1",'zhangsan','score','Math')(4)清空指定的表的所有記錄數(shù)據(jù);Shell:truncate 's1'圖5 清空指定表的所有記錄數(shù)據(jù)編程:/(4)清空

10、指定的表的所有記錄數(shù)據(jù)public static void clearRows(String tableName)throws IOException init(); TableName tablename = TableName.valueOf(tableName); admin.disableTable(tablename); admin.deleteTable(tablename); HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); admin.createTable(hTableDescriptor

11、); close();(5)統(tǒng)計(jì)表的行數(shù)。Shell:count 's1'圖6 統(tǒng)計(jì)表的行數(shù)編程:/(5)統(tǒng)計(jì)表的行數(shù)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

12、 result = scanner.next();result!=null;result=scanner.next() num+; System.out.println("行數(shù):"+ num); scanner.close(); close();2. 現(xiàn)有以下關(guān)系型數(shù)據(jù)庫(kù)中的表和數(shù)據(jù),要求將其轉(zhuǎn)換為適合于HBase存儲(chǔ)的表并插入數(shù)據(jù):學(xué)生表(Student)學(xué)號(hào)(S_No)姓名(S_Name)性別(S_Sex)年齡(S_Age)Zhangsanmale23Maryfemale22Lisimale24課程表(Course)課程號(hào)(C_No)課程名(C_Name)學(xué)分(C_Cr

13、edit)Math2.0Computer Science5.0English3.0 選課表(SC)學(xué)號(hào)(SC_Sno)課程號(hào)(SC_Cno)成績(jī)(SC_Score)866977999895 學(xué)生Student表p.s:主鍵的列名是隨機(jī)分配的,因此無(wú)需創(chuàng)建主鍵列創(chuàng)建表:create 'Student','S_No','S_Name','S_Sex','S_Age'圖7 創(chuàng)建Student表插入數(shù)據(jù):插入數(shù)據(jù)shell命令第一行數(shù)據(jù)put 'Student','s001','S_

14、No',''put 'Student','s001','S_Name','Zhangsan'put 'Student','s001','S_Sex','male'put 'Student','s001','S_Age','23'第二行數(shù)據(jù)put 'Student','s002','S_No',''put 'St

15、udent','s002','S_Name','Mary'put 'Student','s002','S_Sex','female'put 'Student','s002','S_Age','22'第三行數(shù)據(jù)put 'Student','s003','S_No',''put 'Student','s003','

16、S_Name','Lisi'put 'Student','s003','S_Sex','male'put 'Student','s003','S_Age','24'圖8 添加數(shù)據(jù)并查看圖9 添加3個(gè)學(xué)生 課程Course表創(chuàng)建表:create 'Course','C_No','C_Name','C_Credit'圖10 創(chuàng)建Course表插入數(shù)據(jù):插入數(shù)據(jù)shell命令第一行數(shù)據(jù)p

17、ut 'Course','c001','C_No',''put 'Course','c001','C_Name','Math'put 'Course','c001','C_Credit','2.0'第二行數(shù)據(jù)put 'Course','c002','C_No',''put 'Course','c002',

18、9;C_Name','Computer'put 'Course','c002','C_Credit','5.0'第三行數(shù)據(jù)put 'Course','c003','C_No',''put 'Course','c003','C_Name','English'put 'Course','c003','C_Credit','3.0&

19、#39;圖11 添加數(shù)據(jù)圖12 添加3個(gè)課程 選課表創(chuàng)建表:create 'SC','SC_Sno','SC_Cno','SC_Score'圖13 創(chuàng)建表SC插入數(shù)據(jù):插入數(shù)據(jù)shell命令第一行數(shù)據(jù)put 'SC','sc001','SC_Sno',''put 'SC','sc001','SC_Cno',''put 'SC','sc001','SC_Score&#

20、39;,'86'第二行數(shù)據(jù)put 'SC','sc002','SC_Sno',''put 'SC','sc002','SC_Cno',''put 'SC','sc002','SC_Score','69'第三行數(shù)據(jù)put 'SC','sc003','SC_Sno',''put 'SC','sc003

21、9;,'SC_Cno',''put 'SC','sc003','SC_Score','77'第四行數(shù)據(jù)put 'SC','sc004','SC_Sno',''put 'SC','sc004','SC_Cno',''put 'SC','sc004','SC_Score','99'第五行數(shù)據(jù)put 'SC&

22、#39;,'sc005','SC_Sno',''put 'SC','sc005','SC_Cno',''put 'SC','sc005','SC_Score','98'第六行數(shù)據(jù)put 'SC','sc006','SC_Sno',''put 'SC','sc006','SC_Cno',''put

23、 'SC','sc006','SC_Score','95'圖14 插入數(shù)據(jù)圖15 數(shù)據(jù)顯示圖16 QuestionOne運(yùn)行后控制臺(tái)消息同時(shí),請(qǐng)編程完成以下指定功能:(完整可執(zhí)行代碼見(jiàn) 代碼/QuestionTwo.java)(1)createTable(String tableName, String fields)創(chuàng)建表,參數(shù)tableName為表的名稱(chēng),字符串?dāng)?shù)組fields為存儲(chǔ)記錄各個(gè)域名稱(chēng)的數(shù)組。要求當(dāng)HBase已經(jīng)存在名為tableName的表的時(shí)候,先刪除原有的表,然后再創(chuàng)建新的表。代碼:public static

24、 void createTable(String tableName,String fields) throws IOException init(); TableName tablename = TableName.valueOf(tableName); if(admin.tableExists(tablename) System.out.println("table is exists!"); admin.disableTable(tablename); admin.deleteTable(tablename);/刪除原來(lái)的表 HTableDescriptor hTab

25、leDescriptor = new HTableDescriptor(tablename); for(String str:fields) HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str); hTableDescriptor.addFamily(hColumnDescriptor); admin.createTable(hTableDescriptor); close();(2)addRecord(String tableName, String row, String fields, String values

26、)向表tableName、行row(用S_Name表示)和字符串?dāng)?shù)組files指定的單元格中添加對(duì)應(yīng)的數(shù)據(jù)values。其中fields中每個(gè)元素如果對(duì)應(yīng)的列族下還有相應(yīng)的列限定符的話,用“columnFamily:column”表示。例如,同時(shí)向“Math”、“Computer Science”、“English”三列添加成績(jī)時(shí),字符串?dāng)?shù)組fields為“Score:Math”,”Score;Computer Science”,”Score:English”,數(shù)組values存儲(chǔ)這三門(mén)課的成績(jī)。代碼:public static void addRecord(String tableName,

27、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 = fieldsi.split(":"); put.addColumn(cols0.getBytes(), cols1.getBytes(), valu

28、esi.getBytes(); table.put(put); table.close(); close();(3)scanColumn(String tableName, String column)瀏覽表tableName某一列的數(shù)據(jù),如果某一行記錄中該列數(shù)據(jù)不存在,則返回null。要求當(dāng)參數(shù)column為某一列族名稱(chēng)時(shí),如果底下有若干個(gè)列限定符,則要列出每個(gè)列限定符代表的列的數(shù)據(jù);當(dāng)參數(shù)column為某一列具體名稱(chēng)(例如“Score:Math”)時(shí),只需要列出該列的數(shù)據(jù)。代碼:public static void scanColumn(String tableName,String co

29、lumn)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);

30、 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

31、.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)+" "); (4)modifyData(Str

32、ing tableName, String row, String column)修改表tableName,行row(可以用學(xué)生姓名S_Name表示),列column指定的單元格的數(shù)據(jù)。代碼: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(); put.addColumn(column.getBytes(),null,val.getBytes(); table.put(put); table.close(); close();(5)deleteRow(String tableName, String row)刪除表tableName中row指定的行的記錄。public static void deleteRow(String tableName,String row)throws IOException init(); Table table = connection.getTab

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論