1.只需要引入HBase的客户端依赖即可。
<!--Hbase客户端-->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.1</version>
</dependency>
2.HBaseUtils.java 工具类抽取
public class HBaseUtils {
private static Configuration configuration;
// 1.获取配置对象
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","hadoop1:2181,hadoop2:2181,hadoop3:2181");
}
// 2.获取连接对象
public static Admin getAdmin() throws IOException {
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
return admin;
}
/*获取table*/
public static Table getTable() throws IOException {
return getTable("user_info");
}
public static Table getTable(String tablename) throws IOException {
Connection connection = ConnectionFactory.createConnection(configuration);
return connection.getTable(TableName.valueOf(tablename));
}
/*关闭table*/
public static void close(Table table) throws IOException {
if (table != null){
table.close();
}
}
// 3.释放admin
public static void close(Admin admin) throws IOException {
admin.close();
}
public static void close(Admin admin,Table table) throws IOException {
close(admin);
close(table);
}
}
3.DDL 操作
public class TableDDL {
private HBaseAdmin admin;
@Before
public void before() throws IOException {
admin = (HBaseAdmin) HBaseUtils.getAdmin();
}
/*创建表*/
@Test
public void createTable() throws IOException {
// 1.创建表描述器对象
HTableDescriptor ht = new HTableDescriptor(TableName.valueOf("user_info"));
// 2.添加列簇
HColumnDescriptor familyColumn1 = new HColumnDescriptor("base_info");
HColumnDescriptor familyColumn2 = new HColumnDescriptor("extra_info");
ht.addFamily(familyColumn1);
ht.addFamily(familyColumn2);
admin.createTable(ht);
}
/*删除表*/
@Test
public void deleteTable() throws IOException {
TableName tableName = TableName.valueOf("user_info");
HTableDescriptor user_info = new HTableDescriptor(tableName);
if (!admin.isTableDisabled(tableName)){
admin.disableTable(tableName);
}
admin.deleteTable(tableName);
}
/*修改表*/
@Test
public void modifyTable() throws IOException {
TableName tableName = TableName.valueOf("user_info");
HTableDescriptor user_info = admin.getTableDescriptor(tableName);
// 2.添加列簇
HColumnDescriptor familyColumn1 = new HColumnDescriptor("base_info2");
HColumnDescriptor familyColumn2 = new HColumnDescriptor("extra_info2");
user_info.addFamily(familyColumn1);
user_info.addFamily(familyColumn2);
admin.modifyTable(tableName,user_info);
}
/*查询所有的列簇*/
@Test
public void listAllFamily() throws IOException {
//查询所有的列簇
HTableDescriptor user_info = admin.getTableDescriptor(TableName.valueOf("user_info"));
HColumnDescriptor[] columnFamilies = user_info.getColumnFamilies();
for (HColumnDescriptor ht : columnFamilies){
System.out.println(ht.getNameAsString());
}
}
/*删除一个列簇*/
@Test
public void removeFamily() throws IOException {
TableName tableName = TableName.valueOf("user_info");
HTableDescriptor user_info = admin.getTableDescriptor(tableName);
//删除
//user_info.removeFamily(Bytes.toBytes("extra_info"));
//提交修改
//admin.modifyTable(tableName,user_info);
admin.deleteColumn(tableName,Bytes.toBytes("extra_info"));
}
@After
public void after() throws IOException {
HBaseUtils.close(admin);
}
}
4.DML 操作
public class TableDML {
private Table table;
@Before
public void before() throws IOException {
table = HBaseUtils.getTable();
}
/*插入一条记录
* put 'ns1:t1','r1','c1','value'
* 批量插入的话,直接放入list集合即可
* */
@Test
public void put() throws IOException {
// 1.创建put对象
Put put = new Put(Bytes.toBytes("001")); //行建
// 2.添加列数据
put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("name"),Bytes.toBytes("wang"));
table.put(put);
}
/*查询一条记录*/
@Test
public void get1() throws IOException {
// 1.创建get对象
Get get = new Get(Bytes.toBytes("001"));
Result result = table.get(get);
NavigableMap<byte[], byte[]> base_info = result.getFamilyMap(Bytes.toBytes("base_info"));
// 2.便利有序集合
Set<Map.Entry<byte[], byte[]>> entries = base_info.entrySet();
for (Map.Entry<byte[], byte[]> entry : entries){
System.out.println(new String(entry.getKey() + "--->"+new String(entry.getValue())));
}
}
/*查询一条记录*/
@Test
public void get2() throws IOException {
// 1.创建get对象
Get get = new Get(Bytes.toBytes("001"));
Result result = table.get(get);
CellScanner cellScanner = result.cellScanner();
// 2.遍历
while (cellScanner.advance()){
//当前的cell
Cell cell = cellScanner.current();
System.out.println(new String(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()));//列簇
System.out.println(new String(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));//列名
System.out.println(new String(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));//列值
}
}
/*查询一条记录*/
@Test
public void get3() throws IOException {
// 1.创建get对象
Get get = new Get(Bytes.toBytes("001"));
Result result = table.get(get);
CellScanner cellScanner = result.cellScanner();
// 2.遍历
while (cellScanner.advance()){
//当前的cell
Cell cell = cellScanner.current();
System.out.println(new String(CellUtil.cloneRow(cell)));//行建
System.out.println(new String(CellUtil.cloneFamily(cell)));
System.out.println(new String(CellUtil.cloneQualifier(cell)));
System.out.println(new String(CellUtil.cloneValue(cell)));
}
}
@After
public void after() throws IOException {
HBaseUtils.close(table);
}
}
来源:oschina
链接:https://my.oschina.net/wyn365/blog/3212634