java 连接 HBase 获取一行记录后返回为map,有些map键名和对象的属性名对应不上导致转化对象复杂。
我通过开发经验,写了一个工具类,将HBase返回的记录先封装成map,其中键名由下划线转化为小驼峰命名方式,然后将对应不上的属性名调换map中的key,最终将map转化为json,再由json字符串转化为对象。
先附上转化map的代码
/**
* 格式化输出、返回
*
* @param result 查询结果
* @param rowKeyName 行键名
* @param cellEndName 列名结尾
*/
public Map<String, String> showCell(Result result, String rowKeyName, String cellEndName) throws Exception {
//存放列名和值
Map<String, String> colMap = new HashMap<>(16);
//获取行结果
Cell[] cells = result.rawCells();
if (cells != null && cells.length > 0) {
for (Cell cell : cells) {
//行键名 (正序客户号)
colMap.put(rowKeyName, StringUtils.reverseStr(Bytes.toString(CellUtil.cloneRow(cell))));
//列名
String cellName = Bytes.toString(CellUtil.cloneQualifier(cell));
//值
String value = Bytes.toString(CellUtil.cloneValue(cell));
//为\N设置空
if (value != null && value.length() > 0 && "\\N".equals(value)) {
value = "";
}
//标志
if (cellEndName != null && cellName.endsWith(cellEndName)) {
//添加列名和值(加上hold前缀)
colMap.put(StringUtils.toCamelCase(HBaseConfig.CELLSTARTNAME + cellName), value);
} else {
//添加列名和值
colMap.put(StringUtils.toCamelCase(cellName), value);
}
}
}
return colMap;
}
其中 cellEndName 参数 可以忽略不传
代码中这个方法是将下划线命名方式转化为小驼峰形式
附上此方法代码StringUtils.toCamelCase
/** 下划线 */
private static final char SEPARATOR = ‘_‘;
/**
* 驼峰式命名法 例如:user_name->userName
*/
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
boolean upperCase = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == SEPARATOR) {
upperCase = true;
} else if (upperCase) {
sb.append(Character.toUpperCase(c));
upperCase = false;
} else {
sb.append(c);
}
}
return sb.toString();
}
然后将对应不上的属性名调换map中的key
附上代码 将原有的map和要转化的对象传入此方法
/**
* 按照实体类中带有Column注解属性的值替换map的key
* Column--name 转化 属性名键
* @param oldMap 原map
* @param clazz 实体类反射对象
* **/
public static Map<String,String> updateMapKey(Map<String,String> oldMap,Class clazz){
//获取类中所有属性
Field[] fields = clazz.getDeclaredFields();
//将数组转化为集合
List<Field> fieldList = Arrays.asList(fields);
//筛选出所有带有Column注解的属性
List<Field> columnFieldList= fieldList.stream().filter(p -> p.getAnnotation(Column.class) != null).collect(Collectors.toList());
if(columnFieldList!=null&&columnFieldList.size()>0){
for(Field field:columnFieldList){
//设置私有访问
field.setAccessible(true);
//获取属性中的注解
Column annotation = field.getAnnotation(Column.class);
if(annotation!=null){
//原map中含有此注解的name属性(原列名)的键
if(oldMap.containsKey(annotation.name())){
//重新赋值属性名键
oldMap.put(field.getName(),oldMap.get(annotation.name()));
//删掉原有未转化的键
oldMap.remove(annotation.name());
}
}
}
}
return oldMap;
}
其中有个注解Column类负责将需要转化的属性名放在需要转化的类的属性上
cloumn注解类代码
package com.pactera.project.custview.interceptor.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* HBase的column的注解
*
* @author xumingjun
*
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
/**
* HBase 列名
*
* @return
*/
String name() default "";
}
标注上注解类似于

将name中的键替换成属性名的键
最后map转化为json,再由json字符串转化为对象。
//将 Map 转换为 实体类
CustVo custVo = JSONObject.parseObject(JSONObject.toJSONString(newColDataMap), CustVo.class);
这样就可以将Hbase返回记录转化为对象了
有兴趣的加我微信
不懂的可以问我

有帮助的赏识1元

原文:https://www.cnblogs.com/xumingjun/p/14453286.html