微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

hbase 工具

依赖

 <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.4.11</version>
        </dependency>

 

代码

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐