Hbase教程
作者: 时海 风自在
Java API

maven依赖:

注:jar包冲突在hbase中经常遇到,有时需要排除一些依赖


 <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-shaded-client</artifactId>
            <version>1.2.6</version>
            <!--<exclusions>-->
                <!--<exclusion>-->
                    <!--<groupId>org.slf4j</groupId>-->
                    <!--<artifactId>slf4j-log4j12</artifactId>-->
                <!--</exclusion>-->
                <!--<exclusion>-->
                    <!--<groupId>log4j</groupId>-->
                    <!--<artifactId>log4j</artifactId>-->
                <!--</exclusion>-->
            <!--</exclusions>-->
        </dependency>


创建链接


    static Connection connection;

    static {
        try {
            Configuration conf = HBaseConfiguration.create();
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


查询整个表


    public static List<Map<String, String>> queryTable(String tableName) throws IOException {

        List<Map<String, String>> returnResult = new ArrayList<>();

        Table table = connection.getTable(TableName.valueOf(tableName));

        ResultScanner scanner = table.getScanner(new Scan());

        for (Result result : scanner) {

            Map<String, String> item = new HashMap<>();

            String row = new String(result.getRow());

            item.put(Constants.hbaseRowKey, row);

            List<Cell> listCells = result.listCells();
            for (Cell cell : listCells) {

                String key = new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell
                        .getQualifierLength());
                String value = new String(cell.getValueArray(), cell.getValueOffset(), cell
                        .getValueLength());

                if (StringUtils.isBlank(key) || StringUtils.isBlank(value) || "null".equals(value.trim())) {
                    continue;
                }

                item.put(key, value);

            }

            returnResult.add(item);
        }

        return returnResult;

    }


查询单条记录


    public static Map<String, String> getData(String tableName, String rowKey) throws IOException {

        Map<String, String> returnResult = new HashMap<>();

        Table table = connection.getTable(TableName.valueOf(tableName));

        Get get = new Get(rowKey.getBytes());

        Result result = table.get(get);

        if (result.getRow() == null) {
            return null;
        }

        String row = new String(result.getRow());

        returnResult.put(Constants.hbaseRowKey, row);

        List<Cell> listCells = result.listCells();
        for (Cell cell : listCells) {

            String key = new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell
                    .getQualifierLength());
            String value = new String(cell.getValueArray(), cell.getValueOffset(), cell
                    .getValueLength());

            if (StringUtils.isBlank(key) || StringUtils.isBlank(value) || "null".equals(value.trim())) {
                continue;
            }

            if (key.matches("^pre\\d+_")) {
                key = key.substring(key.indexOf("_") + 1);
            }

            returnResult.put(key, value);

        }

        return returnResult;

    }


分页查询


   public static List<Map<String, String>> listCommunityByPage(String tableName, long pageNum, long pageSize) throws IOException {

        List<Map<String, String>> returnResult = new ArrayList<>();
        try {

            Table table = connection.getTable(TableName.valueOf(tableName));

            long skip = pageSize * (pageNum - 1);

            String preRowKey = null;
            boolean stop = false;
            while (skip > 0 && !stop) {
                long step = 0;
                if (skip >= 1000) {
                    step = 1000;
                } else {
                    step = skip;
                }

                skip -= step;

                Filter pageFilter = new PageFilter(step + 1);
                Filter kof = new KeyOnlyFilter();

                List<Filter> filters = new ArrayList<Filter>();
                filters.add(pageFilter);
                filters.add(kof);
                FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL, filters);
                Scan scan = new Scan();
                scan.setFilter(fl);
                if (!StringUtils.isBlank(preRowKey)) {
                    scan.setStartRow(preRowKey.getBytes());
                }

                ResultScanner rs = table.getScanner(scan);
                long count = 0;
                long endIndex = step + 1;
                for (Result result : rs) {
                    count++;
                    if (count == endIndex) {
                        preRowKey = new String(result.getRow());
                    }
                }
                if (count < endIndex) {
                    stop = true;
                }

            }

            if (stop == true) {
                return returnResult;
            }

            Filter pageFilter = new PageFilter(pageSize);
            Scan scan = new Scan();
            scan.setFilter(pageFilter);
            if (!StringUtils.isBlank(preRowKey)) {
                scan.setStartRow(preRowKey.getBytes());
            }

            ResultScanner rs = table.getScanner(scan);

            for (Result result : rs) {

                Map<String, String> item = new HashMap<>();

                String row = new String(result.getRow());

                item.put(Constants.hbaseRowKey, row);

                List<Cell> listCells = result.listCells();
                for (Cell cell : listCells) {

                    String key = new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell
                            .getQualifierLength());
                    String value = new String(cell.getValueArray(), cell.getValueOffset(), cell
                            .getValueLength());

                    if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
                        continue;
                    }

                    if (key.matches("^pre\\d+_")) {
                        key = key.substring(key.indexOf("_") + 1);
                    }

                    item.put(key, value);

                }

                returnResult.add(item);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return returnResult;
    }



注意到上述有一个常量:

public class Constants {

    public static String hbaseRowKey = "_row_id";

}

标签: cell、returnresult、scan、listcells、string
一个创业中的苦逼程序员
  • 回复
隐藏