依赖
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.4.11</version> </dependency>
代码
HbaseCell
public class HbaseCell { private long timestamp; private String family; private String rowKey; private String column; private String value;
import com.jpush.hbase.pojo.HbaseCell; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Component public class HbaseComponent { private static final Logger log = LoggerFactory.getLogger(HbaseComponent.class); @Autowired private Connection connection; public HbaseCell get(String tableName, String rowKey) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); log.info(get.toString()); Result result = table.get(get); List<Cell> cells = result.listCells(); for (Cell cell : cells) { HbaseCell hbaseCell = cellToHbaseCell(cell); return hbaseCell; } return null; } public HbaseCell get(String tableName, String rowKey,long minStamp, long maxStamp) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); get.setTimeRange(minStamp,maxStamp); log.info(get.toString()); Result result = table.get(get); List<Cell> cells = result.listCells(); for (Cell cell : cells) { HbaseCell hbaseCell = cellToHbaseCell(cell); return hbaseCell; } return null; } public HbaseCell get(String tableName, String rowKey,Long minStamp, Long maxStamp,Integer version) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); if (minStamp != null && maxStamp != null){ get.setTimeRange(minStamp,maxStamp); } if (version != null){ get.readVersions(version); } log.info(get.toString()); Result result = table.get(get); List<Cell> cells = result.listCells(); for (Cell cell : cells) { HbaseCell hbaseCell = cellToHbaseCell(cell); return hbaseCell; } return null; } public List<HbaseCell> mget(String tableName, List<String> rowkeys) throws IOException { log.info("rowkeys:{}",rowkeys); List<Get> getList = new ArrayList<>(); for (String rowkey : rowkeys) { Get get = new Get(rowkey.getBytes()); getList.add(get); } Table table = connection.getTable(TableName.valueOf(tableName)); Result[] results = table.get(getList); List<HbaseCell> hbaseCells = getHbaseCells(results); return hbaseCells; } public List<HbaseCell> mget(String tableName, List<String> rowkeys, Long minStamp, Long maxStamp) throws IOException { log.info("rowkeys:{}",rowkeys); List<Get> getList = new ArrayList<>(); for (String rowkey : rowkeys) { Get get = new Get(rowkey.getBytes()); if (minStamp != null && maxStamp != null){ get.setTimeRange(minStamp,maxStamp); } getList.add(get); } Table table = connection.getTable(TableName.valueOf(tableName)); Result[] results = table.get(getList); List<HbaseCell> hbaseCells = getHbaseCells(results); return hbaseCells; } private List<HbaseCell> getHbaseCells(Result[] results){ List<HbaseCell> hbaseCells = new ArrayList<>(); for (Result result : results) { List<Cell> cells = result.listCells(); for (Cell cell : cells) { HbaseCell hbaseCell = cellToHbaseCell(cell); hbaseCells.add(hbaseCell); } } return hbaseCells; } private List<HbaseCell> getHbaseCells(ResultScanner scanner ){ List<HbaseCell> hbaseCells = new ArrayList<>(); for (Result result : scanner) { List<Cell> cells = result.listCells(); for (Cell cell : cells) { HbaseCell hbaseCell = cellToHbaseCell(cell); hbaseCells.add(hbaseCell); } } return hbaseCells; } public List<HbaseCell> scan(String tableName,String startKey,String endKey,int version) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); scan.withStartRow(startKey.getBytes()); scan.withStopRow(endKey.getBytes()); scan.readVersions(version); log.info("scan:{}",scan); ResultScanner scanner = table.getScanner(scan); List<HbaseCell> hbaseCells = getHbaseCells(scanner); return hbaseCells; } public List<HbaseCell> scan(String tableName,String startKey,String endKey) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); scan.withStartRow(startKey.getBytes()); scan.withStopRow(endKey.getBytes()); log.info("scan:{}",scan); ResultScanner scanner = table.getScanner(scan); List<HbaseCell> hbaseCells = getHbaseCells(scanner); return hbaseCells; } private HbaseCell cellToHbaseCell(Cell cell ){ HbaseCell hbaseCell = new HbaseCell(); hbaseCell.setValue(Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); hbaseCell.setTimestamp(cell.getTimestamp()); hbaseCell.setFamily(Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength())); hbaseCell.setColumn(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())); hbaseCell.setRowKey(Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength())); return hbaseCell; } }