hbase 工具


依赖

 <dependency>
            <groupId>org.apache.hbasegroupId>
            <artifactId>hbase-clientartifactId>
            <version>2.4.11version>
        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 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 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 cells = result.listCells();
        for (Cell cell : cells) {
            HbaseCell hbaseCell = cellToHbaseCell(cell);
            return hbaseCell;
        }
        return null;
    }


    public List mget(String tableName, List rowkeys) throws IOException {
        log.info("rowkeys:{}",rowkeys);
        List 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 hbaseCells = getHbaseCells(results);
        return hbaseCells;
    }

    public List mget(String tableName, List rowkeys, Long minStamp, Long maxStamp) throws IOException {
        log.info("rowkeys:{}",rowkeys);
        List 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 hbaseCells = getHbaseCells(results);
        return hbaseCells;
    }

    private List getHbaseCells(Result[] results){
        List hbaseCells = new ArrayList<>();
        for (Result result : results) {
            List cells = result.listCells();
            for (Cell cell : cells) {
                HbaseCell hbaseCell = cellToHbaseCell(cell);
                hbaseCells.add(hbaseCell);
            }
        }
        return hbaseCells;
    }

    private List getHbaseCells(ResultScanner scanner ){
        List hbaseCells = new ArrayList<>();
        for (Result result : scanner) {
            List cells = result.listCells();
            for (Cell cell : cells) {
                HbaseCell hbaseCell = cellToHbaseCell(cell);
                hbaseCells.add(hbaseCell);
            }
        }
        return hbaseCells;
    }

    public List 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 hbaseCells = getHbaseCells(scanner);

        return hbaseCells;
    }

    public List 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 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;
    }
}

相关