1、实验4熟悉常用的HBase操作教学文案实验4熟悉常用的HBase操作姓名:包生友 专业年级:软件143 学号:20140126991. 实验目的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命令完成相同任务:(完整可执行代码见 代码
2、/QuestionOne.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.print
3、ln(表名:+hTableDescriptor.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(); ResultScanne
4、r 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(列簇: +new String(CellUtil
5、.cloneFamily(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:Mat
6、h,69图3 给s1添加数据编程:/向表添加数据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
7、(), val.getBytes(); 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 = connecti
8、on.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); table.close(); close();deleteRow(s1,zhangsan,score,Math)(4)清空指定的表的所有记录
9、数据; Shell:truncate 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(t
10、ableName); admin.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.getSca
11、nner(scan); int 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)2015001Zhangsanmale232015003Maryfemale222015003Lis
12、imale24课程表(Course)课程号(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_Na
13、me,S_Sex,S_Age图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
14、_No,2015003put 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
15、,123002put Course,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,123001pu
16、t SC,sc001,SC_Score,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,2015003
17、put SC,sc005,SC_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为存储记
18、录各个域名称的数组。要求当HBase已经存在名为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)
19、; admin.deleteTable(tablename);/删除原来的表 HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename); for(String str:fields) HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str); hTableDescriptor.addFamily(hColumnDescriptor); admin.createTable(hTableDescriptor);300元以下 9 18% close()
20、;现在是个飞速发展的时代,与时俱进的大学生当然也不会闲着,在装扮上也不俱一格,那么对作为必备道具的饰品多样性的要求也就可想而知了。(2)addRecord(String tableName, String row, String fields, String values) 向表tableName、行row(用S_Name表示)和字符串数组files指定的单元格中添加对应的数据values。其中fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“Computer Science”、“English”三列添加
21、成绩时,字符串数组fields为“Score:Math”,”Score;Computer Science”,”Score:English”,数组values存储这三门课的成绩。代码:public static void addRecord(String tableName,String row,String fields,String values) throws IOException init();(3)优惠多 Table table = connection.getTable(TableName.valueOf(tableName);调研课题: for(int i = 0;i != fi
22、elds.length;i+) Put put = new Put(row.getBytes();自制饰品一反传统的饰品消费模式,引导的是一种全新的饰品文化,所以非常容易被我们年轻的女生接受。 String cols = fieldsi.split(:);标题:大学生究竟难在哪?创业要迈五道坎 2004年3月23日 put.addColumn(cols0.getBytes(), cols1.getBytes(), valuesi.getBytes(); table.put(put); table.close();大学生购买力有限,即决定了要求商品能价廉物美,但更注重的还是在购买过程中对精神文化
23、爱好的追求,满足心理需求。 close();(3) 年龄优势根据调查资料分析:大学生的消费购买能力还是有限的,为此DIY手工艺品的消费不能高,这才有广阔的市场。四、影响的宏观环境分析(3)scanColumn(String tableName, String column) 浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。代码: public static void
24、 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; res
25、ult = 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.print
26、ln(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表示),列colu
27、mn指定的单元格的数据。代码: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.cl
28、ose(); 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(Bytes.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 S
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1