我是靠谱客的博主 沉静便当,最近开发中收集的这篇文章主要介绍读取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;
}

}

最后

以上就是沉静便当为你收集整理的读取hbase数据的全部内容,希望文章能够帮你解决读取hbase数据所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部