java反射的简单使用

前几天项目中遇到一个相对有意思的事情,那就是一个InsertHbase的工具类接收一个对象,然后获取到对象的每一个属性及其值,最后Insert到Hbase中
如:

public class People{

    private String name;
    private int age;

    public People(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

获取到对象的每一个属性及其值,最后Insert到Hbase中

/**
 * Created by shengjk1 on 2016/6/13.
 */
public class ObjInsertHbase {
    private static Logger logger = Logger.getLogger(ObjInsertHbase.class);

    /**
     * @param mapObj <rowkey,Object>
     * @param table_name
     * @throws Exception
     */
    public static void insert(Map<String,Object>mapObj, String table_name) throws Exception {
        if (mapObj.size() < 1 || StringUtils.isBlank(table_name)) {
            throw new RuntimeException("insertHbase 参数不符合要求: mapObj.size() .size():" + mapObj.size()  + "  table_name:" + table_name);
        }

        Table table = null;
        Connection conn = null;
        try {

            long c = System.currentTimeMillis();

            PropertiesConfiguration pro = new PropertiesConfiguration("conf.properties");
            String zkquorum = pro.getString("hbase.zookeeper.quorum");

            Configuration config = HBaseConfiguration.create();


            config.set("hbase.zookeeper.quorum", zkquorum);
            conn = ConnectionFactory.createConnection(config);
            table = conn.getTable(TableName.valueOf(table_name));

            List<Put> putList=new ArrayList();

            if (mapObj.keySet().size()<1 || mapObj.values().size()<1){
                logger.error("insert hbase传入map有误");
                throw new RuntimeException("insert hbase传入map有误");
            }

            for (String key : mapObj.keySet()) {


                Put put = new Put(key.getBytes());
                Object obj=mapObj.get(key);

                if (null==key|| null==obj){
                    throw new RuntimeException("数据错误 rowkey :"+key +" obj: "+obj);
                }

                /**
                 * 获取对象中的每一个字段
                 */
                Field[] fields = obj.getClass().getDeclaredFields();
                for (int i = 0, len = fields.length; i < len; i++) {

                    String varName = fields[i].getName();
                    boolean accessFlag = fields[i].isAccessible();
                    fields[i].setAccessible(true);
                    Object o = fields[i].get(obj) == null ? "" : fields[i].get(obj);
                    fields[i].setAccessible(accessFlag);

                    if (!varName.equalsIgnoreCase("serialVersionUID") && StringUtils.isNotBlank(varName)) {
                        logger.info("varName " + varName + "   o " + o);
                //Insert到hbase  
                    put.addColumn("f".getBytes(), varName.getBytes(), o.toString().getBytes());
                    } else {
                        logger.error("varName 属性名获取失败");
                        throw new RuntimeException("varName 属性名获取失败");
                    }

                    putList.add(put);
                }
            }
            table.put(putList);


            long b = System.currentTimeMillis();

            logger.info("insert hbase ======== " + (b - c) + " 毫秒");
        } finally {
            table.close();
            conn.close();
        }
    }

}

Hbase中结果:

a                              column=f:age, timestamp=1475072798158, value=12                                         
 a                              column=f:name, timestamp=1475072798158, value=ad                                        
 a0                             column=f:error, timestamp=1475080718658, value=aa                                       
 a1                             column=f:age, timestamp=1475079642084, value=12                                         
 a1                             column=f:name, timestamp=1475079642084, value=                                          
 a11                            column=f:age, timestamp=1475080206238, value=12                                         
 a11                            column=f:name, timestamp=1475080206238, value=                                          
 a2                             column=f:age, timestamp=1475079642084, value=121                                        
 a2                             column=f:name, timestamp=1475079642084, value=a 

方便以后建二级索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shengjk1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值