使用scan读取hbase的前三条数据,并且将空值赋值为null

弄了三个小时,终于弄好了,纪念一下吧,也算没白熬夜

public List<String> getHBasePreview(Integer id, String tableName) {
        List<String> list = new ArrayList<>();
        List<List<String>> list1 = new ArrayList<>();
        Configuration config = HBaseConfiguration.create();
        Connection connection = null;
        Table table = null;
        try {
            DataSource dataSource = dataSourceAllService.getDBSource(id);
            config.set("hbase.client.retries.number", "2");
            config.set("zookeeper.session.timeout", "15000");
            if (!StringUtils.isEmpty(dataSource.getHost()) && dataSource.getPort() != null) {
                config.set("hbase.zookeeper.property.clientPort", String.valueOf(dataSource.getPort()));
                config.set("hbase.zookeeper.quorum", dataSource.getHost());
            } else {
                LinkedHashMap<String, Object> hbaseMap = JSONObject.parseObject(dataSource.getSetMap(), LinkedHashMap.class);
                config.addResource(new Path(String.valueOf(hbaseMap.get("hbase-site.xml"))));
            }
            connection = ConnectionFactory.createConnection(config);
            table = connection.getTable(TableName.valueOf(tableName));
            Scan scan = new Scan();
            scan.setCaching(1);
            Filter filter = new PageFilter(3);
            scan.setFilter(filter);
            ResultScanner resultScanner = table.getScanner(scan);

        list1 = showCell(resultScanner);
        for (List<String> a : list1){
            list.add(a.toString().substring(1,a.toString().length() - 1));
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        if (table != null) {
            try {
                table.close();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    return list;
}
public List<List<String>> showCell(ResultScanner resultScanner){
    Map<String,String> row1 = new HashMap<>();
    Map<String,String> row2 = new HashMap<>();
    Map<String,String> row3 = new HashMap<>();
    List<List<String>> columnValue = new ArrayList<>();//返回的list集合
    List<Map<String,String>> rows = new ArrayList<>();//存放前三条数据的集合

    int x = 0;
    for (Result result:resultScanner){
        x++;
        Cell[] cells = result.rawCells();
        for (Cell cell:cells){
           if (x == 1){
               row1.put(Bytes.toString(CellUtil.cloneFamily(cell)) + "_" + Bytes.toString(CellUtil.cloneQualifier(cell)),Bytes.toString(CellUtil.cloneValue(cell)));
           }
           if (x == 2){
               row2.put(Bytes.toString(CellUtil.cloneFamily(cell)) + "_" + Bytes.toString(CellUtil.cloneQualifier(cell)),Bytes.toString(CellUtil.cloneValue(cell)));
           }
           if (x == 3){
               row3.put(Bytes.toString(CellUtil.cloneFamily(cell)) + "_" + Bytes.toString(CellUtil.cloneQualifier(cell)),Bytes.toString(CellUtil.cloneValue(cell)));
           }
        }
    }
    rows.add(row1);rows.add(row2);rows.add(row3);
    List<String> headers = rows.stream().flatMap(row -> row.keySet().stream())
            .collect(Collectors.toSet())
            .stream().sorted().collect(Collectors.toList());
    columnValue.add(headers);
    for (Map<String,String> row : rows){
        List<String> value = new ArrayList<>();
        for (String column : headers){
            value.add(row.get(column));
        }
        columnValue.add(value);
    }
    return columnValue;
}

猜你喜欢

转载自blog.csdn.net/love_zy0216/article/details/86037645