上一章介绍了 Hbase API的核心类,这章主要实现HBase的增删改查功能。看代码:
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.Iterator;
import java.util.List;
public class HBaseTest {
//初始化Configuration对象
private Configuration conf = null;
//初始化连接
private Connection conn = null;
public void init() throws Exception {
//获取Configuration对象
conf = HBaseConfiguration.create();
//对于hbase的客户端来说,只需要知道hbase所经过的Zookeeper集群地址即可
//因为hbase的客户端找hbase读写数据完全不用经过HMaster
// conf.set("hbase.zookeeper.quorum", "master:2181,salve1:2181,slave2:2181");
conf.set("hbase.rootdir","hdfs://192.168.142.128:8020/hbase");
conf.set("hbase.master","hdfs://192.168.142.128:60000");
conf.set("hbase.zookeeper.property.clientPort","2181");
conf.set("hbase.zookeeper.quorum","master,slave1,slave2");
//获取连接
conn = ConnectionFactory.createConnection(conf);
}
public void createTable() throws Exception {
//获取表管理器对象
Admin admin = conn.getAdmin();
//创建表的描述对象,并指定表名
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("user_info".getBytes()));
//构造第一个列族描述对象,并指定列族名
HColumnDescriptor hcd1 = new HColumnDescriptor("base_info");
//构造第二个列族描述对象,并指定列族名
HColumnDescriptor hcd2 = new HColumnDescriptor("extra_info");
//为该列族设定一个版本数量,最小为1,最大为3
hcd2.setVersions(1, 3);
//将列族描述对象添加到表描述对象中
tableDescriptor.addFamily(hcd1).addFamily(hcd2);
//利用表管理器来创建表
admin.createTable(tableDescriptor);
//关闭
admin.close();
System.out.println("创建表成功");
}
public void testPut() throws Exception {
//创建table对象,通过table对象来添加数据
Table table = conn.getTable(TableName.valueOf("user_info"));
//创建一个集合,用于存放Put对象
ArrayList<Put> puts = new ArrayList<Put>();
//构建put对象(kv形式),并指定其行键
Put put01 = new Put(Bytes.toBytes("user001"));
put01.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"), Bytes.toBytes("zhangsan"));
put01.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("password"), Bytes.toBytes("123456"));
Put put02 = new Put("user002".getBytes());
put02.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"), Bytes.toBytes("lisi"));
put02.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("married"), Bytes.toBytes("false"));
// 把所有的put对象添加到一个集合中
puts.add(put01);
puts.add(put02);
// 提交所有的插入数据的记录
table.put(puts);
// 关闭
table.close();
System.out.println("添加数据成功");
}
public void testGet() throws IOException {
Table table = conn.getTable(TableName.valueOf("user_info"));
Get get = new Get("user001".getBytes());
// 使用HTable得到resultcanner实现类的对象
Result result1 = table.get(get);
List<Cell> cells = result1.listCells();
for (Cell cell : cells) {
//得到rowkey
System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
//得到列族
System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("----------------------******----------------------");
}
}
public void testScan() throws Exception {
//获取table对象
Table table = conn.getTable(TableName.valueOf("user_info"));
//获取scan对象
Scan scan = new Scan();
//获取查询的数据
ResultScanner scanner = table.getScanner(scan);
//获取ResultScanner所有数据,返回迭代器
Iterator<Result> iter = scanner.iterator();
//遍历迭代器
while (iter.hasNext()) {
// 获取当前每一行结果数据
Result result = iter.next();
// 获取当前每一行中所有的cell对象
List<Cell> cells = result.listCells();
// 迭代所有的cell
for(Cell c:cells){
//获取行键
byte[] rowArray = c.getRowArray();
// 获取列族
byte[] familyArray = c.getFamilyArray();
// 获取列族下的列名称
byte[] qualifierArray = c.getQualifierArray();
// 列字段的值
byte[] valueArray = c.getValueArray();
// 打印rowArray、familyArray、qualifierArray、valueArray
System.out.print("行键:"+new String(rowArray, c.getRowOffset(),c.getRowLength()));
System.out.print(" 列族:"+new String(familyArray,c.getFamilyOffset(),c.getFamilyLength()));
System.out.print(" 列:"+ new String(qualifierArray,c.getQualifierOffset(), c.getQualifierLength()));
System.out.println(" 值:"+ new String(valueArray,c.getValueOffset(), c.getValueLength()));
}
System.out.println("-----------------------");
}
//关闭
table.close();
}
public void testDel() throws Exception {
//获取table对象
Table table = conn.getTable(TableName.valueOf("user_info"));
//获取delete对象,需要一个rowkey
Delete delete = new Delete("user001".getBytes());
//在delete对象中指定要删除的列族-列名称
delete.addColumn("base_info".getBytes(), "password".getBytes());
//执行删除操作
table.delete(delete);
System.out.println("删除列成功");
//关闭
table.close();
}
public void testDrop() throws Exception {
//获取一个表的管理器
Admin admin = conn.getAdmin();
//删除表时先需要disable,将表置为不可用,然后在delete
admin.disableTable(TableName.valueOf("user_info"));
admin.deleteTable(TableName.valueOf("user_info"));
System.out.println("删除表成功");
//关闭
admin.close();
}
public static void main(String arg[]) throws Exception {
HBaseTest test = new HBaseTest();
test.init();
test.createTable();
test.testPut();
test.testGet();
test.testScan();
}
}
调用方法,实现完毕。