【大数据实验】熟悉常用的 HBase 操作

本文详细介绍HBase在Hadoop生态系统中的作用,提供HBase Shell命令与Java API的实际操作指南,包括表管理、数据操作及MapReduce集成,适用于初学者快速上手。

实验目的

  • 理解 HBase 在 Hadoop 体系结构中的角色
  • 熟练使用 HBase 操作常用的 Shell 命令
  • 熟悉 HBase操作常用的 Java API

实验平台

  • 操作系统:Ubuntu 16.04
  • Hadoop 版本:3.1.3
  • HBase 版本:2.2.2
  • JDK 版本:1.8
  • Java IDE:Eclipse

注:实验需要开启hbase服务,开启顺序为先Hadoop → Hbase,关闭顺序为Hbase → Hadoop

实验内容和要求

1. 编程实现以下指定功能,并用 Hadoop 提供的 HBase Shell 命令完成相同任务:

(1) 列出 HBase 所有的表的相关信息,例如表名

hbase(main):001:0> list

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

查看记录数据:
hbase(main):001:0> scan '表名'

查看表的信息:
hbase(main):001:0> describe '表名'

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

添加列族或列:
hbase(main):001:0> alter '表名','NAME'=>'列名'

删除列族或列:
hbase(main):001:0> alter '表名','NAME'=>'列名',METHOD=>'delete'

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

hbase(main):001:0> drop '表名'

(5) 统计表的行数

hbase(main):001:0> count '表名'

2.现有以下关系型数据库中的表和数据,要求将其转换为适合于 HBase 存储的表并插入数据:

学生表:

学号 姓名 性别 年龄
2015001 Zhangsan male 23
2015002 Mary female 22
2015003 Lisi male 24

课程表:

课程号 课程名 学分
123001 Math 2.0
123002 Computer Science 5.0
123003 English 3.0

选课表:

学号 课程号 成绩
2015001 123001 86
2015001 123003 69
2015002 123002 77
2015002 123003 99
2015003 123001 98
2015003 123002 95

创建三个表格
在这里插入图片描述

‘Student’表中添加数据
在这里插入图片描述

‘Course’表中添加数据
在这里插入图片描述

‘SC’表中添加数据
在这里插入图片描述

同时,请编程完成以下指定功能:

(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.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 CreateTable {
   
   
	public static Configuration configuration;
	public static Connection connection;
	public static Admin admin;
	
	public static void init(){
   
   //建立连接
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir","hdfs://localhost:9000/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
### HBase常用操作教程 HBase是一种分布式、可扩展的NoSQL数据库,主要用于存储大规模结构化数据。它基于Hadoop生态系统构建,利用HDFS作为底层存储系统[^4]。以下是HBase的一些常见操作及其具体实现方法: #### 1. **HBase Shell 命令** HBase Shell 提供了一系列命令用于管理和操作HBase表。以下是一些常用的Shell命令: - 创建表 使用`create`命令创建一张新表,并定义其列族。 ```bash create 'student', 'info' ``` 上述命令会创建名为`student`的表,并为其添加一个名为`info`的列族。 - 列出所有表 使用`list`命令查看当前所有的HBase表。 ```bash list ``` - 插入数据 向表中插入一条记录。 ```bash put 'student', 'row1', 'info:name', 'John Doe' ``` 此处表示向`student`表的`row1`行键下的`info:name`列插入值`John Doe`[^1]。 - 查询数据 获取某一行的数据。 ```bash get 'student', 'row1' ``` - 删除数据 删除特定行或列的数据。 ```bash delete 'student', 'row1', 'info:name' ``` - 扫描整个表 查看表中的所有数据。 ```bash scan 'student' ``` - 删除整张表 需先禁用该表再执行删除操作。 ```bash disable 'student' drop 'student' ``` 以上命令涵盖了HBase Shell中最基础的操作需求[^2]。 --- #### 2. **Java API 编程接口** 除了通过Shell进行交互外,开发者也可以借助HBase Java API完成更加复杂的功能开发。下面列举了一些核心API调用示例: ##### (1) 初始化连接 建立与HBase集群之间的通信链接。 ```java Configuration config = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin(); ``` ##### (2) 表管理 创建和删除表格。 ```java TableName tableName = TableName.valueOf("course"); if (!admin.tableExists(tableName)) { TableDescriptorBuilder tableDesc = TableDescriptorBuilder.newBuilder(tableName); ColumnFamilyDescriptor columnFamily = ColumnFamilyDescriptorBuilder.of("details"); tableDesc.setColumnFamily(columnFamily); admin.createTable(tableDesc.build()); } // Drop the table when no longer needed. admin.disableTable(tableName); admin.deleteTable(tableName); ``` ##### (3) 数据读写 对表内的实际内容实施增删改查动作。 ```java try (Table table = connection.getTable(tableName)) { Put put = new Put(Bytes.toBytes("record001")); put.addColumn(Bytes.toBytes("details"), Bytes.toBytes("title"), Bytes.toBytes("Mathematics")); table.put(put); Get get = new Get(Bytes.toBytes("record001")); Result result = table.get(get); System.out.println(result.getColumnLatestCell(Bytes.toBytes("details"), Bytes.toBytes("title"))); } catch (IOException e) { e.printStackTrace(); } ``` 上述代码片段展示了如何使用Java程序访问并操控HBase资源[^3]。 --- #### 3. **其他重要特性** - **高性能查询**:由于采用了Key/Value存储模型,即使面对TB级甚至PB级别的数据集,也能保持稳定的检索速度[^4]。 - **水平分区机制**:每张大表会被分割成若干个小区域(region),分布至不同节点上独立维护,从而提升整体并发能力。 - **缺乏关系型支持**:需要注意的是,HBase并不具备传统意义上的JOIN语句或者索引优化等功能,如果项目涉及此类场景,则需额外引入MapReduce或其他计算引擎辅助解决。 ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值