springboot+hbase整合遇到的坑
前些日子,搞了一波Hbase数据库的整合,个人感觉hbase 还不错,大概的介绍和理解,网上很多,但是对Hbase分页以及使用sql查询的整合还是比较少的,最后我也没搞成分页,因为服务器不让我动,我想上Phoneix插件,项目负责人不给弄啊!~~~
1.报错无权限,在初始化Hbase连接的时候,添加下面的代码,admin 是安装hbase机器的用户名。
org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException: Failed 1 action: org.apache.hadoop.hbase.security.AccessDeniedException: Insufficient permissions for user ‘xxxx'
/* * 在创建、查询、插入的时候会报错,当前用户没有权限 * 添加下面一行代码,把hbase已经存在的用户 存放在代码中 * 调用的时候就不会报错 * */ UserGroupInformation userGroupInformation = UserGroupInformation.createRemoteUser("admin"); connection = ConnectionFactory.createConnection(conf, pool, User.create(userGroupInformation));
2.查询的时候添加条件
/** * 根据某个列查询 * @param tablename 表名 * @param columnFamily 列族 * @param vehicleId 列名 * @param vehplatecolorid 列名 * @param startDate 开始时间 *@param startDate 结束时间 * */ public static String selectByParam(String tablename, String columnFamily, String vehicleId, String param1, String vehplatecolorid,String param2,Long startDate,Long endDate ) throws Exception { //过滤器 Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(vehicleId), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(param1)); Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(vehplatecolorid), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(param2)); /*Filter filter3 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(startDate), CompareFilter.CompareOp.LESS_OR_EQUAL , Bytes.toBytes(startDate)); Filter filter4 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(endDate), CompareFilter.CompareOp. GREATER_OR_EQUAL, Bytes.toBytes(endDate));*/ FilterList filterList = new FilterList(); filterList.addFilter(filter1); filterList.addFilter(filter2); /*filterList.addFilter(filter3); filterList.addFilter(filter4);*/ Table table = connection.getTable(TableName.valueOf(tablename)); Scan s = new Scan(); if(startDate!=null && endDate !=null){ //时间戳筛选 s.setTimeRange(startDate,endDate); } s.setFilter(filterList); ResultScanner rs = table.getScanner(s); List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>(); //此处双for循环 需要优化,但没找到其他 读取数据的方式^-^ 只能双循环读取啊 <'_'> ..... for (Result r : rs) { Map map=new HashMap(); for (KeyValue keyValue : r.raw()) { map.put(new String(keyValue.getQualifier()),new String(keyValue.getValue())); } if(map.get("passId")!=null && map.get("vehicleId")!=null){ resList.add(map); } } return JSON.toString(resList); }