Hbase 分佈查詢 - 應用後臺程式控制分頁
原理是後臺查詢出所有的資料,然後在後臺封裝分頁物件,其中可以設定過濾器等。
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseUtils {
private static Configuration config = null;
private static HTablePool tp = null;
static {
// 載入叢集配置
config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "xx.xx.xx");
config.set("hbase.zookeeper.property.clientPort", "2181");
// 建立表池(可偉略提高查詢效能,具體說明請百度或官方API)
tp = new HTablePool(config, 10);
}
/*
* 獲取hbase的表
*/
public static HTableInterface getTable(String tableName) {
if (StringUtils.isEmpty(tableName))
return null;
return tp.getTable(getBytes(tableName));
}
/* 轉換byte陣列 */
public static byte[] getBytes(String str) {
if (str == null)
str= "";
return Bytes.toBytes(str);
}
/**
* 查詢資料
* @param tableKey 表標識
* @param queryKey 查詢標識
* @param startRow 開始行
* @param paramsMap 引數集合
* @return 結果集
*/
public static TBData getDataMap(String tableName, String startRow,
String stopRow, Integer currentPage, Integer pageSize)
throws IOException {
List<Map<String, String>>mapList = null;
mapList = new LinkedList<Map<String,String>>();
ResultScanner scanner = null;
// 為分頁建立的封裝類物件,下面有給出具體屬性
TBData tbData = null;
try {
// 獲取最大返回結果數量
if (pageSize == null || pageSize == 0L)
pageSize = 100;
if (currentPage == null || currentPage == 0)
currentPage = 1;
// 計算起始頁和結束頁
Integer firstPage = (currentPage - 1) * pageSize;
Integer endPage = firstPage + pageSize;
// 從表池中取出HBASE表物件
HTableInterface table = getTable(tableName);
// 獲取篩選物件
Scan scan = getScan(startRow, stopRow);
// 給篩選物件放入過濾器(true標識分頁,具體方法在下面)
scan.setFilter(packageFilters(true));
// 快取1000條資料
scan.setCaching(1000);
scan.setCacheBlocks(false);
scanner= table.getScanner(scan);
int i = 0;
List<byte[]> rowList = new LinkedList<byte[]>();
// 遍歷掃描器物件, 並將需要查詢出來的資料row key取出
for (Result result : scanner) {
String row = toStr(result.getRow());
if (i >= firstPage && i< endPage) {
rowList.add(getBytes(row));
}
i++;
}
// 獲取取出的row key的GET物件
List<Get>getList = getList(rowList);
Result[]results = table.get(getList);
// 遍歷結果
for (Result result : results) {
Map<byte[], byte[]> fmap = packFamilyMap(result);
Map<String, String> rmap = packRowMap(fmap);
mapList.add(rmap);
}
// 封裝分頁物件
tbData= new TBData();
tbData.setCurrentPage(currentPage);
tbData.setPageSize(pageSize);
tbData.setTotalCount(i);
tbData.setTotalPage(getTotalPage(pageSize, i));
tbData.setResultList(mapList);
} catch (IOException e) {
e.printStackTrace();
} finally {
closeScanner(scanner);
}
return tbData;
}
private static int getTotalPage(int pageSize, int totalCount) {
int n = totalCount / pageSize;
if (totalCount % pageSize == 0) {
return n;
} else {
return ((int) n) + 1;
}
}
// 獲取掃描器物件
private static Scan getScan(String startRow,String stopRow) {
Scan scan = new Scan();
scan.setStartRow(getBytes(startRow));
scan.setStopRow(getBytes(stopRow));
return scan;
}
/**
* 封裝查詢條件
*/
private static FilterList packageFilters(boolean isPage) {
FilterList filterList = null;
// MUST_PASS_ALL(條件 AND) MUST_PASS_ONE(條件OR)
filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
Filter filter1 = null;
Filter filter2 = null;
filter1 = newFilter(getBytes("family1"), getBytes("column1"),
CompareOp.EQUAL, getBytes("condition1"));
filter2 = newFilter(getBytes("family2"), getBytes("column1"),
CompareOp.LESS, getBytes("condition2"));
filterList.addFilter(filter1);
filterList.addFilter(filter2);
if (isPage) {
filterList.addFilter(new FirstKeyOnlyFilter());
}
return filterList;
}
private static Filter newFilter(byte[] f, byte[] c, CompareOp op, byte[] v) {
return new SingleColumnValueFilter(f, c, op,v);
}
private static void closeScanner(ResultScanner scanner) {
if (scanner != null)
scanner.close();
}
/**
* 封裝每行資料
*/
private static Map<String, String> packRowMap(Map<byte[], byte[]> dataMap) {
Map<String, String> map = new LinkedHashMap<String, String>();
for (byte[] key : dataMap.keySet()) {
byte[] value = dataMap.get(key);
map.put(toStr(key), toStr(value));
}
return map;
}
/* 根據ROW KEY集合獲取GET物件集合 */
private static List<Get> getList(List<byte[]> rowList) {
List<Get> list = new LinkedList<Get>();
for (byte[] row : rowList) {
Get get = new Get(row);
get.addColumn(getBytes("family1"), getBytes("column1"));
get.addColumn(getBytes("family1"), getBytes("column2"));
get.addColumn(getBytes("family2"), getBytes("column1"));
list.add(get);
}
return list;
}
/**
* 封裝配置的所有欄位列族
*/
private static Map<byte[], byte[]> packFamilyMap(Result result){
Map<byte[], byte[]> dataMap = null;
dataMap = new LinkedHashMap<byte[], byte[]>();
dataMap.putAll(result.getFamilyMap(getBytes("family1")));
dataMap.putAll(result.getFamilyMap(getBytes("family2")));
return dataMap;
}
private static String toStr(byte[] bt) {
return Bytes.toString(bt);
}
public static void main(String[] args) throws IOException {
// 拿出row key的起始行和結束行
// #<0<9<:
String startRow = "aaaa#";
String stopRow = "aaaa:";
int currentPage = 1;
int pageSize = 20;
// 執行hbase查詢
getDataMap("table", startRow, stopRow, currentPage,pageSize);
}
}
下面物件是分佈包裝物件,
import java.util.List;
import java.util.Map;
class TBData {
private Integer currentPage;
private Integer pageSize;
private Integer totalCount;
private Integer totalPage;
private List<Map<String, String>> resultList;
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount){
this.totalCount = totalCount;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List<Map<String, String>> getResultList() {
return resultList;
}
public void setResultList(List<Map<String, String>> resultList) {
this.resultList = resultList;
}
}
相關文章
- HBase學習之二: hbase分頁查詢
- 分庫分表後的分頁查詢
- 分頁查詢及其擴充應用案例
- Elasticsearch 分頁查詢Elasticsearch
- 百億級資料 分庫分表 後怎麼分頁查詢?
- 百億級資料分表後怎麼分頁查詢?
- 億萬級分庫分表後如何進行跨表分頁查詢
- NET 集合分頁查詢
- AntDesignBlazor示例——分頁查詢Blazor
- MySQL的分頁查詢MySql
- ThinkPhp框架:分頁查詢PHP框架
- 分頁查詢優化優化
- Oracle總結【SQL細節、多表查詢、分組查詢、分頁】OracleSQL
- MySQL分頁查詢優化MySql優化
- indexdb實現分頁查詢Index
- Yii2:ArrayDataProvider 小改動後支援分頁查詢IDE
- elasticsearch查詢之大資料集分頁查詢Elasticsearch大資料
- VUE-表格佈局、表格查詢、工具欄、表格、分頁欄Vue
- 分頁實現前臺後臺不同效果,分頁類引入
- MySQL——優化巢狀查詢和分頁查詢MySql優化巢狀
- SSH框架下的分頁查詢框架
- 菜品條件分頁查詢
- 【記錄】SSH分頁查詢功能
- 關於 groupBy 分組查詢的分頁處理
- 小書MybatisPlus第4篇-表格分頁與下拉分頁查詢MyBatis
- SSM框架實現分頁查詢例子SSM框架
- MySQL分優化之超大頁查詢MySql優化
- Hibernate5.1+Sqlserver2000分頁查詢SQLServer
- c# winform 實現分頁查詢C#ORM
- (MySQL學習筆記)分頁查詢MySql筆記
- SpringMVC+Spring Data JPA +Bootstrap 分頁實現和模糊查詢分頁SpringMVCboot
- 淺談HBase的資料分佈
- 資料庫全表查詢之-分頁查詢優化資料庫優化
- 使用SSH完成條件及分頁查詢的主要程式碼
- 查詢——二分查詢
- 使用Mybatis-plus進行分頁查詢,沒有分頁效果,查詢的資料量超出每頁數量設定MyBatis
- 如何優雅地實現分頁查詢
- Oracle資料庫中的分頁查詢Oracle資料庫
- ElasticSearch - 分頁查詢方式二 【scroll】滾動查詢(kibana、Java示例)ElasticsearchJava