HBase还提供了API,我们可以通过编程的方式来进行对HBase的操作。
1. 环境准备
新建项目后在 pom.xml 中添加依赖:
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
2. HBase API
2.1 获取 Configuration 对象
public static Connection connection = null;
public static Admin admin = null;
static {
try {
//1. 获取配置信息
Configuration configuration = HBaseConfiguration.create();
//2. 创建连接对象
connection = ConnectionFactory.createConnection(configuration);
//3. 创建admin对象
admin = connection.getAdmin();
}catch (Exception e){
e.printStackTrace();
}
}
2.2 判断表是否存在
//2.判断表明是否存在
public static boolean isTableExistByNewFun(String tableName) throws IOException {
//3. 判断表是否存在
return admin.tableExists(TableName.valueOf(tableName));
}
2.3 DDL
package com.atguigu.DDL;
import com.atguigu.HbaseAPI;
import com.atguigu.test;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import java.io.IOException;
public class createTable {
public static void main(String[] args) throws IOException {
createTable table = new createTable();
System.out.println(HbaseAPI.isTableExistByNewFun("0408:stu5"));
//1. 创建表测试,在命名空间0408下面创建表
table.createTable("0408:stu5","info1","info2");
System.out.println(HbaseAPI.isTableExistByNewFun("0408:stu5"));
/*//2.删除表
table.dropTable("stu5");*/
//4.创建命名空间 hbase(main):004:0> list_namespace
//NAMESPACE
//0408
//default
//hbase
//3 row(s) in 0.3890 seconds
table.createNameSpace("0408");
//2.关闭资源
HbaseAPI.close();
}
//1.创建表
public void createTable (String tableName , String... cfs) throws IOException {
//1.判断是否存在列族信息
if (cfs.length <= 0){
System.out.println("请设置列族信息");
return;
}
//2. 判断表是否存在
if (HbaseAPI.isTableExistByNewFun(tableName)){
System.out.println(tableName + "表已存在");
return;
}
//2.5 创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//2.6 循环添加列族信息
for (String cf : cfs) {
//2.7 创建列族描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
//2.8 添加具体的列族信息
hTableDescriptor.addFamily(hColumnDescriptor);
}
//3.创建表
HbaseAPI.admin.createTable(hTableDescriptor);
}
//2.删除表
public void dropTable (String tableName) throws IOException {
System.out.println("++++++++++++++++++++表的删除操作++++++++++++++++");
//1.先判断表是否存在
if (HbaseAPI.isTableExistByNewFun(tableName)){
System.out.println(tableName+": 表存在");
}else{
System.out.println(tableName+":不存在");
return;
}
//2. 使表下线
HbaseAPI.admin.disableTable(TableName.valueOf(tableName));
//3.删除表
HbaseAPI.admin.deleteTable(TableName.valueOf(tableName));
}
//3. 创建命名空间
public void createNameSpace(String ns){
//1.创建命令空间描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
//2.创建命名空间
try {
HbaseAPI.admin.createNamespace(namespaceDescriptor);
}catch (NamespaceExistException e){
System.out.println(ns+":命名空间已存在");
}catch (Exception e){
e.printStackTrace();
}
System.out.println("命名空间存在,方法运行结束");
}
}
续:
package com.atguigu.DML;
import com.atguigu.HbaseAPI;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
public class Table_DML {
public static void main(String[] args) throws IOException {
Table_DML table = new Table_DML();
/*//1.创建数据测试
table.putData("stu","1002","info","name","TS");
*/
//2.获取单行数据
/*table.getData("stu","1001","info","name");*/
//3.查询表中的数据
/*table.ScanData("stu");*/
//4.测试删除
table.deleteData("stu","1006","info","name");
HbaseAPI.close();
}
//1.向表内插入数据
public void putData(String tableName,String rowKey , String cf , String cn , String value) throws IOException {
//1.获取表对象
Table table = HbaseAPI.connection.getTable(TableName.valueOf(tableName));
//2.创建put对象
Put put = new Put(Bytes.toBytes(rowKey));
//3.给put对象赋值
put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value));
//多个数据插入
//table.put(new List<Put>);
//4.插入数据
table.put(put);
}
//2.获取数据(get)
public void getData(String tableName , String rowKey , String cf , String cn) throws IOException {
//1.获取表对象
Table table = HbaseAPI.connection.getTable(TableName.valueOf(tableName));
//2.创建Get对象
Get get = new Get(Bytes.toBytes(rowKey));
//2.1 指定获取的列族
get.addFamily(Bytes.toBytes(cf));
//2.2 指定列族和列
get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
//2.3 设置获取数据的版本数
get.setMaxVersions();
//3.获取数据
Result result = table.get(get);
//4.解析result并打印
for (Cell cell : result.rawCells()) {
//5.打印数据
System.out.println( "CF: " + Bytes.toString(CellUtil.cloneFamily(cell)) +
" CN: "+ Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value: "+ Bytes.toString(CellUtil.cloneValue(cell))
);
}
table.close();
}
//3.获取数据(scan)
public void ScanData (String tableName) throws IOException {
//1.获取表的连接
Table table = HbaseAPI.connection.getTable(TableName.valueOf(tableName));
//2. 构建Scan对象
Scan scan = new Scan();
/*获取范围id的值*/
//Scan scan = new Scan(Bytes.toBytes("1001"),Bytes.toBytes("1003"));
//3.扫描表
ResultScanner resultScanner = table.getScanner(scan);
//4.解析resultScanner
for (Result result : resultScanner) {
//5.打印数据
for (Cell cell : result.rawCells()) {
//5.打印数据
System.out.println( "CF: " + Bytes.toString(CellUtil.cloneFamily(cell)) +
" CN: "+ Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value: "+ Bytes.toString(CellUtil.cloneValue(cell))
);
}
}
//5.关闭资源
table.close();
}
//4.删除数据
/*
* @author: 左先生
* @date: 2022-01-15 17:08
* @description:总结:
*/
public void deleteData(String tableName , String rowKey , String cf , String cn) throws IOException {
//1.获取表的连接
Table table = HbaseAPI.connection.getTable(TableName.valueOf(tableName));
//2.构建删除对象
Delete delete = new Delete(Bytes.toBytes(rowKey));
//2.1 设置删除的列,该设置可以删除指定的info以及列信息,进行单个删除,里面的timestamp参数可加可不加
delete.addColumns(Bytes.toBytes(cf),Bytes.toBytes(cn),1642236880388L);
//2.2 删除指定的列族
//delete.addFamily(Bytes.toBytes(cf));
//3.执行删除操作
table.delete(delete);
//4.关闭连接
table.close();
}
}