一.pom配置
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.2</version>
<!-- <scope>provided</scope> -->
</dependency>
二.代码
package com.antg.main;
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;
import java.util.Random;
public class HbaseOp {
// 用于hbase连接
public Connection connection;
// hbase配置
public static Configuration configuration = HBaseConfiguration.create();
//初始化Hbase操作对象
public HbaseOp() throws IOException {
// ad = new HBaseAdmin(configuration); //过期了,推荐使用Admin
configuration.set("hbase.zookeeper.quorum", "192.168.1.34,192.168.1.31,192.168.1.32,192.168.1.41");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
// 对connection初始化
connection = ConnectionFactory.createConnection(configuration);
}
//创建表
public void createTable(String tableName,String... cf1) throws IOException {
//获取admin对象
Admin admin = connection.getAdmin();
//创建tableName对象
TableName tName = TableName.valueOf(tableName);
//创建描述信息
HTableDescriptor hTableDescriptor = new HTableDescriptor(tName);
//判断表是否已经存在
if(admin.tableExists(tName)){
System.out.println("表"+tableName+"已经存在");
return;
}
//列族
for (String s : cf1) {
hTableDescriptor.addFamily(new HColumnDescriptor(s));
}
//创建表
admin.createTable(hTableDescriptor);
System.out.println("表"+tableName+"创建成功");
}
//删除表
public void delTable(String name) throws IOException {
//获取Admin
Admin admin = connection.getAdmin();
//表名
TableName tableName = TableName.valueOf(name);
//判断是否存在
if(admin.tableExists(tableName)){
//先禁用
admin.disableTable(tableName);
//后删除
admin.deleteTable(tableName);
System.out.println(tableName+"删除成功");
}else {
System.out.println("表不存在");
}
}
//插入数据
public void put(String tableName) throws IOException {
TableName tName = TableName.valueOf(tableName);
//获取数据
Table table = connection.getTable(tName);
Random random = new Random();
//创建一个记录容器
List<Put> puts = new ArrayList<>();
for(int i = 0;i<10;i++){
//创建一条记录i
Put put = new Put(Bytes.toBytes("r_"+i));
//加入列(列族,列,值)
put.addColumn(Bytes.toBytes("user"),Bytes.toBytes("username"),Bytes.toBytes("user_"+i));
put.addColumn(Bytes.toBytes("user"),Bytes.toBytes("age"),Bytes.toBytes(i+""));
put.addColumn(Bytes.toBytes("content"),Bytes.toBytes("phone"),Bytes.toBytes("8888"+i));
put.addColumn(Bytes.toBytes("content"),Bytes.toBytes("age"),Bytes.toBytes("aaaa"+i));
//添加到容器中
puts.add(put);
}
table.put(puts);
System.out.println("插入数据成功");
}
//查询数据
public void search(String tableName) throws IOException {
//TableName
TableName tName = TableName.valueOf(tableName);
//获取Table
Table table = connection.getTable(tName);
//获取Get对象
List<Get> gets = new ArrayList<>();
for(int i = 0;i<5;i++){
Get get = new Get(Bytes.toBytes("r_"+i));
gets.add(get);
}
//获取数据
Result[] results = table.get(gets);
//打印数据
for (Result result : results) {
//使用cell获取其中的数据
CellScanner cellScanner = result.cellScanner();
//逐个获取
while (cellScanner.advance()){
Cell cell = cellScanner.current();
String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualify = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowKey + ",columnfamily:"
+ family + ",qualify:" + qualify + ",value:" + value);
}
}
}
//关闭连接
public void clean() throws IOException {
connection.close();
}
//测试数据
public static void main(String[] args) throws IOException {
HbaseOp hbaseOp = new HbaseOp();
//创建表
hbaseOp.createTable("fjh001","user","content");
//插入数据
hbaseOp.put("fjh001");
//查询数据
hbaseOp.search("fjh001");
//删除表
hbaseOp.delTable("fjh001");
//关闭连接
hbaseOp.clean();
}
}
运行结果: