




已阅读5页,还剩18页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
精品文档实验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命令完成相同任务:(完整可执行代码见 代码/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.println(表名:+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(); ResultScanner 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.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:Math,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(), 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 = 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); table.close(); close();deleteRow(s1,zhangsan,score,Math)(4)清空指定的表的所有记录数据;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(tableName); 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.getScanner(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)2015001Zhangsanmale232015003Maryfemale222015003Lisimale24课程表(Course)课程号(C_No)课程名(C_Name)学分(C_Credit)123001Math2.0123002Computer Science5.0123003English3.0 选课表(SC)学号(SC_Sno)课程号(SC_Cno)成绩(SC_Score)201500112300186201500112300369201500212300277201500212300399201500312300198201500312300295 学生Student表p.s:主键的列名是随机分配的,因此无需创建主键列创建表:create Student,S_No,S_Name,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_No,2015003put Student,s003,S_Name,Lisiput Student,s003,S_Sex,maleput Student,s003,S_Age,24图8 添加数据并查看图9 添加3个学生 课程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 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个课程 选课表创建表: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_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,2015003put 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为存储记录各个域名称的数组。要求当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); admin.deleteTable(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 tableName, 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存储这三门课的成绩。代码: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 = fieldsi.split(:); put.addColumn(cols0.getBytes(), 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 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); 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)+ ); (4)modifyData(String tableName, String row, String column)修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。代码: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.getTable(TableName.valueOf(tableName); Delete delete = new Delete(row
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年上海市金融稳定发展研究中心公开招聘工作人员模拟试卷完整参考答案详解
- 2025安徽淮北师范大学招聘高层次人才90人模拟试卷附答案详解(黄金题型)
- 2025黑龙江鸡西市社会治安综合治理中心招聘公益性岗位1人考前自测高频考点模拟试题及答案详解一套
- 2025黑龙江哈尔滨市平房区侵华日军第七三一部队罪证陈列馆讲解员招聘5人考前自测高频考点模拟试题及答案详解(夺冠系列)
- 2025国网经济技术研究院有限公司第二批高校毕业生录用人选的模拟试卷及答案详解(易错题)
- 2025年宁德市供电服务有限公司招聘30人考前自测高频考点模拟试题及答案详解(夺冠系列)
- 2025江苏淮安市淮阴城市产业投资集团有限公司招聘拟聘用人员考前自测高频考点模拟试题附答案详解(考试直接用)
- 2025广西贺州市富川瑶族自治县公安局第一次公开招聘警务辅助人员8人模拟试卷附答案详解(模拟题)
- 2025安康石泉县两河镇中心卫生院招聘(2人)考前自测高频考点模拟试题有答案详解
- 2025届春季雅砻江公司校园招聘正式启动考前自测高频考点模拟试题及一套完整答案详解
- 施工队进场安全教育培训
- 海绵印拓画课件
- 2025年科技创新与成果转化的知识能力考核试题及答案
- 秩序员休假管理制度
- 2025至2030中国惯性导航行业投资现状与前景预测分析报告
- 轻型卒中临床诊疗中国专家共识(2024版)解读
- 非ST段抬高型急性冠脉综合征诊断和治疗指南(2024)解读
- 耳机品质协议书范本
- 2025版VI设计合同范本
- 人美版五年级上册5.绘画中的透视现象一等奖教案设计
- 从法律出发理解与应用新清单标准
评论
0/150
提交评论