首先上我的输出类:
/** * 功能:电池历史数据数据结构 * Created by liuhuichao on 2016/12/5. */ public class ResBatteryDataHistory implements Serializable { private String batteryNo; private Integer batteryType; private Float voltageDeviation; private Float totalVoltage; private String createTime; private Integer createUser; private Integer source; private Float vol1; private Float vol2; private Float vol3; private Float vol4; private Float vol5; private Float vol6; private Float vol7; private Float vol8; private Float vol9; private Float vol10; private Float vol11; private Float vol12; private Float vol13; private Float vol14; private Float vol15; private Float vol16; private Float vol17; private Float vol18; private Float vol19; private Float temprature1; private Float temprature2; private Float chargeNum; private Float longtitude; private Float latitude; private String remarks; private Float totalCurrent; private Float soc; private String testUserName; private Integer cityId; private Integer vehicleId; private static final long serialVersionUID = 1L; public String getBatteryNo() { return batteryNo; } public void setBatteryNo(String batteryNo) { this.batteryNo = batteryNo; } public Integer getBatteryType() { return batteryType; } public void setBatteryType(Integer batteryType) { this.batteryType = batteryType; } public Float getVoltageDeviation() { return voltageDeviation; } public void setVoltageDeviation(Float voltageDeviation) { this.voltageDeviation = voltageDeviation; } public Float getTotalVoltage() { return totalVoltage; } public void setTotalVoltage(Float totalVoltage) { this.totalVoltage = totalVoltage; } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public Integer getCreateUser() { return createUser; } public void setCreateUser(Integer createUser) { this.createUser = createUser; } public Integer getSource() { return source; } public void setSource(Integer source) { this.source = source; } public Float getVol1() { return vol1; } public void setVol1(Float vol1) { this.vol1 = vol1; } public Float getVol2() { return vol2; } public void setVol2(Float vol2) { this.vol2 = vol2; } public Float getVol3() { return vol3; } public void setVol3(Float vol3) { this.vol3 = vol3; } public Float getVol4() { return vol4; } public void setVol4(Float vol4) { this.vol4 = vol4; } public Float getVol5() { return vol5; } public void setVol5(Float vol5) { this.vol5 = vol5; } public Float getVol6() { return vol6; } public void setVol6(Float vol6) { this.vol6 = vol6; } public Float getVol7() { return vol7; } public void setVol7(Float vol7) { this.vol7 = vol7; } public Float getVol8() { return vol8; } public void setVol8(Float vol8) { this.vol8 = vol8; } public Float getVol9() { return vol9; } public void setVol9(Float vol9) { this.vol9 = vol9; } public Float getVol10() { return vol10; } public void setVol10(Float vol10) { this.vol10 = vol10; } public Float getVol11() { return vol11; } public void setVol11(Float vol11) { this.vol11 = vol11; } public Float getVol12() { return vol12; } public void setVol12(Float vol12) { this.vol12 = vol12; } public Float getVol13() { return vol13; } public void setVol13(Float vol13) { this.vol13 = vol13; } public Float getVol14() { return vol14; } public void setVol14(Float vol14) { this.vol14 = vol14; } public Float getVol15() { return vol15; } public void setVol15(Float vol15) { this.vol15 = vol15; } public Float getVol16() { return vol16; } public void setVol16(Float vol16) { this.vol16 = vol16; } public Float getVol17() { return vol17; } public void setVol17(Float vol17) { this.vol17 = vol17; } public Float getVol18() { return vol18; } public void setVol18(Float vol18) { this.vol18 = vol18; } public Float getVol19() { return vol19; } public void setVol19(Float vol19) { this.vol19 = vol19; } public Float getTemprature1() { return temprature1; } public void setTemprature1(Float temprature1) { this.temprature1 = temprature1; } public Float getTemprature2() { return temprature2; } public void setTemprature2(Float temprature2) { this.temprature2 = temprature2; } public Float getChargeNum() { return chargeNum; } public void setChargeNum(Float chargeNum) { this.chargeNum = chargeNum; } public Float getLongtitude() { return longtitude; } public void setLongtitude(Float longtitude) { this.longtitude = longtitude; } public Float getLatitude() { return latitude; } public void setLatitude(Float latitude) { this.latitude = latitude; } public String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } public Float getTotalCurrent() { return totalCurrent; } public void setTotalCurrent(Float totalCurrent) { this.totalCurrent = totalCurrent; } public Float getSoc() { return soc; } public void setSoc(Float soc) { this.soc = soc; } public String getTestUserName() { return testUserName; } public void setTestUserName(String testUserName) { this.testUserName = testUserName; } public Integer getCityId() { return cityId; } public void setCityId(Integer cityId) { this.cityId = cityId; } public Integer getVehicleId() { return vehicleId; } public void setVehicleId(Integer vehicleId) { this.vehicleId = vehicleId; } @Override public String toString() { return "ResBatteryDataHistory{" + "batteryNo:'" + batteryNo + '\'' + ", batteryType:" + batteryType + ", voltageDeviation:" + voltageDeviation + ", totalVoltage:" + totalVoltage + ", createTime:'" + createTime + '\'' + ", createUser:" + createUser + ", source:" + source + ", vol1:" + vol1 + ", vol2:" + vol2 + ", vol3:" + vol3 + ", vol4:" + vol4 + ", vol5:" + vol5 + ", vol6:" + vol6 + ", vol7:" + vol7 + ", vol8:" + vol8 + ", vol9:" + vol9 + ", vol10:" + vol10 + ", vol11:" + vol11 + ", vol12:" + vol12 + ", vol13:" + vol13 + ", vol14:" + vol14 + ", vol15:" + vol15 + ", vol16:" + vol16 + ", vol17:" + vol17 + ", vol18:" + vol18 + ", vol19:" + vol19 + ", temprature1:" + temprature1 + ", temprature2:" + temprature2 + ", chargeNum:" + chargeNum + ", longtitude:" + longtitude + ", latitude:" + latitude + ", remarks:'" + remarks + '\'' + ", totalCurrent:" + totalCurrent + ", soc:" + soc + ", testUserName:'" + testUserName + '\'' + ", cityId:" + cityId + ", vehicleId:" + vehicleId + '}'; } }
封装查询条件类:
/** * * HBase条件查询封装实体类 * Created by liuhuichao on 2016/12/13. */ public class QueryCondition { String family; //列族 String qualifier; //列修饰符 CompareFilter.CompareOp compareOp; //操作符 byte[] value; //列值 FilterList.Operator operator; //连接操作符 public QueryCondition(String family, String qualifier, CompareFilter.CompareOp compareOp, byte[] value) { this.family = family; this.qualifier = qualifier; this.compareOp = compareOp; this.value = value; } public QueryCondition(String family, String qualifier, CompareFilter.CompareOp compareOp, byte[] value, FilterList.Operator operator) { this.family = family; this.qualifier = qualifier; this.compareOp = compareOp; this.value = value; this.operator = operator; } public String getFamily() { return family; } public void setFamily(String family) { this.family = family; } public String getQualifier() { return qualifier; } public void setQualifier(String qualifier) { this.qualifier = qualifier; } public CompareFilter.CompareOp getCompareOp() { return compareOp; } public void setCompareOp(CompareFilter.CompareOp compareOp) { this.compareOp = compareOp; } public byte[] getValue() { return value; } public void setValue(byte[] value) { this.value = value; } public FilterList.Operator getOperator() { return operator; } public void setOperator(FilterList.Operator operator) { this.operator = operator; } }
查询操作类:
/** * 功能:管理电池历史数据 * Created by liuhuichao on 2016/12/8. */ public class HBaseBatteryDataManager extends Thread { private static class HBaseBatteryDataMasterHolder{ private static final HBaseBatteryDataManager INSTANCE = new HBaseBatteryDataManager(); } public final String colunm_family_baseData="baseData";//列族baseData public final String colunm_family_extraData="extraData";//列族extraData /** * 构造:配置初始化 */ private HBaseBatteryDataManager() { //集群环境下使用的配置代码 /* config= HBaseConfiguration.create(); config.set("hbase.master",""); //HMaster地址 config.set("hbase.zookeeper.property.clientPort",""); //Zookeeper端口设置 config.set("hbase.zookeeper.quorum","");//Zookeeper队列名称 try{ table=new HTable(config, Bytes.toBytes("demo_table_name")); //连接表 admin=new HBaseAdmin(config); }catch (IOException e){ e.printStackTrace(); }*/ //单机环境下的config config= HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum","lhc-centos"); try{ table=new HTable(config, Bytes.toBytes("batteryTest")); //连接表 }catch (IOException e){ e.printStackTrace(); } } /** * 外部调用这个类的方法 * @return */ public static final HBaseBatteryDataManager getInstance() { return HBaseBatteryDataMasterHolder.INSTANCE; } private Configuration config; public HTable table; public HBaseAdmin admin; /** * 增加一条电池历史数据 * @param resBatteryDataHistory */ public void addBatteryData(ResBatteryDataHistory resBatteryDataHistory) throws IOException { if(resBatteryDataHistory==null){ return; } table.put(getSavePut(resBatteryDataHistory)); table.close(); } /** * 批量添加电池数据 * @param batteryList */ public void addBatteryDataList(List<ResBatteryDataHistory> batteryList) throws IOException { List<Put> puts = new ArrayList<Put>(); for(ResBatteryDataHistory battery : batteryList){ puts.add(getSavePut(battery)); } table.put(puts); table.close(); } /** * 根据rowKey获取电池历史数据 * @param rowKey * @return */ public ResBatteryDataHistory getBatteryDataHistoryByRowKey(String rowKey) throws IOException { Get get=new Get(Bytes.toBytes(rowKey)); Result result=table.get(get); if(result==null){ return null; } return convertToData(result); } /** * 获取电池数据某个单元格的数据 * @param rowKey * @param colFamily * @param col * @return */ public byte[] getCellData(String rowKey,String colFamily,String col) throws IOException { Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col)); //获取的result数据是结果集,还需要格式化输出想要的数据才行 Result result = table.get(get); byte[] cellValue= result.getValue(colFamily.getBytes(),col==null?null:col.getBytes()); table.close(); return cellValue; } /** * 更新表中的一个单元格 * @param rowKey * @param familyName * @param columnName * @param value */ public void updateCell(String rowKey,String familyName, String columnName, byte[] value) throws IOException { Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), value); table.put(put); table.close(); } /** * 删除一个单元格 * @param rowKey * @param familyName * @param columnName */ public void deleteCell( String rowKey,String familyName, String columnName) throws IOException { Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); table.delete(delete); table.close(); } /** * 删除一行 * @param rowKey */ public void deleteAllColumns(String rowKey) throws IOException { Delete delete = new Delete(Bytes.toBytes(rowKey)); table.delete(delete); table.close(); } /** * 获取所有数据 * @return */ public List<ResBatteryDataHistory> getResultScans() throws IOException { Scan scan = new Scan(); ResultScanner resultScanner = null; List<ResBatteryDataHistory> list=new ArrayList<>() ; long beginTime=System.currentTimeMillis(); resultScanner=table.getScanner(scan); long endTime=System.currentTimeMillis(); double spendTime=(endTime-beginTime)/1000.0; System.out.println("扫描表共计时间:------"+spendTime); if(resultScanner==null){ return null; } for (Result result : resultScanner){ list.add(convertToData(result)); } table.close(); return list; } /** * * 根据单个列查询数据 【and查询】 * 例如,查询extraData:create_user==‘1’的所有数据 * @param queryConditionList 条件拼接列表 * @return * @throws IOException */ public List<ResBatteryDataHistory> QueryDataByConditionsAnd(List<QueryCondition> queryConditionList) throws IOException { if(queryConditionList==null || queryConditionList.size()<1){ return null; } ResultScanner rs = null; List<ResBatteryDataHistory> list=new ArrayList<>() ; List<Filter> filters = new ArrayList<Filter>(); for(QueryCondition query : queryConditionList){ SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes(query.getFamily()), Bytes.toBytes(query.getQualifier()), query.getCompareOp(),query.getValue()); filter.setFilterIfMissing(true); //设置这些列不存在的数据不返回 filters.add(filter); } FilterList filterList = new FilterList(filters); Scan scan = new Scan(); scan.setFilter(filterList); rs = table.getScanner(scan); for (Result result : rs){ list.add(convertToData(result)); } table.close(); return list; } /** * 查询数据 【Or查询】 * @param queryConditionList * @return */ public List<ResBatteryDataHistory> QueryDataByConditionsOr(List<QueryCondition> queryConditionList) throws IOException { if(queryConditionList==null || queryConditionList.size()<1){ return null; } List<ResBatteryDataHistory> list=new ArrayList<>() ; ResultScanner rs = null; List<Filter> filters = new ArrayList<Filter>(); Scan scan = new Scan(); for(QueryCondition query : queryConditionList){ SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes(query.getFamily()), Bytes.toBytes(query.getQualifier()), query.getCompareOp(),query.getValue()); filter.setFilterIfMissing(true); filters.add(filter); } FilterList filterList = new FilterList( FilterList.Operator.MUST_PASS_ONE, filters); scan.setFilter(filterList); rs = table.getScanner(scan); for (Result result : rs){ list.add(convertToData(result)); } table.close(); return list; } /** * 混合条件查询 * @param queryConditionList * @return * @throws IOException */ public List<ResBatteryDataHistory> QueryDataByConditions(List<QueryCondition> queryConditionList) throws IOException { if(queryConditionList==null || queryConditionList.size()<1){ return null; } List<ResBatteryDataHistory> list=new ArrayList<>() ; Scan scan = new Scan(); FilterList filterList = null; FilterList.Operator operator = null; List<Filter> filters = new ArrayList<Filter>(); ResultScanner rs = null; for(QueryCondition query : queryConditionList){ SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes(query.getFamily()), Bytes.toBytes(query.getQualifier()), query.getCompareOp(),query.getValue()); filter.setFilterIfMissing(true); //去掉没有这种列的数据 if(query.getOperator()!=null){//有操作符的时候 if (operator == null) { operator = query.getOperator(); filterList = new FilterList( query.getOperator()); filterList.addFilter(filter); System.out.println("filterList==1" + filterList); } else if (operator.equals(query.getOperator())) { filterList.addFilter(filter); } else { filterList.addFilter(filter); System.out.println("filterList==2" + filterList); FilterList addFilterList = new FilterList( query.getOperator()); addFilterList.addFilter(filterList); System.out.println("addFilterList==1" + addFilterList); filterList = addFilterList; System.out.println("filterList==3" + filterList); } } else { if (filterList == null) { filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);// 默认只有一个条件的时候 } filterList.addFilter(filter); } } scan.setFilter(filterList); rs = table.getScanner(scan); for (Result result : rs){ list.add(convertToData(result)); } table.close(); return list; } /** *混合条件分页查询 * @param queryConditionList * @param pageSize * @param lastRow * @return */ public List<ResBatteryDataHistory> QueryDataByConditionsAndPage(List<QueryCondition> queryConditionList, int pageSize,byte[] lastRow) throws IOException { List<ResBatteryDataHistory> list=new ArrayList<>() ; final byte[] POSTFIX = new byte[] { 0x00 }; ResultScanner rs = null; Scan scan = new Scan(); FilterList filterList = null; FilterList.Operator operator = null; //拼接查询条件 for(QueryCondition query : queryConditionList){ SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes(query.getFamily()), Bytes.toBytes(query.getQualifier()), query.getCompareOp(),query.getValue()); filter.setFilterIfMissing(true); //去掉没有这种列的数据 if(query.getOperator()!=null){//有操作符的时候 if (operator == null) { operator = query.getOperator(); filterList = new FilterList( query.getOperator()); filterList.addFilter(filter); System.out.println("filterList==1" + filterList); } else if (operator.equals(query.getOperator())) { filterList.addFilter(filter); } else { filterList.addFilter(filter); System.out.println("filterList==2" + filterList); FilterList addFilterList = new FilterList( query.getOperator()); addFilterList.addFilter(filterList); System.out.println("addFilterList==1" + addFilterList); filterList = addFilterList; System.out.println("filterList==3" + filterList); } } else { if (filterList == null) { filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);// 默认只有一个条件的时候 } filterList.addFilter(filter); } } FilterList pageFilterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);// 默认只有一个条件的时候 Filter pageFilter = new PageFilter(pageSize); pageFilterList.addFilter(pageFilter); pageFilterList.addFilter(filterList); if (lastRow != null) { // 注意这里添加了POSTFIX操作,不然死循环了 byte[] startRow = Bytes.add(lastRow, POSTFIX); scan.setStartRow(startRow); } System.out.println(pageFilterList + ":pageFilterList"); scan.setFilter(pageFilterList); rs = table.getScanner(scan); for (Result result : rs){ list.add(convertToData(result)); } table.close(); return list; } /** * 分页查询——无条件 * @param pageSize * @param lastRow * @return * @throws IOException */ public List<ResBatteryDataHistory> QueryDataByPage( int pageSize,byte[] lastRow) throws IOException { List<ResBatteryDataHistory> list=new ArrayList<>() ; final byte[] POSTFIX = new byte[] { 0x00 }; Scan scan = new Scan(); ResultScanner resultScanner = null; Table table = null; Filter filter = new PageFilter(pageSize); scan.setFilter(filter); if (lastRow != null) { // 注意这里添加了POSTFIX操作,不然死循环了 byte[] startRow = Bytes.add(lastRow, POSTFIX); scan.setStartRow(startRow); } resultScanner = table.getScanner(scan); for (Result result : resultScanner){ list.add(convertToData(result)); } table.close(); return list; } /** * 查询总行数 * @param pageSize * @return */ public int QueryDataByPage(int pageSize) throws IOException { final byte[] POSTFIX = new byte[] { 0x00 }; int totalRows = 0; Filter filter = new PageFilter(pageSize); byte[] lastRow = null; while (true) { Scan scan = new Scan(); scan.setFilter(filter); if (lastRow != null) { // 注意这里添加了POSTFIX操作,不然死循环了 byte[] startRow = Bytes.add(lastRow, POSTFIX); scan.setStartRow(startRow); } ResultScanner scanner = table.getScanner(scan); int localRows = 0; Result result; while ((result = scanner.next()) != null) { System.out.println(localRows++ + ":" + result); totalRows++; lastRow = result.getRow(); } scanner.close(); if (localRows == 0) break; } return totalRows; } /** * 将Result转换成电池历史数据对象 * @param result * @return */ private ResBatteryDataHistory convertToData(Result result){ ResBatteryDataHistory resBatteryDataHistory=new ResBatteryDataHistory(); if(result==null || result.list().size()<1){ return null; } for(KeyValue kv :result.raw()){ if(Bytes.toString(kv.getFamily()).equals(colunm_family_baseData)){ switch (Bytes.toString(kv.getQualifier())){ case "battery_no": resBatteryDataHistory.setBatteryNo(Bytes.toString(kv.getValue())); break; case "battery_type": resBatteryDataHistory.setBatteryType(Bytes.toInt(kv.getValue())); break; case "voltage_deviation": resBatteryDataHistory.setVoltageDeviation(Bytes.toFloat(kv.getValue())); break; case "total_voltage": resBatteryDataHistory.setTotalVoltage(Bytes.toFloat(kv.getValue())); break; case "total_current": resBatteryDataHistory.setTotalCurrent(Bytes.toFloat(kv.getValue())); break; case "vol1": resBatteryDataHistory.setVol1(Bytes.toFloat(kv.getValue())); break; case "vol2": resBatteryDataHistory.setVol2(Bytes.toFloat(kv.getValue())); break; case "vol3": resBatteryDataHistory.setVol3(Bytes.toFloat(kv.getValue())); break; case "vol4": resBatteryDataHistory.setVol4(Bytes.toFloat(kv.getValue())); break; case "vol5": resBatteryDataHistory.setVol5(Bytes.toFloat(kv.getValue())); break; case "vol6": resBatteryDataHistory.setVol6(Bytes.toFloat(kv.getValue())); break; case "vol7": resBatteryDataHistory.setVol7(Bytes.toFloat(kv.getValue())); break; case "vol8": resBatteryDataHistory.setVol8(Bytes.toFloat(kv.getValue())); break; case "vol9": resBatteryDataHistory.setVol9(Bytes.toFloat(kv.getValue())); break; case "vol10": resBatteryDataHistory.setVol10(Bytes.toFloat(kv.getValue())); break; case "vol11": resBatteryDataHistory.setVol11(Bytes.toFloat(kv.getValue())); break; case "vol12": resBatteryDataHistory.setVol12(Bytes.toFloat(kv.getValue())); break; case "vol13": resBatteryDataHistory.setVol13(Bytes.toFloat(kv.getValue())); break; case "vol14": resBatteryDataHistory.setVol14(Bytes.toFloat(kv.getValue())); break; case "vol15": resBatteryDataHistory.setVol15(Bytes.toFloat(kv.getValue())); break; case "vol16": resBatteryDataHistory.setVol16(Bytes.toFloat(kv.getValue())); break; case "vol17": resBatteryDataHistory.setVol17(Bytes.toFloat(kv.getValue())); break; case "vol18": resBatteryDataHistory.setVol18(Bytes.toFloat(kv.getValue())); break; case "vol19": resBatteryDataHistory.setVol19(Bytes.toFloat(kv.getValue())); break; case "temprature1": resBatteryDataHistory.setTemprature1(Bytes.toFloat(kv.getValue())); break; case "temprature2": resBatteryDataHistory.setTemprature2(Bytes.toFloat(kv.getValue())); break; case "charge_num": resBatteryDataHistory.setChargeNum(Bytes.toFloat(kv.getValue())); break; case "soc": resBatteryDataHistory.setSoc(Bytes.toFloat(kv.getValue())); break; case "create_time": resBatteryDataHistory.setCreateTime(Bytes.toString(kv.getValue())); break; case "city_id": resBatteryDataHistory.setCityId(Bytes.toInt(kv.getValue())); break; case "vehicle_id": resBatteryDataHistory.setVehicleId(Bytes.toInt(kv.getValue())); break; } } if(Bytes.toString(kv.getFamily()).equals(colunm_family_extraData)){ switch (Bytes.toString(kv.getQualifier())){ case "create_user" : resBatteryDataHistory.setCreateUser(Bytes.toInt(kv.getValue())); break; case "source" : resBatteryDataHistory.setSource(Bytes.toInt(kv.getValue())); break; case "longtitude" : resBatteryDataHistory.setLongtitude(Bytes.toFloat(kv.getValue())); break; case "latitude" : resBatteryDataHistory.setLatitude(Bytes.toFloat(kv.getValue())); break; case "remarks" : resBatteryDataHistory.setRemarks(Bytes.toString(kv.getValue())); break; case "test_user_name" : resBatteryDataHistory.setTestUserName(Bytes.toString(kv.getValue())); break; } } } return resBatteryDataHistory; } /** * 获取插入数据的Put对象 * @param resBatteryDataHistory * @return */ private Put getSavePut(ResBatteryDataHistory resBatteryDataHistory){ Put put=new Put(Bytes.toBytes(resBatteryDataHistory.getBatteryNo()+System.currentTimeMillis())); put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("battery_no"),Bytes.toBytes(resBatteryDataHistory.getBatteryNo()));//电池编号 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("battery_type"),Bytes.toBytes(resBatteryDataHistory.getBatteryType()));//电池类型 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("voltage_deviation"),Bytes.toBytes(resBatteryDataHistory.getVoltageDeviation()));//压差 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("total_voltage"),Bytes.toBytes(resBatteryDataHistory.getTotalVoltage()));//总电压 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("total_current"),Bytes.toBytes(resBatteryDataHistory.getTotalCurrent()));//总电流 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol1"),Bytes.toBytes(resBatteryDataHistory.getVol1()));//电芯电压1 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol2"),Bytes.toBytes(resBatteryDataHistory.getVol2()));//电芯电压2 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol3"),Bytes.toBytes(resBatteryDataHistory.getVol3()));//电芯电压3 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol4"),Bytes.toBytes(resBatteryDataHistory.getVol4()));//电芯电压4 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol5"),Bytes.toBytes(resBatteryDataHistory.getVol5()));//电芯电压5 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol6"),Bytes.toBytes(resBatteryDataHistory.getVol6()));//电芯电压6 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol7"),Bytes.toBytes(resBatteryDataHistory.getVol7()));//电芯电压7 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol8"),Bytes.toBytes(resBatteryDataHistory.getVol8()));//电芯电压8 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol9"),Bytes.toBytes(resBatteryDataHistory.getVol9()));//电芯电压9 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol10"),Bytes.toBytes(resBatteryDataHistory.getVol10()));//电芯电压10 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol11"),Bytes.toBytes(resBatteryDataHistory.getVol11()));//电芯电压11 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol12"),Bytes.toBytes(resBatteryDataHistory.getVol12()));//电芯电压12 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol13"),Bytes.toBytes(resBatteryDataHistory.getVol13()));//电芯电压13 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol14"),Bytes.toBytes(resBatteryDataHistory.getVol14()));//电芯电压14 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol15"),Bytes.toBytes(resBatteryDataHistory.getVol15()));//电芯电压15 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol16"),Bytes.toBytes(resBatteryDataHistory.getVol16()));//电芯电压16 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol17"),Bytes.toBytes(resBatteryDataHistory.getVol17()));//电芯电压17 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol18"),Bytes.toBytes(resBatteryDataHistory.getVol18()));//电芯电压18 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vol19"),Bytes.toBytes(resBatteryDataHistory.getVol19()));//电芯电压19 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("temprature1"),Bytes.toBytes(resBatteryDataHistory.getTemprature1()));//温度1 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("temprature2"),Bytes.toBytes(resBatteryDataHistory.getTemprature2()));//温度2 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("charge_num"),Bytes.toBytes(resBatteryDataHistory.getChargeNum()));//充电次数 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("soc"),Bytes.toBytes(resBatteryDataHistory.getSoc()));//总电压 put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("create_time"),Bytes.toBytes(resBatteryDataHistory.getCreateTime()));//创建时间 if(resBatteryDataHistory.getCityId()!=null){ put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("city_id"),Bytes.toBytes(resBatteryDataHistory.getCityId()));//城市ID } if(resBatteryDataHistory.getVehicleId()!=null){ put.addColumn(Bytes.toBytes(colunm_family_baseData),Bytes.toBytes("vehicle_id"),Bytes.toBytes(resBatteryDataHistory.getVehicleId()));//车辆ID } put.addColumn(Bytes.toBytes(colunm_family_extraData),Bytes.toBytes("create_user"),Bytes.toBytes(resBatteryDataHistory.getCreateUser()));//创建人 put.addColumn(Bytes.toBytes(colunm_family_extraData),Bytes.toBytes("source"),Bytes.toBytes(resBatteryDataHistory.getSource()));//数据来源 put.addColumn(Bytes.toBytes(colunm_family_extraData),Bytes.toBytes("longtitude"),Bytes.toBytes(resBatteryDataHistory.getLongtitude()));//精度 put.addColumn(Bytes.toBytes(colunm_family_extraData),Bytes.toBytes("latitude"),Bytes.toBytes(resBatteryDataHistory.getLatitude()));//纬度 put.addColumn(Bytes.toBytes(colunm_family_extraData),Bytes.toBytes("remarks"),Bytes.toBytes(resBatteryDataHistory.getRemarks()));//备注 put.addColumn(Bytes.toBytes(colunm_family_extraData),Bytes.toBytes("test_user_name"),Bytes.toBytes(resBatteryDataHistory.getTestUserName()));//测试用户 return put; } }
单元测试代码:
/** * 功能:测试HBase的工具类 * Created by liuhuichao on 2016/12/14. */ public class HBaseBatteryManagerTest { HBaseBatteryDataManager manager= HBaseBatteryDataManager.getInstance(); /** * 测试Hbase的单条Put操作 */ @Test public void testPut() throws IOException { ResBatteryDataHistory battery=new ResBatteryDataHistory(); battery.setBatteryNo("4015106002"); battery.setBatteryType(0); battery.setVoltageDeviation(1F); battery.setTotalVoltage(1F); battery.setCreateTime("2016-12-12 11:11:11"); battery.setCreateUser(250); battery.setSource(3); battery.setVol1(1F); battery.setVol2(1F); battery.setVol3(1F); battery.setVol4(1F); battery.setVol5(1F); battery.setVol6(1F); battery.setVol7(1F); battery.setVol8(1F); battery.setVol9(1F); battery.setVol10(1F); battery.setVol11(1F); battery.setVol12(1F); battery.setVol13(1F); battery.setVol14(1F); battery.setVol15(1F); battery.setVol16(1F); battery.setVol17(1F); battery.setVol18(1F); battery.setVol19(1F); battery.setTemprature1(1F); battery.setTemprature2(1F); battery.setChargeNum(11F); battery.setLongtitude(12F); battery.setLatitude(12F); battery.setRemarks("lhc-代码测试"); battery.setTotalCurrent(22F); battery.setSoc(22F); battery.setTestUserName("lhc"); battery.setCityId(11011); battery.setVehicleId(11011); manager.addBatteryData(battery); } @Test public void addBatteryDataListTest() throws IOException { List<ResBatteryDataHistory> batteryList=new ArrayList<>(); for(int i=20000;i<200000;i++){ ResBatteryDataHistory battery=new ResBatteryDataHistory(); battery.setBatteryNo(String.valueOf(i)); battery.setBatteryType(0); battery.setVoltageDeviation(1F); battery.setTotalVoltage(1F); battery.setCreateTime("2016-12-12 11:11:11"); battery.setCreateUser(250); battery.setSource(3); battery.setVol1(1F); battery.setVol2(1F); battery.setVol3(1F); battery.setVol4(1F); battery.setVol5(1F); battery.setVol6(1F); battery.setVol7(1F); battery.setVol8(1F); battery.setVol9(1F); battery.setVol10(1F); battery.setVol11(1F); battery.setVol12(1F); battery.setVol13(1F); battery.setVol14(1F); battery.setVol15(1F); battery.setVol16(1F); battery.setVol17(1F); battery.setVol18(1F); battery.setVol19(1F); battery.setTemprature1(1F); battery.setTemprature2(1F); battery.setChargeNum(11F); battery.setLongtitude(12F); battery.setLatitude(12F); battery.setRemarks("lhc-代码测试"); battery.setTotalCurrent(22F); battery.setSoc(22F); battery.setTestUserName("lhc"); battery.setCityId(11011); battery.setVehicleId(11011); batteryList.add(battery); } long begin=System.currentTimeMillis(); System.out.println("开始存入:"+begin); manager.addBatteryDataList(batteryList); long end=System.currentTimeMillis(); System.out.println("结束存入:"+end); System.out.println((end-begin)/1000.0); } /** * 测试根据RowKey获取数据 * @throws IOException */ @Test public void testGetByRowKey() throws IOException { System.out.println(manager.getBatteryDataHistoryByRowKey("40151060021481685540182")); } /** * 测试获取单元格数据 */ @Test public void getCellDataTest() throws IOException { System.out.println(Bytes.toFloat(manager.getCellData("40151060021481685540182",manager.colunm_family_baseData,"vol1"))); } /** * 更新用户名 * @throws IOException */ @Test public void updateCellTest() throws IOException { System.out.println(manager.getBatteryDataHistoryByRowKey("40151060021481685540182")); manager.updateCell("40151060021481685540182",manager.colunm_family_extraData,"test_user_name",Bytes.toBytes("liuhuichao128")); System.out.println(manager.getBatteryDataHistoryByRowKey("40151060021481685540182")); } /** * 测试表扫描 * @throws IOException */ @Test public void scanTest() throws IOException { System.out.println( manager.getResultScans().size()); } /** * 测试and查询 */ @Test public void QueryDataByConditionsAnd() throws IOException { List<QueryCondition> queryConditionList=new ArrayList<>(); queryConditionList.add(new QueryCondition(manager.colunm_family_baseData,"vol1", CompareFilter.CompareOp.EQUAL,Bytes.toBytes(1F))); long begin=System.currentTimeMillis(); System.out.println(manager.QueryDataByConditionsAnd(queryConditionList).size()); long end=System.currentTimeMillis(); System.out.println((end-begin)/1000.00); } /** * 测试or查询 * @throws IOException */ @Test public void QueryDataByConditionsOrTest() throws IOException { List<QueryCondition> queryConditionList=new ArrayList<>(); queryConditionList.add(new QueryCondition(manager.colunm_family_baseData,"vol1", CompareFilter.CompareOp.EQUAL,Bytes.toBytes(1F))); System.out.println(manager.QueryDataByConditionsOr(queryConditionList)); } }
自行体会吧。
时间: 2024-09-27 01:34:39