/** * 遍历多行 * * @param tableName 表名 * @param start_rowkey 开始行键 * @param stop_rowkey 结束行键 * @return 行列表 */ public ArrayList<HbaseRow> scanRows(String tableName, String start_rowkey, String stop_rowkey) { ResultScanner rowstmp = null; ArrayList<HbaseRow> rows = null; try { Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(start_rowkey)); scan.setStopRow(Bytes.toBytes(stop_rowkey)); HTable table = new HTable(conf, Bytes.toBytes(tableName)); rowstmp = table.getScanner(scan); rows = new ArrayList<>(); for (Result rowtmp : rowstmp) { HbaseRow row = new HbaseRow(); row.rowkey = Bytes.toString(rowtmp.getRow()); for (Cell cell : rowtmp.listCells()) { HbaseColumn col = new HbaseColumn(cell); row.cols.add(col); } rows.add(row); } } catch (Exception e) { logger.error("scanRows failed", e); } finally { rowstmp.close(); } return rows; }
/** * 查询单行 * * @param tableName 表名 * @param rowKey 行键名 * @return 行 */ public HbaseRow getRow(String tableName, String rowKey) { HbaseRow row = null; try { Get get = new Get(Bytes.toBytes(rowKey)); HTable table = new HTable(conf, Bytes.toBytes(tableName)); // 获取表 Result rowtmp = table.get(get); row = new HbaseRow(); row.rowkey = Bytes.toString(rowtmp.getRow()); for (Cell cell : rowtmp.listCells()) { HbaseColumn col = new HbaseColumn(cell); row.cols.add(col); } } catch (Exception e) { logger.error("getRow failed", e); } return row; }