1、实验4熟悉常用的HBase操作实验4熟悉常用的HBase操作姓名:包生友 专业年级:软件143 学号:*6991.实验目的1. 理解HBase在Hadoop体系结构中的角色;2. 熟练使用HBase操作常用的Shell命令;3. 熟悉HBase操作常用的Java API。2.实验环境操作系统:LinuxHadoop版本:2.6.0或以上版本HBase版本:1.1.2或以上版本JDK版本:1.6或以上版本Java IDE:Eclipse3.实验内容和完成情况1. 编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:(完整可执行代码见 代码/QuestionOne.
2、java)(1)列出HBase所有的表的相关信息,例如表名;Shell:List图1 列出HBase所有表的相关信息编程:/(1)列出HBase所有的表的相关信息,例如表名、创建时间等public static void listTables() throws IOException init();/建立连接 HTableDescriptor hTableDescriptors = admin.listTables(); for(HTableDescriptor hTableDescriptor :hTableDescriptors) System.out.println(表名:+hTable
3、Descriptor.getNameAsString(); close();/关闭连接(2)在终端打印出指定的表的所有记录数据;Shell:scan s1图2 打印指定表的所有记录数据编程:/(2)在终端打印出指定的表的所有记录数据public static void getData(String tableName)throws IOException init(); Table table = connection.getTable(TableName.valueOf(tableName); Scan scan = new Scan(); ResultScanner scanner = t
4、able.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(列簇: +new String(CellUtil.cloneFamily(
5、cell); System.out.print( 列: +new String(CellUtil.cloneQualifier(cell); System.out.print( 值: +new String(CellUtil.cloneValue(cell); System.out.println(时间戳: +cell.getTimestamp(); (3)向已经创建好的表添加和删除指定的列族或列;p.s:此题请先在Shell中创建s1作为示例表: create s1,scorea) 在s1表,添加数据:Shell: put s1,zhangsan,score:Math,69图3 给s1添加数
6、据编程:/向表添加数据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.getBy
7、tes(); table.put(put); table.close(); close(); insertRow(s1,zhangsan,score,Math,69)b) 在s1表,删除指定的列:Shell: delete s1,zhangsan,score:Math图4 删除数据编程:/删除数据public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException init(); Table table = connection.getTable(T
8、ableName.valueOf(tableName); Delete delete = new Delete(rowKey.getBytes(); /删除指定列族 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)清空指定的表的所有记录数据; Shell:tru
9、ncate s1图5 清空指定表的所有记录数据编程:/(4)清空指定的表的所有记录数据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); ad
10、min.createTable(hTableDescriptor); close();(5)统计表的行数。Shell: count s1图6 统计表的行数编程:/(5)统计表的行数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); i
11、nt num = 0; for (Result result = scanner.next();result!=null;result=scanner.next() num+; System.out.println(行数:+ num); scanner.close(); close();2. 现有以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据:学生表(Student)学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)2015001Zhangsanmale232015003Maryfemale222015003Lisimale24课程表(Co
12、urse)课程号(C_No)课程名(C_Name)学分(C_Credit)123001Math2.0123002Computer Science5.0123003English3.0 选课表(SC)学号(SC_Sno)课程号(SC_Cno)成绩(SC_Score)2015001123001862015001123003692015002123002772015002123003992015003123001982015003123002951学生Student表p.s:主键的列名是随机分配的,因此无需创建主键列创建表:create Student,S_No,S_Name,S_Sex,S_Age
13、图7 创建Student表插入数据:插入数据shell命令第一行数据put Student,s001,S_No,2015001put Student,s001,S_Name,Zhangsanput Student,s001,S_Sex,maleput Student,s001,S_Age,23第二行数据put Student,s002,S_No,2015002put Student,s002,S_Name,Maryput Student,s002,S_Sex,femaleput Student,s002,S_Age,22第三行数据put Student,s003,S_No,2015003put
14、 Student,s003,S_Name,Lisiput Student,s003,S_Sex,maleput Student,s003,S_Age,24图8 添加数据并查看图9 添加3个学生2课程Course表创建表:create Course,C_No,C_Name,C_Credit图10 创建Course表插入数据:插入数据shell命令第一行数据put Course,c001,C_No,123001put Course,c001,C_Name,Mathput Course,c001,C_Credit,2.0第二行数据put Course,c002,C_No,123002put Cour
15、se,c002,C_Name,Computerput Course,c002,C_Credit,5.0第三行数据put Course,c003,C_No,123003put Course,c003,C_Name,Englishput Course,c003,C_Credit,3.0图11 添加数据图12 添加3个课程3选课表创建表:create SC,SC_Sno,SC_Cno,SC_Score图13 创建表SC插入数据:插入数据shell命令第一行数据put SC,sc001,SC_Sno,2015001put SC,sc001,SC_Cno,123001put SC,sc001,SC_Sc
16、ore,86第二行数据put SC,sc002,SC_Sno,2015001put SC,sc002,SC_Cno,123003put SC,sc002,SC_Score,69第三行数据put SC,sc003,SC_Sno,2015002put SC,sc003,SC_Cno,123002put SC,sc003,SC_Score,77第四行数据put SC,sc004,SC_Sno,2015002put SC,sc004,SC_Cno,123003put SC,sc004,SC_Score,99第五行数据put SC,sc005,SC_Sno,2015003put SC,sc005,SC_
17、Cno,123001put SC,sc005,SC_Score,98第六行数据put SC,sc006,SC_Sno,2015003put SC,sc006,SC_Cno,123002put SC,sc006,SC_Score,95图14 插入数据图15 数据显示图16 QuestionOne运行后控制台消息同时,请编程完成以下指定功能:(完整可执行代码见 代码/QuestionTwo.java)(1)createTable(String tableName, String fields) 创建表,参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组。要求当HBa
18、se已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。代码: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!); admin.disableTable(tablename); admin.deleteTa
19、ble(tablename);/删除原来的表 HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename); for(String str:fields) HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str); hTableDescriptor.addFamily(hColumnDescriptor); admin.createTable(hTableDescriptor); close();(2)addRecord(String tableNa
20、me, String row, String fields, String values) 向表tableName、行row(用S_Name表示)和字符串数组files指定的单元格中添加对应的数据values。其中fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,字符串数组fields为“Score:Math”,”Score;Computer Science”,”Score:English”,数组values存储这三门课的成绩。代码:p
21、ublic 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 = fieldsi.split(:); put.addColumn(cols0.g
22、etBytes(), cols1.getBytes(), valuesi.getBytes(); table.put(put); table.close(); close();(3)scanColumn(String tableName, String column) 浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。代码: public static void sc
23、anColumn(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
24、 = scanner.next() showCell(result); 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(
25、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(String tableName, String row, String column) 修改表tableName,行row(可以用学生姓名S_Name表示),列column指
26、定的单元格的数据。代码: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
27、(); close();(5)deleteRow(String tableName, String row) 删除表tableName中row指定的行的记录。public static void deleteRow(String tableName,String row)throws IOException init(); Table table = connection.getTable(TableName.valueOf(tableName); Delete delete = new Delete(row.getBytes(); /删除指定列族 /delete.addFamily(Byte
28、s.toBytes(colFamily); /删除指定列 /delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col); table.delete(delete); table.close(); close();图17 QuestionTwo运行后控制台消息3. 利用HBase和MapReduce完成如下任务:假设HBase有2张表,表的逻辑视图及部分数据如下所示:表 逻辑视图及部分数据书名(bookName)价格(price)Database System Concept30$Thinking in Java60$Data Mining25$要求:从HBase读出上述两张表的数据,对“price”的排序,并将结果存储到HBase中。操作方法如下:create book,bookNameput book,val_60$,bookName:,Thinking in Java put book,val_20$,bookName:,Database System Conceptput book,val_30$,bookName:,Data M
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1