本文介绍了通过Hbase API对数据库中数据进行增删查。
依赖
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
工具类
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;
public class HbaseTest {
static Admin admin = null;
static Connection connection = null;
static {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "xx.xx.xx.xx");
try {
connection = ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
e.printStackTrace();
}
try {
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void close(Connection connection, Admin admin) throws IOException {
if (connection != null) {
connection.close();
}
if (admin != null) {
admin.close();
}
}
// 判断表是否存在
public static boolean tableExists(String tableName) throws IOException {
return admin.tableExists(TableName.valueOf(tableName));
}
// 创建表
public static void createTable(String tableName, String... columnFamily) throws IOException {
if (columnFamily.length <= 0) {
System.out.println("请设置列族信息");
return;
}
if (tableExists(tableName)) {
System.out.println("表" + tableName + "已存在");
return;
} else {
// 创建表描述器
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
for (String cf : columnFamily) {
ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build();
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
}
//根据对表的配置,创建表
admin.createTable(tableDescriptorBuilder.build());
System.out.println("表" + tableName + "创建成功!");
}
}
// 删除表
public static void deleteTable(String tableName) throws IOException {
if (tableExists(tableName)) {
// 使表不可用
admin.disableTable(TableName.valueOf(tableName));
// 删除表
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("表" + tableName + "已经删除");
} else {
System.out.println("表" + tableName + "不存在!");
}
}
// 创建命名空间
public static void createNameSpace(String nameSpace) {
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(nameSpace).build();
try {
admin.createNamespace(namespaceDescriptor);
} catch (NamespaceExistException e) {
System.out.println(nameSpace + "命名空间已经存在!");
} catch (IOException e) {
e.printStackTrace();
}
}
// 插入数据
public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
// 获取Table对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 向表中插入数据
Put data = new Put(Bytes.toBytes(rowKey));
// 向put中组装数据
data.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(data);
table.close();
System.out.println("插入数据成功");
}
// 删除多行数据
public static void deleteData(String tableName, String... rowKeys) throws IOException {
// 获取Table对象
Table table = connection.getTable(TableName.valueOf(tableName));
ArrayList<Delete> deleteArrayList = new ArrayList<>();
for (String row : rowKeys) {
Delete delete = new Delete(Bytes.toBytes(row));
deleteArrayList.add(delete);
}
table.delete(deleteArrayList);
}
// 全表扫描
public static void scanTable(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
for (Result result : results) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
//得到rowkey
System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) +
",列族" + Bytes.toString(CellUtil.cloneFamily(cell)) +
",列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
",值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
table.close();
}
// 获取指定的rowKey的数据
public static void getData(String tableName, String rowKey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) +
",列族" + Bytes.toString(CellUtil.cloneFamily(cell)) +
",列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
",值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
// 获取指定的"列族:列"的数据
public static void getData(String tableName, String rowKey, String columnFamily, String column) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) +
",列族" + Bytes.toString(CellUtil.cloneFamily(cell)) +
",列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
",值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
public static void main(String[] args) throws IOException {
deleteTable("changfan");
createTable("changfan", "info");
addRowData("changfan", "1001", "info", "name", "dashuige");
addRowData("changfan", "1001", "info", "age", "26");
getData("changfan", "1001");
deleteData("changfan", "1001");
}
}
参考文章