HBase API基本操作(含java代码实现)
目录
在前面我们已经安装和部署好了HBase服务,并对HBase的架构原理做了介绍:
HBase的架构、数据结构和进阶原理(读写流程、flush、合并、拆分)详解
那么,本篇文章再结合具体的例子对HBase的java API做一个基本实现,具体如下:
一、环境准备
1、启动服务
- 启动Zookeeper服务;
- 启动Hadoop服务;
- 启动HBase服务;

2、创建Maven工程
导入以下依赖:
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
二、HBaseAPI
1、编写一个Student类
package xsluo.hbase;
public class Student {
private String id;
private String name;
private String age;
private String gender;
private String phone;
private String email;
public Student(){
}
public Student(String id,String name,String age,String gender,String phone,String email) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age='" + age + '\'' +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
2、编写HBaseAPI类
package xsluo.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HBaseAPI {
private static Admin admin;
public static void main(String[] args) {
try {
System.out.println("===================1、创建测试用表table777===================");
createTable("table777",new String[]{"information","contact"});
System.out.println("===================2、往table777表插入数据===================");
Student student1 = new Student("001", "Ace", "27", "M", "176XXXX1867", "123@163.com");
insertData("table777",student1);
Student student2 = new Student("002", "Jack", "20", "M", "138XXXX1631", "456@qq.com");
insertData("table777",student2);
Student student3 = new Student("003", "Maria", "18", "F", "188XXXX7825", "789@qq.com");
insertData("table777",student3);
System.out.println("==================3、获取table777表所有数据===================");
List<Student> list = getAllData("table777");
for (Student student4 : list) {
System.out.println(student4.toString());
}
System.out.println("==================4、获取table777表原始数据===================");
getNoDealData("table777");
System.out.println("===================5、根据rowkey查询某一条数据===================");
Student student5 = getDataByRowKey("table777", "stu-003");
System.out.println(student5.toString());
System.out.println("===================6、获取指定单条数据的某个字段===================");
String stuPhone = getCellData("table777", "stu-001", "contact", "phone");
System.out.println(stuPhone);
System.out.println("==================7、插入一条测试数据test006===================");
Student student6 = new Student("test006", "test", "30", "M", "123456", "hhh@qq.com");
insertData("table777",student6);
System.out.println("-------------------8、获取插入test006后的所有数据--------------------");
List<Student> list2 = getAllData("table777");
for (Student student7 : list2){
System.out.println(student7.toString());
}
System.out.println("-------------------9、删除测试数据test006--------------------");
deleteByRowKey("table777", "stu-test006");
System.out.println("--------------------10、获取表中所有数据--------------------");
List<Student> list3 = getAllData("table777");
for (Student student8 : list3){
System.out.println(student8.toString());
}
System.out.println("-------------------11、删除table777表--------------------");
deleteTable("table777");
} catch (IOException e) {
e.printStackTrace();
}
}
//根据rowKey进行查询
public static Student getDataByRowKey(String tableName, String rowKey) throws IOException {
Table table = initHbase().getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
Student student = new Student();
student.setId(rowKey);
//先判断是否有此条数据
if(!get.isCheckExistenceOnly()){
Result result = table.get(get);
for (Cell cell : result.rawCells()){
String colName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
if(colName.equals("name")){
student.setName(value);
}
if(colName.equals("age")){
student.setAge(value);
}
if (colName.equals("gender")){
student.setGender(value);
}
if (colName.equals("phone")){
student.setPhone(value);
}
if (colName.equals("email")){
student.setEmail(value);
}
}
}
return student;
}
//查询指定单cell内容
public static String getCellData(String tableName, String rowKey, String family, String col){
try {
Table table = initHbase().getTable(TableName.valueOf(tableName));
String result = null;
Get get = new Get(rowKey.getBytes());
if(!get.isCheckExistenceOnly()){
get.addColumn(Bytes.toBytes(family),Bytes.toBytes(col));
Result res = table.get(get);
byte[] resByte = res.getValue(Bytes.toBytes(family), Bytes.toBytes(col));
return result = Bytes.toString(resByte);
}else{
return result = "查询结果不存在";
}
} catch (IOException e) {
e.printStackTrace();
}
return "出现异常";
}
//查询指定表名中所有的数据
public static List<Student> getAllData(String tableName){
Table table = null;
List<Student> list = new ArrayList<Student>();
try {
table = initHbase().getTable(TableName.valueOf(tableName));
ResultScanner results = table.getScanner(new Scan());
for (Result result : results){
String id = new String(result.getRow());
System.out.println("用户名:" + new String(result.getRow()));
Student stu = new Student();
Cell[] cells = result.rawCells();
for(Cell cell : cells){
String row = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String colName = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
stu.setId(row);
if(colName.equals("name")){ stu.setName(value); }
if(colName.equals("age")){ stu.setAge(value); }
if (colName.equals("gender")){ stu.setGender(value); }
if (colName.equals("phone")){ stu.setPhone(value); }
if (colName.equals("email")){ stu.setEmail(value); }
}
list.add(stu);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
//删除指定cell数据
public static void deleteByRowKey(String tableName, String rowKey) throws IOException {
Table table = initHbase().getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
//删除指定列
//delete.addColumns(Bytes.toBytes("contact"), Bytes.toBytes("email"));
table.delete(delete);
System.out.println(rowKey + "数据已删除!");
}
//删除表
public static void deleteTable(String tableName){
try {
TableName tablename = TableName.valueOf(tableName);
admin = initHbase().getAdmin();
admin.disableTable(tablename);
admin.deleteTable(tablename);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(tableName + "表已删除!");
}
//获取原始数据
public static void getNoDealData(String tableName){
try {
Table table= initHbase().getTable(TableName.valueOf(tableName));
//得到用于扫描region的对象
Scan scan = new Scan();
//得到Resultcanner实现类的对象
ResultScanner resutScanner = table.getScanner(scan);
for(Result result: resutScanner){
System.out.println("scan: " + result);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//插入数据
public static void insertData(String tableName, Student student) throws IOException {
TableName tablename = TableName.valueOf(tableName);
Put put = new Put(("stu-" + student.getId()).getBytes());
//参数:1.列族名 2.列名 3.值
put.addColumn("information".getBytes(), "name".getBytes(), student.getName().getBytes()) ;
put.addColumn("information".getBytes(), "age".getBytes(), student.getAge().getBytes()) ;
put.addColumn("information".getBytes(), "gender".getBytes(), student.getGender().getBytes()) ;
put.addColumn("contact".getBytes(), "phone".getBytes(), student.getPhone().getBytes());
put.addColumn("contact".getBytes(), "email".getBytes(), student.getEmail().getBytes());
//HTable table = new HTable(initHbase().getConfiguration(),tablename);已弃用
Table table = initHbase().getTable(tablename);
table.put(put);
System.out.println(student.getId() + "数据插入成功!");
table.close();
}
//创建表
public static void createTable(String tableNmae, String[] columnFamily) throws IOException {
TableName tableName = TableName.valueOf(tableNmae);
admin = initHbase().getAdmin();
if (admin.tableExists(tableName)) {
System.out.println("表已存在!");
} else {
//创建表属性对象,表名需要转字节
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
//创建多个列族
for (String cf : columnFamily) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hTableDescriptor.addFamily(hColumnDescriptor);
}
//根据对表的配置,创建表
admin.createTable(hTableDescriptor);
System.out.println("表" + tableNmae + "创建成功!");
}
}
//连接集群
public static Connection initHbase() throws IOException {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
//集群配置↓
configuration.set("hbase.zookeeper.quorum", "192.168.2.100,192.168.2.101,192.168.2.102");
configuration.set("hbase.master", "192.168.2.100:60000");
Connection connection = ConnectionFactory.createConnection(configuration);
return connection;
}
}
3、测试结果

