请问hbase2.x 想获取hbase表列簇中所有的列,除了遍历所有数据以外还有其他方法么?
是的,HBase提供了Get类和Scan类,可以用来获取某个特定列簇下面的所有列。
以Get类为例,步骤如下:
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "localhost");
conf.setInt("hbase.zookeeper.property.clientPort", 2181);
Connection conn = ConnectionFactory.createConnection(conf);
TableName tableName = TableName.valueOf("testTable");
Table table = conn.getTable(tableName);
Get get = new Get(Bytes.toBytes("rowKey"));
get.addFamily(Bytes.toBytes("familyName"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
byte[] qualifier = CellUtil.cloneQualifier(cell);
// 判断列名是否符合要求,符合则进行处理
}
注意,如果要获取所有列,可以不设置addColumn方法。例如:
Get get = new Get(Bytes.toBytes("rowKey"));
get.addFamily(Bytes.toBytes("familyName"));
这样所有的列都会被读取,稍后再进行处理即可。