实验三 熟悉常用的HBase操作
一、实验目的
(1)理解HBase在Hadoop体系结构中的角色;
(2)熟练使用HBase操作常用的Shell命令;
(3)熟悉HBase操作常用的Java API。
二、实验平台
操作系统:centos7;
Hadoop版本:3.3;
HBase版本:2.2.2;
JDK版本:1.8;
Java IDE:IDEA。
三、实验内容和要求
(一)编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
(1) 列出HBase所有的表的相关信息,例如表名、创建时间等;

(2) 在终端打印出指定的表的所有记录数据;

(3) 向已经创建好的表添加和删除指定的列族或列;

(4) 清空指定的表的所有记录数据;

(5) 统计表的行数。

(二)HBase数据库操作
1 现有以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据:
学生表(Student)
| 学号(S_No) | 姓名(S_Name) | 性别(S_Sex) | 年龄(S_Age) |
|---|---|---|---|
| 2015001 | Zhangsan | male | 23 |
| 2015003 | Mary | female | 22 |
| 2015003 | Lisi | male | 24 |
课程表(Course)
| 课程号(C_No) | 课程名(C_Name) | 学分(C_Credit) |
|---|---|---|
| 123001 | Math | 2.0 |
| 123002 | Computer | 5.0 |
| 123003 | English | 3.0 |
选课表(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 |
2 请编程实现以下功能:
(1) createTable(String tableName, String[] fields)
创建表,参数tableName为表的名称,字符串数组fields为存储记录各个字段名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。
package Main;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class main {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void init(){
//建立连接
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://127.0.0.1:8020/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch(IOException e){
e.printStackTrace();
}
}
public static void close(){
//关闭连接
try{
if(admin != null){
admin.close();
}
if(connection != null){
connection.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
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);
}
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tablename);
for(int i=0;i<fields.length;i++){
ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(fields[i])).build();
tableDescriptor.setColumnFamily(family);
}
admin.createTable(tableDescriptor.build());
close();
}
public

本文详细介绍了在Hadoop体系结构中,如何通过HBaseShell命令和JavaAPI进行基础操作,包括创建表、删除列族、添加/删除记录、统计行数,以及将关系型数据库数据转换为HBase存储。还提供了使用Java代码实现的功能,如创建表、添加记录和扫描列等。
最低0.47元/天 解锁文章





