12.26
实验3
熟悉常用的HBase操作
1.实验目的
(1)理解HBase在Hadoop体系结构中的角色;
(2)熟练使用HBase操作常用的Shell命令;
(3)熟悉HBase操作常用的Java API。
2.实验平台
(1)操作系统:Linux(建议Ubuntu16.04或Ubuntu18.04);
(2)Hadoop版本:3.1.3;
(3)HBase版本:2.2.2;
(4)JDK版本:1.8;
(5)Java IDE:Eclipse。
3. 实验步骤
(一)编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
(1) 列出HBase所有的表的相关信息,例如表名;
(2) 在终端打印出指定的表的所有记录数据;
(3) 向已经创建好的表添加和删除指定的列族或列;
# Add column family
alter 'tableName', 'ADD', 'columnFamily'
# Delete column family
alter 'tableName', 'DELETE', 'columnFamily'
# Add a column to an existing column family
put 'tableName', 'rowKey', 'columnFamily:columnQualifier', 'value'
# Delete a specific column from a row
delete 'tableName', 'rowKey', 'columnFamily:columnQualifier'
(4) 清空指定的表的所有记录数据;
(5) 统计表的行数。
(二)HBase数据库操作
1. 现有以下关系型数据库中的表和数据(见表14-3到表14-5),要求将其转换为适合于HBase存储的表并插入数据:
表14-3 学生表(Student)
|
学号(S_No) |
姓名(S_Name) |
性别(S_Sex) |
年龄(S_Age) |
|
2015001 |
Zhangsan |
male |
23 |
|
2015002 |
Mary |
female |
22 |
|
2015003 |
Lisi |
male |
24 |
表14-4 课程表(Course)
|
课程号(C_No) |
课程名(C_Name) |
学分(C_Credit) |
|
123001 |
Math |
2.0 |
|
123002 |
Computer Science |
5.0 |
|
123003 |
English |
3.0 |
表14-5 选课表(SC)
|
学号(SC_Sno) |
课程号(SC_Cno) |
成绩(SC_Score) |
|
2015001 |
123001 |
86 |
|
2015001 |
123003 |
69 |
|
2015002 |
123002 |
77 |
|
2015002 |
123003 |
99 |
|
2015003 |
123001 |
98 |
|
2015003 |
123002 |
95 |
create 'student', 'info'
put 'student', '2015001', 'info:name', 'Zhangsan' put 'student', '2015001', 'info:sex', 'male' put 'student', '2015001', 'info:age', '23' put 'student', '2015002', 'info:name', 'Mary' put 'student', '2015002', 'info:sex', 'female' put 'student', '2015002', 'info:age', '22' put 'student', '2015003', 'info:name', 'Lisi' put 'student', '2015003', 'info:sex', 'male' put 'student', '2015003', 'info:age', '24'
create 'course', 'info'
put 'course', '123001', 'info:name', 'Math' put 'course', '123001', 'info:credit', '2.0' put 'course', '123002', 'info:name', 'Computer Science' put 'course', '123002', 'info:credit', '5.0' put 'course', '123003', 'info:name', 'English' put 'course', '123003', 'info:credit', '3.0'
create 'sc', 'score'
put 'sc', '2015001_123001', 'score:grade', '86' put 'sc', '2015001_123003', 'score:grade', '69' put 'sc', '2015002_123002', 'score:grade', '77' put 'sc', '2015002_123003', 'score:grade', '99' put 'sc', '2015003_123001', 'score:grade', '98' put 'sc', '2015003_123002', 'score:grade', '95'
2. 请编程实现以下功能:
(1)createTable(String tableName, String[] fields)
创建表,参数tableName为表的名称,字符串数组fields为存储记录各个字段名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; public class HBaseOperations { private static Configuration conf = HBaseConfiguration.create(); private static Connection connection; private static Admin admin; static { try { connection = ConnectionFactory.createConnection(conf); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } public static void createTable(String tableName, String[] fields) throws IOException { TableName tn = TableName.valueOf(tableName); if (admin.tableExists(tn)) { admin.disableTable(tn); admin.deleteTable(tn); } TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tn); for (String field : fields) { ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(field)).build(); tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor); } admin.createTable(tableDescriptorBuilder.build()); } }
(2)addRecord(String tableName, String row, String[] fields, String[] values)
向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据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 { try (Table table = connection.getTable(TableName.valueOf(tableName))) { Put put = new Put(Bytes.toBytes(row)); for (int i = 0; i < fields.length; i++) { String[] parts = fields[i].split(":"); put.addColumn(Bytes.toBytes(parts[0]), Bytes.toBytes(parts.length > 1 ? parts[1] : ""), Bytes.toBytes(values[i])); } table.put(put); } }
(3)scanColumn(String tableName, String column)
浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。
public static void scanColumn(String tableName, String column) throws IOException { try (Table table = connection.getTable(TableName.valueOf(tableName))) { Scan scan = new Scan(); String[] parts = column.split(":"); if (parts.length == 2) { // Specific column scan.addColumn(Bytes.toBytes(parts[0]), Bytes.toBytes(parts[1])); } else { // Column family scan.addFamily(Bytes.toBytes(column)); } ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { for (Cell cell : result.rawCells()) { System.out.println("Row: " + Bytes.toString(result.getRow()) + ", Column Family: " + Bytes.toString(CellUtil.cloneFamily(cell)) + ", Column Qualifier: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + ", Value: " + Bytes.toString(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 newValue) throws IOException { try (Table table = connection.getTable(TableName.valueOf(tableName))) { Put put = new Put(Bytes.toBytes(row)); String[] parts = column.split(":"); put.addColumn(Bytes.toBytes(parts[0]), Bytes.toBytes(parts.length > 1 ? parts[1] : ""), Bytes.toBytes(newValue)); table.put(put); } }
(5)deleteRow(String tableName, String row)
删除表tableName中row指定的行的记录。
public static void deleteRow(String tableName, String row) throws IOException { try (Table table = connection.getTable(TableName.valueOf(tableName))) { Delete delete = new Delete(Bytes.toBytes(row)); table.delete(delete); } }
4.实验报告
|
题目: |
实验3
熟悉常用的HBase操作 |
姓名 |
胡铁丞 |
日期2024-11-18日 |
|
实验环境:(1)操作系统:Linux;
(2)Hadoop版本:3.1.0。
(3)虚拟机:VMware。
(4)工具:xshell。
(5)JDK版本:1.8;
(6)Java IDE:IDEA。
(7)HBase版本:2.4; |
||||
|
实验内容与完成情况:完成 |
||||
|
出现的问题:(1) 打开hbase shell无法正常运行
(2) hbase:023:0> drop 'STUDENT' ERROR: Table STUDENT is enabled. Disable it first. For usage try 'help "drop"' Took 0.0225 seconds hbase:024:0>
(3) API连接失败 |
||||
|
解决方案(列出遇到的问题和解决办法,列出没有解决的问题): (1) 打开hbase之前要先打开zookeeper
(2) 在删除之前需要先禁用它disable 'STUDENT' 然后drop 'STUDENT'
(3) API连接时conf.set("hbase.rootdir", "hdfs://hadoop102:8020/hbase"); |
||||
浙公网安备 33010602011771号