public List<UserInfo> stateList(String userid) throws IOException{
HBaseUtils hBaseUtils = HBaseUtils.getInstance();
Configuration conf = hBaseUtils.getConfiguration();
HTable table = new HTable(conf, "iboss_userinfo");
// 要查询的字段
Scan scan = new Scan();
//设置过滤器
FilterList filterList = new FilterList(Operator.MUST_PASS_ALL);
// where条件
if (StringUtils.isNotEmpty(userid)) {
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("userinfo"),Bytes.toBytes("userid"), CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes(userid)));
filterList.addFilter(filter);
}
scan.setFilter(filterList);
ResultScanner rs = table.getScanner(scan);
String RowKey=null;
for (Result r : rs) {
for (KeyValue kv : r.raw()) {
RowKey=Bytes.toString(kv.getRow());
}
}
System.out.println(RowKey);
if (StringUtils.isEmpty(RowKey)) {
return null;
}
Get get = new Get(Bytes.toBytes(RowKey));
get.setMaxVersions(10); // 设置一次性获取多少个版本的数据
get.addColumn(Bytes.toBytes("userinfo"), Bytes.toBytes("state"));
get.addColumn(Bytes.toBytes("userinfo"), Bytes.toBytes("platform"));
//获得结果
Result result = table.get(get);
List<Cell> cells = result.listCells();
List<UserInfo> list = new ArrayList<UserInfo>();
if(null != cells && !cells.isEmpty()){
for(Cell ce:cells){
UserInfo userInfo = new UserInfo();
userInfo.setRowKey(Bytes.toString(ce.getRow()));
userInfo.setState(Bytes.toString(ce.getValue()));
System.out.println(Bytes.toString(ce.getValueArray()));
String dateString = DateUtil.Date2Str(new Date(ce.getTimestamp()));
userInfo.setTimestamp(dateString);
list.add(userInfo);
}
}
return list;
}
hbase 示例1 状态列表
最新推荐文章于 2023-11-28 22:03:15 发布