Hbase 根据设备ID等,倒序分页查询(java)
相关说明:
- hbase没有专门用于分页的语句,需要自己维护每一页的
StartRowKey
。 - 可通过设置
scan.setReversed(true)
,进行倒序扫描。 - 可通过
new FilterList(FilterList.Operator.MUST_PASS_ALL);
设置多维过滤。 - 可通过
new PageFilter(pageSize);
设置分页过滤器过滤。
1、版本1:
每页的开始行健
- 第一页:
String startRow = endKey
- 第 n 页:
- 先,通过
int scanDatas = (pageNum - 1) * pageSize + 1;
计算当前页,前面需要扫描的总行数; - 再,通过
new PageFilter(scanDatas);
扫描该页前面的数据; - 然后,通过 下面的遍历,得到当前页的开始rowkey
- 最后,与第一页一样。
- 先,通过
for (Result result : scanner1) {
byte[] row_bytes = result.getRow();
startRow = Bytes.toString(row_bytes);
//获取前一页最后一个rowkey
}
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class pageScan12 {
private Connection connection;
private Table table ;
private String tableName = "test_io";
private String iotID="AacW6cTXG2";
private String column="AacW6cTXG20";
String startKey = "1577810000";
String endKey = "1577810100";
int pageNum = 2;//页码
int pageSize = 10;//每页的大小
@BeforeTest
public void init() throws IOException {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
connection = ConnectionFactory.createConnection(configuration);
//获得表
table = connection.getTable(TableName.valueOf(tableName));
}
@AfterTest
public void close() throws IOException {
table.close();
connection.close();
}
//分页过滤器
@Test
public void pageFilter12() throws IOException {
Scan scan = new Scan();
scan.setReversed(true);//倒序扫描
//---------------多维过滤查询 条件设置-------------------------
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
//------------ 查询 diviceid = “XXX”的某一行-------------
SingleColumnValueFilter deviceid1 = new SingleColumnValueFilter("fdata".getBytes(), "deviceid".getBytes(),CompareFilter.CompareOp.EQUAL, iotID.getBytes());
filterList.addFilter(deviceid1);
// -------------查询column列
SubstringComparator substringComparator = new SubstringComparator(column);
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, substringComparator);
filterList.addFilter(qualifierFilter);
String startRow = endKey;//倒序扫描时,startRow = endKey
if(pageNum == 1) {
//获取第一页的数据
scan.setStartRow(startRow.getBytes());
PageFilter pageFilter = new PageFilter(pageSize);
filterList.addFilter(pageFilter);
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
printlReult(scanner);
} else {
//如果所读分页不是第一页
int scanDatas = (pageNum - 1) * pageSize + 1;
scan.setStartRow(startRow.getBytes());
PageFilter pageFilter = new PageFilter(scanDatas);
filterList.addFilter(pageFilter);
scan.setFilter(filterList);
ResultScanner scanner1 = table.getScanner(scan