前言
之前曾经发表过博文,整理了Hbase2.1.0之后的相关API。这里对获取数据的方法进行详细整理
创建连接
//获取到当前设置
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "bigdate01:2181,bigdate02:2181,bigdate03:2181");
//创建Hbase连接
Connection conn = ConnectionFactory.createConnection(conf);
获取操作表
//设置操作的表
TableName tablename=TableName.valueOf(Bytes.toBytes("testtable"));
Table table = conn.getTable(tablename);
获取数据
通过行键 列族 列获取到值
Get get=new Get(Bytes.toBytes("row-1"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
//这种方法通过指定的列族 列名获取到对应的值,获取到的值只有一个版本
if(table.exists(get)) {//判断需要获取的数据是否存在
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
System.out.println("获取到的值为:"+Bytes.toString(value));
}
通过行键获取到数据
Get get1=new Get(Bytes.toBytes("row-1"));
get1.addFamily(Bytes.toBytes("colfam1"));
//获取到当前选中列族的数据
if (table.exists(get1)) {
Result result = table.get(get1);
NavigableMap<byte[], byte[]> fam1 = result.getFamilyMap(Bytes.toBytes("colfam1"));
Set<Entry<byte[], byte[]>> entrySet = fam1.entrySet();
Iterator<Entry<byte[], byte[]>> iterator = entrySet.iterator();
System.out.println("获取到的键值对数量:"+result.size());
while(iterator.hasNext()) {
Entry<byte[], byte[]> next = iterator.next();
byte[] key = next.getKey();
System.out.println("当前获取到的键为:"+Bytes.toString(key)+",当前获取的值为:"+Bytes.toString(fam1.get(key)));
}
}
get的其他方法
Get get3=new Get(Bytes.toBytes("row-1"));
get3.addFamily(Bytes.toBytes("colfam1"));//限制读取的列族
//设置读取的列族和列,这种方法读取的的值,获取到值是当前列的最新版本。
get3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
get3.setTimeRange(0, Integer.MAX_VALUE);//设置可读取的时间戳范围
get3.setTimestamp(12458585);//设置读取数据的时间戳
get3.setMaxVersions();//设置读取的最大版本数,这种设置会把最大的版本数为Integer.maxvalue()
get3.setMaxVersions(2);//设置读取的最大版本数为2
System.out.println("列族大小:"+get3.numFamilies());//这里获取到大小,指的是通过Get对象中设置的列族数量
Result对象的说明
/**创建的get对象,通过table.get()方法执行,返回值Result
*
*/
Get get3=new Get(Bytes.toBytes("row-1"));
get3.addFamily(Bytes.toBytes("colfam1"));//限制读取的列族
//设置读取的列族和列,这种方法读取的的值,获取到值是当前列的最新版本。
get3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
Result result = table.get(get3);
//返回对应列的最新单元格的值
byte[] value = result.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
//获取Get是咧设置的行键
byte[] row = result.getRow();
System.out.println("获取到当前对应的行键:"+Bytes.toString(row));
判断获取的数据是否为空
/***************************************************************************************
* size()方法和isEmpty()方法,两者是对服务器端返回的键值对进行操作
* 在HBase以前的版本中,返回的键值对是以KeyValue对象的形式返回的
* 在2.1.0及以后的版本中,返回的键值对是以Cell的对象的形式
***************************************************************************************
*/
int size = result.size();
System.out.println("服务器返回的键值对数量为:"+size);
System.out.println("判断当前返回的数据是否为空:"+result.isEmpty());
读取Cell对象
Cell[] cells = result.rawCells();
//读取cell对象
if(cells.length>0) {
for (int i = 0; i < cells.length; i++) {
System.out.println("当前读取到的行:"+Bytes.toString(CellUtil.cloneRow(cells[i])));
System.out.println("当前读取到的列族:"+Bytes.toString(CellUtil.cloneFamily(cells[i])));
System.out.println("当前读取到的列:"+Bytes.toString(CellUtil.cloneQualifier(cells[i])));
System.out.println("当前读取到的值:"+Bytes.toString(CellUtil.cloneValue(cells[i])));
}
}else {
System.out.println("读取到的数据大小为空");
}
Result的其他方法
//返回对应列的最新版本值,和getColumn不同的是,它返回的对象封装成了Cell对象
Cell cell = result.getColumnLatestCell(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
boolean contains = result.containsColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
System.out.println("判断结果中是否包含对应的值:"+contains);
//将所有的get请求返回的内容都装入一个Java的Map类实例,这样用户可以使用该方法遍历所有的结果
// 行键 列族 时间戳 列
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
//和getMap不同的是,只会获取到当前每个列的最新版本
NavigableMap<byte[], NavigableMap<byte[], byte[]>> noVersionMap = result.getNoVersionMap();
//通过指定的列族,获取结果中所有版本
NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap(Bytes.toBytes("colfam1"));
Set<Entry<byte[], byte[]>> entrySet = familyMap.entrySet();
Iterator<Entry<byte[], byte[]>> it = entrySet.iterator();
while(it.hasNext()) {
Entry<byte[], byte[]> next = it.next();
System.out.println("familyMap获取到的键:"+Bytes.toString(next.getKey())+",获取到的值为:"+Bytes.toString(next.getValue()));
}
一次请求,多次获取数据
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "bigdate01:2181,bigdate02:2181,bigdate03:2181");
Connection conn = ConnectionFactory.createConnection(conf);
TableName tableName = TableName.valueOf(Bytes.toBytes("testtable"));
Table table = conn.getTable(tableName);
List<Get> gets=new ArrayList<>();
Get get=new Get(Bytes.toBytes("row-1"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
if(table.exists(get)) {//首先判断表中是否存在请求数据
gets.add(get);
}
Get get1=new Get(Bytes.toBytes("row-1"));
get1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("lib"));
if(table.exists(get1)) {//判断需要获取的数据是否存在
gets.add(get1);
}
Result[] results = table.get(gets);
System.out.println("First iteration 。。。。");
for (Result result : results) {
String row=Bytes.toString(result.getRow());
System.out.println("Row:"+row+" ");
for (Cell cell : result.rawCells()) {
System.out.println("Family:"+Bytes.toString(CellUtil.cloneFamily(cell))+",quailer:"+Bytes.toString(CellUtil.cloneQualifier(cell))+",value:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}