通过使用PageFilter实现分页
public class PageFilterText {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
// 只要设置hbase所用的zookeeper集群的地址,客户端就能获得hbase的所有节点相关信息c121,c120,c123
conf.set(“hbase.zookeeper.quorum”, “c120:2181,c121:2181,c123:2181”);
// conn = ConnectionFactory.createConnection(conf);
// Configuration conf = new Configuration();
// Configuration conf = HBaseConfiguration. create ();
// Table table = conn.getTable(TableName.valueOf(“t_user_info”));
HTable hTable = new HTable(conf, “sjfbj:_MetricUID”);
long pageSize = 7;
int totalRowsCount = 0;
PageFilter filter = new PageFilter(pageSize);
byte[] lastRow = null;
while(true) {
Scan scan = new Scan();
scan.setFilter(filter);
if(lastRow != null) {
byte[] postfix = Bytes.toBytes("postfix");
//加个后缀,可以防止重复拿到上一次的最后一条数据
byte[] startRow = Bytes.add(lastRow, postfix);
scan.setStartRow(startRow);
System.out.println(“start row : ” + Bytes.toString(startRow));
}
ResultScanner scanner = hTable.getScanner(scan);
int localRowsCount = 0;
for(Result result : scanner) {
System.out.println(localRowsCount++ + " : " + result);
totalRowsCount++;
lastRow = result.getRow(); // ResultScanner 的结果集是排序好的,这样就可以取到最后一个 row 了
}
scanner.close();
if(localRowsCount == 0) break;
}
hTable.close();
System.out.println("total rows is : " + totalRowsCount);
}
}
通过next(页数)实现分页
public class CrashLogTable extends HBaseDAO {
private static final Log LOG = LogFactory.getLog(CrashLogTable.class);
private static final String TABLE_NAME = "crashLog";
private static final byte[] CLIENT_APP_VER_Q = Bytes.toBytes("cav");
private static final byte[] STACK_Q = Bytes.toBytes("st");
private static final byte[] DEVICE_MODEL_Q = Bytes.toBytes("dm");
private static final byte[] OS_Q = Bytes.toBytes("os");
private static final byte[] CRASH_TIME_Q = Bytes.toBytes("ct");
private static final byte[] RANDOM_NUM_Q = Bytes.toBytes("rn");
public CrashLogTable(AnalyticsConf conf) throws IOException {
super(conf, TABLE_NAME);
// TODO Auto-generated constructor stub
}
public void insert(long appID, Error error, Message message, long versionUID) throws IOException {
UUID uuid = UUID.randomUUID();
int hashCode = uuid.hashCode();
int randomNum = Math.abs(hashCode);
long version = this.getCurrVersion();
byte[] rowKey = HbaseBytes.add(Bytes.toBytes(appID), Bytes.toBytes(error.getUID()),
Bytes.toBytes(Long.MAX_VALUE - error.getTimestamp().getTimeInMillis()), Bytes.toBytes(randomNum));
Put put = new Put(rowKey);
put.add(Constant.DEFAULT_TABLE_FAMILY, STACK_Q, version, Bytes.toBytes(error.getStack()));
put.add(Constant.DEFAULT_TABLE_FAMILY, CLIENT_APP_VER_Q, version, Bytes.toBytes(versionUID));
put.add(Constant.DEFAULT_TABLE_FAMILY, DEVICE_MODEL_Q, version, Bytes.toBytes(message.getModelUID()));
put.add(Constant.DEFAULT_TABLE_FAMILY, OS_Q, version, Bytes.toBytes(message.getOsUID()));
put.add(Constant.DEFAULT_TABLE_FAMILY, CRASH_TIME_Q, version,
Bytes.toBytes(error.getTimestamp().getTimeInMillis()));
put.add(Constant.DEFAULT_TABLE_FAMILY, RANDOM_NUM_Q, version, Bytes.toBytes(randomNum));
getTable().put(put);
}
public ArrayList<CrashLog> scan(long appID, long crashID, long startTime, long random, long size)
throws IOException {
// TODO Auto-generated method stub
Result result = new Result();
// TreeMap<String, CrashLog> crashes = new TreeMap<String, CrashLog>();
ArrayList<CrashLog> crashes = new ArrayList<CrashLog>();
byte[] startKey;
byte[] endKey;
if (startTime == 0 && random == 0) {
startKey = Bytes.add(Bytes.toBytes(appID), Bytes.toBytes(crashID));
endKey = Bytes.add(Bytes.toBytes(appID), Bytes.toBytes(crashID),Bytes.toBytes(Long.MAX_VALUE));
} else {
startKey = HbaseBytes.add(Bytes.toBytes(appID), Bytes.toBytes(crashID),
Bytes.toBytes(Long.MAX_VALUE - startTime), Bytes.toBytes(random));
endKey = Bytes.add(Bytes.toBytes(appID), Bytes.toBytes(crashID),Bytes.toBytes(Long.MAX_VALUE)
);
}
Scan scan = new Scan(startKey, endKey);
scan.setCaching(1024);
ResultScanner scanner = getTable().getScanner(scan);
Result[] results = scanner.next((int) size);
try {
for (int i = 0; i < results.length; i++) {
// if (startTime == 0 && random == 0) {
// if (i == results.length - 1) {
// result = results[i];
// }
// result = results[i - 1];
//
// }
// else {
// result = results[i];
// }
result = results[i];
byte[] crashTime = result.getValue(Constant.DEFAULT_TABLE_FAMILY, CRASH_TIME_Q);
byte[] stack = result.getValue(Constant.DEFAULT_TABLE_FAMILY, STACK_Q);
byte[] version = result.getValue(Constant.DEFAULT_TABLE_FAMILY, CLIENT_APP_VER_Q);
byte[] randomNum = result.getValue(Constant.DEFAULT_TABLE_FAMILY, RANDOM_NUM_Q);
byte[] deviceModel = result.getValue(Constant.DEFAULT_TABLE_FAMILY, DEVICE_MODEL_Q);
byte[] os = result.getValue(Constant.DEFAULT_TABLE_FAMILY, OS_Q);
CrashLog crashLog = new CrashLog();
crashLog.setCrashTime(Bytes.toLong(crashTime));
crashLog.setClientAppVer(Bytes.toLong(version));
crashLog.setDeviceModel(Bytes.toLong(deviceModel));
crashLog.setOs(Bytes.toLong(os));
crashLog.setRandomNum(Bytes.toInt(randomNum));
crashLog.setStack(Bytes.toString(stack));
crashes.add(crashLog);
// crashes.put(String.valueOf(Bytes.toLong(crashTime))+String.valueOf(Bytes.toLong(randomNum)),
// crashLog);
}
return crashes;
} finally {
scanner.close();
}
}
public CrashLog get(long appID, long crashID, long startTime, int random) throws IOException {
// TODO Auto-generated method stub
byte[] rowKey = HbaseBytes.add(Bytes.toBytes(appID), Bytes.toBytes(crashID),
Bytes.toBytes(Long.MAX_VALUE - startTime), Bytes.toBytes(random));
Get get = new Get(rowKey);
Result result = getTable().get(get);
if (result.isEmpty())
return null;
byte[] crashTime = result.getValue(Constant.DEFAULT_TABLE_FAMILY, CRASH_TIME_Q);
byte[] stack = result.getValue(Constant.DEFAULT_TABLE_FAMILY, STACK_Q);
byte[] version = result.getValue(Constant.DEFAULT_TABLE_FAMILY, CLIENT_APP_VER_Q);
byte[] randomNum = result.getValue(Constant.DEFAULT_TABLE_FAMILY, RANDOM_NUM_Q);
byte[] deviceModel = result.getValue(Constant.DEFAULT_TABLE_FAMILY, DEVICE_MODEL_Q);
byte[] os = result.getValue(Constant.DEFAULT_TABLE_FAMILY, OS_Q);
CrashLog crashLog = new CrashLog();
crashLog.setCrashTime(Bytes.toLong(crashTime));
crashLog.setClientAppVer(Bytes.toLong(version));
crashLog.setDeviceModel(Bytes.toLong(deviceModel));
crashLog.setOs(Bytes.toLong(os));
crashLog.setRandomNum(Bytes.toInt(randomNum));
crashLog.setStack(Bytes.toString(stack));
return crashLog;
}
}