HBaseConvetorUtil 实体转换工具

本文介绍了一个用于HBase的数据转换工具类HBaseConvetorUtil,该工具类提供了将HBase查询结果转换为Java对象的功能,并实现了从Java对象到HBase Put和Delete操作的转换。文中详细解释了如何使用此类进行数据读写。
HBaseConvetorUtil 实体转换工具类
public class HBaseConvetorUtil {
       /**
   * @Title: convetor
   * @Description传入hbase返回结果值。返回实例集合
   * @param
   * @return
   * @throws
    */
   public static <T> List<T>convetor(Class<T> cla,ResultScanner resultScanner) throws Exception{
      List<T> list = new ArrayList<T>();
      for (Result result :resultScanner) {
         Field []fileds=cla.getDeclaredFields();
         T t = cla.newInstance();
         for(Field field:fileds){
field.setAccessible(true);
            String fileName=field.getName();
            if(result.containsColumn(Bytes.toBytes("BASEINFO"), Bytes.toBytes(fileName.toUpperCase()))){
               if(result.getValue(Bytes.toBytes("BASEINFO"), Bytes.toBytes(fileName.toUpperCase())).length==0){
                  continue;
               }
               String value=Bytes.toString(result.getValue(Bytes.toBytes("BASEINFO"), Bytes.toBytes(fileName.toUpperCase())));
               field.set(t, value);
            }
         }
         list.add(t);
      }
      return list;
   }
   
   /**
   * @Title: convetor
   * @Description传入hbase返回结果值,返回实例
   * @param
   * @return
   * @throws
    */
   public static <T> Tconvetor(Class<T> cla,Result result) throws Exception{
         Field []fileds=cla.getDeclaredFields();
         T t = cla.newInstance();
         for(Field field:fileds){
field.setAccessible(true);
            String fileName=field.getName();
            if(result.containsColumn(Bytes.toBytes("BASEINFO"), Bytes.toBytes(fileName.toUpperCase()))){
               if(result.getValue(Bytes.toBytes("BASEINFO"), Bytes.toBytes(fileName.toUpperCase())).length==0){
                  continue;
               }
               String value=Bytes.toString(result.getValue(Bytes.toBytes("BASEINFO"), Bytes.toBytes(fileName.toUpperCase())));
               field.set(t, value);
            }
         }
      return t;
   }
   
   /**
   * @Title: convetor
   * @Description传入保存实例和主键ID,返回PutDelete
   * @param
   * @return
   * @throws
    */
   public static <T> PutDeleteconvetor(T t,String id) throws Exception {
      Put put=new Put(Bytes.toBytes(id));
      Delete delete=new Delete(Bytes.toBytes(id));
      Field [] fileds=t.getClass().getDeclaredFields();
      for(Field field:fileds){
          field.setAccessible(true);
         StringfieldName=field.getName();
         Object value =field.get(t);
         if(null==value){
            delete.deleteColumn(Bytes.toBytes("BASEINFO"), Bytes.toBytes(fieldName.toUpperCase()));
            continue;
         }
         put.add(Bytes.toBytes("BASEINFO"), Bytes.toBytes(fieldName.toUpperCase()), Bytes.toBytes((String)value));
      }
      PutDelete putdelete = new PutDelete();
      putdelete.setPut(put);
      putdelete.setDelete(delete);
      return putdelete;
   }
      
}

很多其它精彩内请注意内容:http://bbs.superwu.cn
遵循超人学院微通道的二维码:

转载于:https://www.cnblogs.com/zfyouxi/p/4580446.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值