读取hbase数据

@Repository
public class StandardHbaseReaderTemplate {

private static Logger logger = LoggerFactory.getLogger(StandardBatteryTemplate.class);

@Value("${}")
private String tableName;

@Autowired
private HbaseTemplate hbaseTemplate;

public List<Map<String, Map<String, Object>>> query(String tenantId, String date, String family, List<String> columns, String startRowKey, String endRowKey) {
    Scan scan = new Scan();
    scan.setCaching(10000);
    scan.setMaxResultSize(-1);
    scan.setMaxVersions(1);
    scan.setStartRow(Bytes.toBytes(startRowKey));
    scan.setStopRow(Bytes.toBytes(endRowKey));
    for (String column : columns) {
        scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
    }
    String name = ****;
    logger.debug(LogDomain.HBASE, "table: [{}], start row key: [{}], end row key: [{}]",
            name, startRowKey, endRowKey);
    try {
        return hbaseTemplate.find(name, scan, (result, rowNum) -> {
            Map<String, Map<String, Object>> mapMap = new HashMap<>();

            List<Cell> cellList = result.listCells();
            for (Cell cell : cellList) {
                String rowKey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                Class clazz = getClazz(qualifier);
                Object value = getValue(clazz, cell);
            if (!mapMap.containsKey(rowKey)) {
                Map<String, Object> map = new HashMap<>();
                map.put(qualifier, value);
                mapMap.put(rowKey, map);
            } else {
                mapMap.get(rowKey).put(qualifier, value);
            }
        }
        return mapMap;
        });
    } catch (HbaseSystemException e) {
        logger.error(LogDomain.HBASE, "Hbase error", e);
        return Collections.emptyList();
    }
}

private Class getClazz(String qualifier) {
    Class clazz = null;
    if (StandardBatteryQualifier.ofCellName(qualifier) != null) {
        clazz = StandardBatteryQualifier.ofCellName(qualifier).getClazz();
    } else if (StandardBatterySensorDataQualifier.ofCellName(qualifier) != null) {
        clazz = StandardBatterySensorDataQualifier.ofCellName(qualifier).getClazz();
    }
    return clazz;
}

private Object getValue(Class clazz, Cell cell) {
    if (clazz == null) {
        return null;
    } else if (clazz == Byte.class) {
        return cell.getValueArray();
    } else if (clazz == Boolean.class) {
        return Bytes.toBoolean(cell.getValueArray());
    } else if (clazz == Short.class) {
        return Bytes.toShort(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
    } else if (clazz == Integer.class) {
        return Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
    } else if (clazz == Long.class) {
        return Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
    } else if (clazz == Float.class) {
        return Bytes.toFloat(cell.getValueArray(), cell.getValueOffset());
    } else if (clazz == Double.class) {
        return Bytes.toDouble(cell.getValueArray(), cell.getValueOffset());
    } else if (clazz == BigDecimal.class) {
        return Bytes.toBigDecimal(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
    } else if (clazz == String.class) {
        return Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
    } else {
        logger.error(LogDomain.HBASE, "Unknown Class: {}", clazz);
    }
    return null;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值