java反射的简单使用

2018-10-24 15:25:00 浏览数 (1)

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

代码语言:javascript复制
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中

代码语言:javascript复制
/**
 * 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中结果:

代码语言:javascript复制
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 

方便以后建二级索引

0 人点赞