HBase API入门
环境准备
新建项目后在 pom.xml 中添加依赖:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.5</version>
</dependency>
DDL
public class HBaseDDL {
private static Connection connection;
private static Admin admin;
static {
//创建配置信息,指定连接集群
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
try {
//创建连接器
connection = ConnectionFactory.createConnection(configuration);
//创建DDL操作对象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close(){
try {
//关闭连接
admin.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
创建命名空间
public static void createNameSpace(String nameSpace){
//创建命名空间描述器
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(nameSpace);
NamespaceDescriptor descriptor = builder.build();
//创建命名空间
try {
admin.createNamespace(descriptor);
} catch (NamespaceExistException e) {
System.out.println("命名空间已存在");
} catch (IOException e) {
e.printStackTrace();
}
}
判断表是否存在
public static boolean isTableExist(String tableName) {
boolean tableExists = false;
try {
tableExists = admin.tableExists(TableName.valueOf(tableName));
} catch (IOException e) {
e.printStackTrace();
}
return tableExists;
}
创建表
public static void createTable(String tableName,String... args){
//判断是否有列族信息
if (args.length <=0){
System.out.println("请输入列族信息!");
return;
}
//判断表是否存在
if (isTableExist(tableName)){
System.out.println(tableName+"表已存在");
return;
}
//创建表描述器
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
//放入列族信息
for (String arg : args) {
//创建列族描述器
ColumnFamilyDescriptor familyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(arg)).build();
builder.setColumnFamily(familyDescriptor);
}
TableDescriptor tableDescriptor = builder.build();
//创建表
try {
admin.createTable(tableDescriptor);
} catch (IOException e) {
e.printStackTrace();
}
}
删除表
public static void deleteTable(String tableName){
//判断表是否存在
if (isTableExist(tableName)){
System.out.println(tableName+"表不存在");
return;
}
TableName name = TableName.valueOf(tableName);
try {
//使表不可用
admin.disableTable(name);
//删除表
admin.deleteTable(name);
} catch (IOException e) {
e.printStackTrace();
}
}
DML
public class HBaseDML {
private static Connection connection;
static {
//创建配置信息,指定连接集群
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
try {
//创建连接器
connection = ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close() {
try {
//关闭连接
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
插入数据
public static void putData(String tableName, String rowKey, String cf, String cn, String value) {
Table table = null;
try {
//获取table对象
table = connection.getTable(TableName.valueOf(tableName));
//创建Put对象
Put put = new Put(Bytes.toBytes(rowKey));
//给put对象添加数据
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
//执行插入数据
table.put(put);
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (table != null) {
table.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
get获取数据
public static void getData(String tableName, String rowKey, String cf, String cn) {
Table table = null;
try {
//获取table对象
table = connection.getTable(TableName.valueOf(tableName));
//创建Get对象,指定rowKey
Get get = new Get(Bytes.toBytes(rowKey));
//指定到列族(可选)
get.addFamily(Bytes.toBytes(cf));
//指定到列(可选)
get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
//查询数据
Result result = table.get(get);
//解析结果
for (Cell cell : result.rawCells()) {
System.out.println("CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
+ ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
+ ",Value:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (table != null) {
table.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
scan获取数据
public static void scanTable(String tableName,String startRowKey,String stopRowKey){
Table table = null;
try {
//获取table对象
table = connection.getTable(TableName.valueOf(tableName));
//创建Scan对象
Scan scan = new Scan();
//inclusive:true表示包含此行
scan.withStartRow(Bytes.toBytes(startRowKey),true);
scan.withStopRow(Bytes.toBytes(stopRowKey),true);
//查询数据
ResultScanner results = table.getScanner(scan);
//解析结果
Iterator<Result> resultIterator = results.iterator();
while (resultIterator.hasNext()){
Result result = resultIterator.next();
for (Cell cell : result.rawCells()) {
System.out.println("CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
+ ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
+ ",Value:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (table != null) {
table.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
删除数据
public static void deleteData(String tableName, String rowKey, String cf, String cn){
Table table = null;
try {
//获取table对象
table = connection.getTable(TableName.valueOf(tableName));
//创建delete对象,指定rowKey
//DeleteFamily:执行删除整个RowKey数据所添加的标记,作用范围:当前列族小于等标记时间戳的数据
Delete delete = new Delete(Bytes.toBytes(rowKey));
//指定到列族(可选)
//DeleteFamily:执行删除整个RowKey数据所添加的标记,作用范围:当前列族小于等标记时间戳的数据
delete.addFamily(Bytes.toBytes(cf));
//指定到列(可选)
//Delete:执行指定到列族的列数据所添加的标记,作用范围:只作用于标记中所携带的时间戳的范围
delete.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
//指定到列(可选)
//DeleteColumn:执行指定到列族的列数据所添加的标记,作用范围:当前列族小于等标记时间戳的数据
delete.addColumns(Bytes.toBytes(cf),Bytes.toBytes(cn));
//执行删除
table.delete(delete);
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (table != null) {
table.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}