大数据练习三
实验3:熟悉常用的HBase操作
实验环境
操作系统:Ubuntu 18.04
Hadoop版本:3.1.3
HBase版本:2.2.2
JDK版本:1.8.0_212
Eclipse版本:2020-03
实验内容与完成情况
(一)HBase Shell命令实现
序号 功能 命令
1 列出HBase所有表的相关信息 list
2 打印指定表的所有记录数据 scan 'tableName'
3 向表添加和删除指定的列族或列 # 添加列族
alter 'tableName', 'newColumnFamily'
# 删除列族
alter 'tableName', {NAME => 'columnFamily', METHOD => 'delete'}
# 添加列限定符(直接插入数据即可)
put 'tableName', 'rowKey', 'columnFamily:columnQualifier', 'value'
4 清空指定表的所有记录数据 truncate 'tableName'
5 统计表的行数 count 'tableName'
(二)关系型数据库表转换为HBase表
1学生表(Student):
表名:Student
行键:S_No
列族:Info
列:S_Name, S_Sex, S_Age
2课程表(Course):
表名:Course
行键:C_No
列族:Info
列:C_Name, C_Credit
3选课表(SC):
表名:SC
行键:SC_Sno_SC_Cno(组合键)
列族:Score
列:SC_Score
(三)Java API实现
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseJavaAPI {
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();
}
}
// 1. 创建表
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);
}
org.apache.hadoop.hbase.HTableDescriptor tableDesc = new org.apache.hadoop.hbase.HTableDescriptor(tn);
for (String field : fields) {
tableDesc.addFamily(new org.apache.hadoop.hbase.HColumnDescriptor(field));
}
admin.createTable(tableDesc);
}
// 2. 向表中添加记录
public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(row));
for (int i = 0; i < fields.length; i++) {
String[] cols = fields[i].split("😊;
if (cols.length == 1) {
put.addColumn(Bytes.toBytes(cols[0]), null, Bytes.toBytes(values[i]));
} else if (cols.length == 2) {
put.addColumn(Bytes.toBytes(cols[0]), Bytes.toBytes(cols[1]), Bytes.toBytes(values[i]));
}
}
table.put(put);
table.close();
}
// 3. 浏览表的某一列数据
public static void scanColumn(String tableName, String column) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
String[] cols = column.split("😊;
if (cols.length == 1) {
scan.addFamily(Bytes.toBytes(cols[0]));
} else if (cols.length == 2) {
scan.addColumn(Bytes.toBytes(cols[0]), Bytes.toBytes(cols[1]));
}
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println("Row: " + Bytes.toString(result.getRow()));
for (org.apache.hadoop.hbase.Cell cell : result.rawCells()) {
String family = Bytes.toString(org.apache.hadoop.hbase.CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(org.apache.hadoop.hbase.CellUtil.cloneQualifier(cell));
String value = Bytes.toString(org.apache.hadoop.hbase.CellUtil.cloneValue(cell));
System.out.println(" " + family + ":" + qualifier + " = " + value);
}
}
scanner.close();
table.close();
}
// 4. 修改表中的数据
public static void modifyData(String tableName, String row, String column, String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(row));
String[] cols = column.split("😊;
if (cols.length == 1) {
put.addColumn(Bytes.toBytes(cols[0]), null, Bytes.toBytes(value));
} else if (cols.length == 2) {
put.addColumn(Bytes.toBytes(cols[0]), Bytes.toBytes(cols[1]), Bytes.toBytes(value));
}
table.put(put);
table.close();
}
// 5. 删除表中的行
public static void deleteRow(String tableName, String row) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(row));
table.delete(delete);
table.close();
}
public static void main(String[] args) throws IOException {
// 示例使用
String tableName = "Student";
String[] fields = {"Info"};
createTable(tableName, fields);
String[] columns = {"Info:S_Name", "Info:S_Sex", "Info:S_Age"};
String[] values1 = {"Zhangsan", "male", "23"};
addRecord(tableName, "2015001", columns, values1);
String[] values2 = {"Mary", "female", "22"};
addRecord(tableName, "2015002", columns, values2);
scanColumn(tableName, "Info");
modifyData(tableName, "2015001", "Info:S_Age", "24");
deleteRow(tableName, "2015002");
admin.close();
connection.close();
}
}
编译和运行命令
编译
hadoop com.sun.tools.javac.Main -cp $(hbase classpath) HBaseJavaAPI.java
运行
java -cp $(hbase classpath):. HBaseJavaAPI
出现的问题及解决方案
问题 解决方案
HBase启动失败 确保Hadoop已启动,且HBase配置文件(hbase-site.xml)正确
Java API连接HBase失败 检查HBase服务是否正常,配置文件是否包含正确的hbase.zookeeper.quorum
创建表时提示权限不足 确保HBase用户对表有创建权限,或使用root用户运行
扫描表时无结果 检查表名、列族或列限定符是否正确,确保表中已插入数据

浙公网安备 33010602011771号