java中Hbase各种查询
根据列族名,key,范围查询等
网上看到很多Hbase查询,没找到一个相对全面的,这里我来总结一下吧
声明下面conn是什么
public static Connection conn;
static{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", );
conf.set("hbase.zookeeper.quorum", );
conf.set("hbase.client.scanner.timeout.period", ));
conf.set("hbase.rpc.timeout", );
conn = ConnectionFactory.createConnection(conf);
}
根据表名获取所有的key
参数:
String tableName 表名
Table table = conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
List<String> list = new ArrayList<>();
for(Result result:results){
list.add(new String(result.getRow()));
}
根据key查询一条数据
参数:
String tableName 表名
String rowKey 表的key(id)
Table table = conn.getTable(TableName.valueOf(tableName));
byte[] row = Bytes.toBytes(rowKey);
Get get = new Get(row);
Result r = table.get(get);
if(null != r && !r.isEmpty()){
for (Cell cell : r.listCells()){
//列族名
String family = Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
//列名
String key = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
//列值
String value = Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength());
}
}
根据表名,KEY和列族名 查询一行这个列族中列的数量
参数:
String tableName 表名
String columnFamily 列族名
String rowKey 表的key(id)
Table table = conn.getTable(TableName.valueOf(tableName));
byte[] row = Bytes.toBytes(rowKey);
Get get = new Get(row);
Result result = table.get(get);
if(null != result && !result.isEmpty()){
Map<byte[], byte[]> familyMap = result.getFamilyMap(Bytes.toBytes(columnFamily));
return familyMap.size();
}
根据表名和列族名 查询每行这个列族中列的数量
参数:
String tableName 表名
String columnFamily 列族名
Table table = conn.getTable(TableName.valueOf(tableName));
ResultScanner results = table.getScanner(new Scan());
Map<String,Integer> map = new HashMap<>();
for (Result result:results){
String key = new String(result.getRow());
Map<byte[], byte[]> familyMap = result.getFamilyMap(Bytes.toBytes(columnFamily));
//familyMap.size()是columnFamily这个列族中列的数量
map.put(key,familyMap.size());
}
List<Map.Entry<String,Integer>> list = new ArrayList<>();
list.addAll(map.entrySet());
根据KEY的范围查询数据
参数:
String tableName 表名
String startRowKey
String endRowKey
Table table = conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRowKey));
scan.setStopRow(Bytes.toBytes(endRowKey));
ResultScanner results = table.getScanner(scan);
Map<String,Result> map = new HashMap<>();
for (Result result : results){
String key = new String(result.getRow());
map.put(key,result);
}
List<Map.Entry> list = new ArrayList<>();
list.addAll(map.entrySet());
根据时间戳查询 时间范围内的数据
Table table = conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.setTimeRange(beginTime,endTime);
ResultScanner results = table.getScanner(scan);
Map<String,Object> map = new HashMap<>();
for (Result result : results){
String key = new String(result.getRow());
map.put(key,result);
}
List<Map.Entry> list = new ArrayList<>();
list.addAll(map.entrySet());
根据查询符合范围的数据
Table table = conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
List<Filter> filters = new ArrayList<Filter>();
Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),Bytes.toBytes(column),
CompareFilter.CompareOp.GREATER_OR_EQUAL,Bytes.toBytes(begin));
Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),Bytes.toBytes(column),
CompareFilter.CompareOp.LESS,Bytes.toBytes(end));
filters.add(filter1);
filters.add(filter2);
FilterList filterList = new FilterList(filters);
scan.setFilter(filterList);
ResultScanner results = table.getScanner(scan);
Map<String,Result> map = new HashMap<>();
for(Result result:results){
String key = new String(result.getRow());
map.put(key,result);
}
List<Map.Entry> list = new ArrayList<>();
list.addAll(map.entrySet());
根据条件查询数据
Table table = conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
List<Filter> filters = new ArrayList<Filter>();
columnAndValue.forEach((k,v)->{
Filter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),Bytes.toBytes(k),
CompareFilter.CompareOp.EQUAL,Bytes.toBytes(v));
filters.add(filter);
});
FilterList filterList = new FilterList(filters);
scan.setFilter(filterList);
ResultScanner results = table.getScanner(scan);
Map<String,Result> map = new HashMap<>();
for(Result result:results){
String key = new String(result.getRow());
map.put(key,result);
}
List<Map.Entry> list = new ArrayList<>();
list.addAll(map.entrySet());