版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、精品文檔實(shí)驗(yàn) 4 熟悉常用的HBase操作姓名:包生友專業(yè)年級(jí):軟件143學(xué)號(hào): 20140126991. 實(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版本:或以上版本HBase版本:或以上版本JDK版本: 1.6 或以上版本Java IDE :Eclipse3. 實(shí)驗(yàn)內(nèi)容和完成情況1. 編程實(shí)現(xiàn)以下指定功能,并用Hadoop 提供的 HBase Shell 命令完成相同任務(wù):(完整可執(zhí)行代碼見代碼 /Questi
2、onOne.java)( 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();。1歡迎下載精品文檔for(HTableDescriptor hTableDescriptor :hTableDescriptors)表名 :&qu
3、ot;+hTableDescriptor.getNameAsString();close();/關(guān)閉連接( 2)在終端打印出指定的表的所有記錄數(shù)據(jù);Shell: scan 's1'圖 2 打印指定表的所有記錄數(shù)據(jù)編程:/(2)在終端打印出指定的表的所有記錄數(shù)據(jù)public static void getData(String tableName)throws IOExceptioninit();Table table = connection.getTable(TableName.valueOf(tableName);Scan scan = new Scan();ResultS
4、canner scanner = table.getScanner(scan);for (Result result:scanner)printRecoder(result);close();/ 打印一條記錄的詳情public static void printRecoder(Result result)throws IOExceptionfor(Cell cell:result.rawCells()行健 : "+new String(CellUtil.cloneRow(cell);列簇 : "+new String(CellUtil.cloneFamily(cell);列
5、 : "+new String(CellUtil.cloneQualifier(cell);值 : "+new String(CellUtil.cloneValue(cell);。2歡迎下載精品文檔時(shí)間戳 : "+cell.getTimestamp();(3)向已經(jīng)創(chuàng)建好的表添加和刪除指定的列族或列;p.s :此題請(qǐng)先在Shell中創(chuàng)建 s1 作為示例表 : create 's1','score'a) 在 s1 表 , 添加數(shù)據(jù):Shell:put 's1','zhangsan','score
6、:Math','69'圖 3 給 s1 添加數(shù)據(jù)編程:/ 向表添加數(shù)據(jù)public static void insertRow(String tableName,String rowKey,StringcolFamily,String col,String val) throws IOException init();Table table = connection.getTable(TableName.valueOf(tableName); Put put = new Put(rowKey.getBytes(); put.addColumn(colFamily.get
7、Bytes(), col.getBytes(), val.getBytes();table.put(put);table.close();close();insertRow("s1",'zhangsan','score','Math','69')b) 在 s1 表,刪除指定的列:Shell:。3歡迎下載精品文檔delete 's1','zhangsan','score:Math'圖 4 刪除數(shù)據(jù)編程:/ 刪除數(shù)據(jù)public static void deleteR
8、ow(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),Byte
9、s.toBytes(col);table.delete(delete);table.close();close();deleteRow("s1",'zhangsan','score','Math')( 4)清空指定的表的所有記錄數(shù)據(jù);Shell: truncate 's1'。4歡迎下載精品文檔圖 5 清空指定表的所有記錄數(shù)據(jù)編程 :/(4)清空指定的表的所有記錄數(shù)據(jù)public static void clearRows(String tableName)throws IOExceptioninit();Tab
10、leName tablename = TableName.valueOf(tableName);admin.disableTable(tablename);admin.deleteTable(tablename);HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);admin.createTable(hTableDescriptor);close();(5)統(tǒng)計(jì)表的行數(shù)。Shell:count 's1'圖 6 統(tǒng)計(jì)表的行數(shù)編程:/(5)統(tǒng)計(jì)表的行數(shù)public static void count
11、Rows(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+;。5歡迎下載精品文檔行數(shù) :"+ num);scann
12、er.close();close();2.現(xiàn)有以下關(guān)系型數(shù)據(jù)庫中的表和數(shù)據(jù),要求將其轉(zhuǎn)換為適合于HBase存儲(chǔ)的表并插入數(shù)據(jù):學(xué)生表( Student )學(xué)號(hào)( S_No)姓名( S_Name)性別( S_Sex)年齡( S_Age)2015001Zhangsanmale232015003Maryfemale222015003Lisimale24課程表( Course )課程號(hào)( C_No)課程名( C_Name)學(xué)分( C_Credit )123001Math2.0123002Computer Science5.0123003English3.0選課表( SC)學(xué)號(hào)( SC_Sno)課程號(hào)(
13、 SC_Cno)成績(jī)( SC_Score)201500112300186201500112300369201500212300277201500212300399201500312300198201500312300295學(xué)生 Student表p.s :主鍵的列名是隨機(jī)分配的,因此無需創(chuàng)建主鍵列創(chuàng)建表: create 'Student','S_No','S_Name','S_Sex','S_Age'。6歡迎下載精品文檔圖 7 創(chuàng)建 Student表插入數(shù)據(jù) :插入數(shù)據(jù)shell命令第一行數(shù)據(jù)put 'Stu
14、dent','s001','S_No','2015001'put 'Student','s001','S_Name','Zhangsan'put 'Student','s001','S_Sex','male'put 'Student','s001','S_Age','23'第二行數(shù)據(jù)put 'Student','s002
15、9;,'S_No','2015002'put 'Student','s002','S_Name','Mary'put 'Student','s002','S_Sex','female'put 'Student','s002','S_Age','22'第三行數(shù)據(jù)put 'Student','s003','S_No','2
16、015003'put 'Student','s003','S_Name','Lisi'put 'Student','s003','S_Sex','male'put 'Student','s003','S_Age','24'。7歡迎下載精品文檔圖 8 添加數(shù)據(jù)并查看圖 9 添加 3個(gè)學(xué)生課程 Course 表創(chuàng)建表: create 'Course','C_No',
17、39;C_Name','C_Credit'圖 10 創(chuàng)建 Course 表插入數(shù)據(jù):插入數(shù)據(jù)shell命令第一行數(shù)據(jù)put 'Course','c001','C_No','123001'put 'Course','c001','C_Name','Math'put 'Course','c001','C_Credit','2.0'第二行數(shù)據(jù)put 'Course',
18、39;c002','C_No','123002'。8歡迎下載精品文檔put 'Course','c002','C_Name','Computer'put 'Course','c002','C_Credit','5.0'第三行數(shù)據(jù)put 'Course','c003','C_No','123003'put 'Course','c003'
19、,'C_Name','English'put 'Course','c003','C_Credit','3.0'圖 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命令。9歡迎下載精品文檔第一行數(shù)據(jù)put 'SC','sc001','SC_Sno','
20、2015001'put 'SC','sc001','SC_Cno','123001'put 'SC','sc001','SC_Score','86'第二行數(shù)據(jù)put 'SC','sc002','SC_Sno','2015001'put 'SC','sc002','SC_Cno','123003'put 'SC',
21、9;sc002','SC_Score','69'第三行數(shù)據(jù)put 'SC','sc003','SC_Sno','2015002'put 'SC','sc003','SC_Cno','123002'put 'SC','sc003','SC_Score','77'第四行數(shù)據(jù)put 'SC','sc004','SC_Sno'
22、,'2015002'put 'SC','sc004','SC_Cno','123003'put 'SC','sc004','SC_Score','99'第五行數(shù)據(jù)put 'SC','sc005','SC_Sno','2015003'put 'SC','sc005','SC_Cno','123001'put 'SC
23、9;,'sc005','SC_Score','98'第六行數(shù)據(jù)put 'SC','sc006','SC_Sno','2015003'put 'SC','sc006','SC_Cno','123002'put 'SC','sc006','SC_Score','95'圖 14 插入數(shù)據(jù)。10歡迎下載精品文檔圖 15 數(shù)據(jù)顯示。11歡迎下載精品文檔圖 16 Que
24、stionOne 運(yùn)行后控制臺(tái)消息同時(shí),請(qǐng)編程完成以下指定功能:(完整可執(zhí)行代碼見代碼 /QuestionTwo.java)( 1) createTable(String tableName, String fields)創(chuàng)建表,參數(shù)tableName 為表的名稱,字符串?dāng)?shù)組fields為存儲(chǔ)記錄各個(gè)域名稱的數(shù)組。要求當(dāng) HBase已經(jīng)存在名為tableName 的表的時(shí)候, 先刪除原有的表,然后再創(chuàng)建新的表。12歡迎下載精品文檔代碼:public static void createTable(String tableName,String fields) throws IOExceptio
25、n init();TableName tablename = TableName.valueOf(tableName);if(admin.tableExists(tablename)admin.disableTable(tablename);admin.deleteTable(tablename);/刪除原來的表HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);for(String str:fields)HColumnDescriptor hColumnDescriptor = new HColumnDesc
26、riptor(str);hTableDescriptor.addFamily(hColumnDescriptor);admin.createTable(hTableDescriptor);close();( 2) addRecord(String tableName, String row, String fields, String values)向表 tableName 、行 row(用 S_Name表示)和字符串?dāng)?shù)組files指定的單元格中添加對(duì)應(yīng)的數(shù)據(jù)values 。其中 fields中每個(gè)元素如果對(duì)應(yīng)的列族下還有相應(yīng)的列限定符的話,用“columnFamily:column ”表示。
27、 例如,同時(shí)向 “ Math ”、“ Computer Science ”、“ English ” 三列添加成績(jī)時(shí), 字符串?dāng)?shù)組 fields 為 “ Score:Math ”, ”Score ;Computer Science ”, ”Score:English” ,數(shù)組 values存儲(chǔ)這三門課的成績(jī)。代碼:publicstaticvoidaddRecord(StringtableName,Stringrow,Stringfields,Stringvalues) throws IOException init();。13歡迎下載精品文檔Table table = connection.ge
28、tTable(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(),valuesi.getBytes();table.put(put);table.close();close();( 3) scanColumn(String tableName, String column
29、)瀏覽表 tableName 某一列的數(shù)據(jù),如果某一行記錄中該列數(shù)據(jù)不存在,則返回null 。要求當(dāng)參數(shù)column 為某一列族名稱時(shí),如果底下有若干個(gè)列限定符,則要列出每個(gè)列限定符代表的列的數(shù)據(jù);當(dāng)參數(shù)column 為某一列具體名稱(例如“Score:Math ”)時(shí),只需要列出該列的數(shù)據(jù)。代碼:publicstaticvoidscanColumn(StringtableName,Stringcolumn)throws IOExceptioninit();Table table = connection.getTable(TableName.valueOf(tableName);Scan s
30、can = new Scan();scan.addFamily(Bytes.toBytes(column);ResultScanner scanner = table.getScanner(scan);for(Resultresult= scanner.next();result!= null;result= scanner.next()showCell(result);table.close();close();。14歡迎下載精品文檔/ 格式化輸出public static void showCell(Result result)Cell cells = result.rawCells();
31、for(Cell cell:cells)String(CellUtil.cloneFamily(cell)+" ");String(CellUtil.cloneQualifier(cell)+" ");( 4) modifyData(String tableName, String row, String column)修改表 tableName ,行 row(可以用學(xué)生姓名S_Name表示),列column 指定的單元格的數(shù)據(jù)。代碼:public static void modifyData(String tableName,String row,St
32、ring column,Stringval)throws IOExceptioninit();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)。15歡迎下載精品文檔刪除表 tableName 中 row 指定的行的記錄。public static void deleteRow(String tableName,String row)throws IOExceptioninit();Table table = connection.getTable(TableName.valueOf(tableName);Delete delete = new Dele
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 內(nèi)蒙古自治區(qū)退役軍人事務(wù)廳
- 鋅錠買賣合同書
- 長(zhǎng)期合作購銷合同協(xié)議
- 湘教版地理八下7.1《香港特別行政區(qū)的國際樞紐功能》聽課評(píng)課記錄1
- 專項(xiàng)借款合同范本
- 采購委托合同
- 智能制造與企業(yè)韌性:機(jī)制與效應(yīng)
- 層狀復(fù)合與粒子改性對(duì)電弧熔絲增材制造Al-Mg-Si合金組織與性能的影響
- 2025年粵教版八年級(jí)歷史下冊(cè)月考試卷含答案
- 2025年華東師大版選修5歷史下冊(cè)階段測(cè)試試卷含答案
- 部編版三語下《語文園地七》核心素養(yǎng)分層作業(yè)學(xué)習(xí)任務(wù)單(含答案)
- 第四章投資性房地產(chǎn)課件
- SB-T 11238-2023 報(bào)廢電動(dòng)汽車回收拆解技術(shù)要求
- 旅游公司發(fā)展規(guī)劃
- 新舊施工現(xiàn)場(chǎng)臨時(shí)用電安全技術(shù)規(guī)范對(duì)照表
- 03軸流式壓氣機(jī)b特性
- 五星級(jí)酒店收入測(cè)算f
- 某省博物館十大展陳評(píng)選項(xiàng)目申報(bào)書
- GB/T 9109.5-2017石油和液體石油產(chǎn)品動(dòng)態(tài)計(jì)量第5部分:油量計(jì)算
- GB/T 16316-1996電氣安裝用導(dǎo)管配件的技術(shù)要求第1部分:通用要求
- GA/T 455-2021居民身份證印刷要求
評(píng)論
0/150
提交評(píng)論