网上找了很多都没有找到类似的,于是自己想办法,只能大概满足要求吧,并不能确切的定位到哪一条数据,
具体的思路是:通过scan,设置timeRange,需要起始时间和结束时间,结束时间可以定为当前时间,起始时间可以定为当前时间前一天(时间粒度可以更改为小时,分钟,或者秒,粒度越细,结果越准确),然后去scan,判断是否扫描到数据,如果没有,则把起始时间再往前一天,直到扫描到数据为止。此方法只能大概定位到哪个时间范围。
public static void queryDataTime2(String tableName) throws IOException, ParseException {
TableName name = TableName.valueOf(tableName);
Table table = connection.getTable(name);
Scan scan = new Scan();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = sdf.parse("2019-07-24 00:00:00");
long now = parse.getTime();
long start = 0L;
Calendar c = Calendar.getInstance();
c.setTime(parse);
ResultScanner rs = null;
while (true){
c.add(Calendar.HOUR_OF_DAY, -1);
start = c.getTimeInMillis();
scan.setTimeRange(start, now);
try {
rs = table.getScanner(scan);
Iterator<Result> iterator = rs.iterator();
System.out.println(new Date(start));
if (iterator.hasNext()){
System.out.println("扫描结束 "+new Date(start));
break;
}
} catch (IOException e) {
System.out.println("遍历查询指定表中的所有数据失败");
e.printStackTrace();
}
}
}