Hbase scan通过rowkey条件查询

Configuration config = new Configuration();
config.set("hbase.zookeeper.quorum", "10.1.1.1");//需根据hbase配置进行设置
HBaseConfiguration hbaseConfig = new HBaseConfiguration(config);

Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(time1+uid));
scan.setStopRow(Bytes.toBytes(time2+uid));
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(".*"+uid));
scan.setFilter(filter);

ResultScanner rs = null;
HTable table = new HTable(hbaseConfig, Bytes.toBytes(tableName));
String rowkey = null;
rs = table.getScanner(scan);
for (Result r : rs) {
    for (KeyValue kv : r.list()) {
        rowkey = Bytes.toString(kv.getRow());
        System.out.println(rowkey);
    }   
}

一般rowkey的名字是uuid+time. 如果只想用uuid模糊查询,可以这样。


public static List> getList(String tableName,String starttime,String stoptime,String uuid,String muuid) throws IOException{
			 HTablePool pool = new HTablePool(configuration, 1000);	 
			 uuid = "285836d1-cc0f-11e7-982d-00163e32a1d5";
		/*	 Filter filter1 = new SingleColumnValueFilter(Bytes
						.toBytes("MpsId"), null, CompareOp.EQUAL, Bytes
						.toBytes(muuid));*/
          //时间段查询
            Scan scan = new Scan();
            scan.addFamily(Bytes.toBytes("automate"));
            scan.setStartRow(Bytes.toBytes(uuid+"_"+starttime));
            scan.setStopRow(Bytes.toBytes(uuid+"_"+stoptime));
         //   scan.setFilter(filter1);
            List> list = new ArrayList<>();
            Map maps = new LinkedHashMap<>();
           
            ResultScanner scanner1 = pool.getTable(tableName).getScanner(scan);
            //ResultScanner scanner1 = table.getScanner(scan);
            for (Result res : scanner1) {
           	 Map map = new LinkedHashMap<>();
           	 System.out.println("==="+res.toString());
           	 for (KeyValue kv : res.raw()) {
           	  //测量值
      		   if(new String(kv.getQualifier()).equals("MeasValue")){
      		   	 System.err.println( new String(kv.getQualifier()) + ":" + (int)(Float.parseFloat(new String(kv.getValue()))*100)/100.0);
      		    	 map.put(new String(kv.getQualifier()),  new String(kv.getValue()));
      		    }
      		    //差值
      		    if(new String(kv.getQualifier()).equals("DiffValue")){
         		   	 System.err.println( new String(kv.getQualifier()) + ":" + (int)(Float.parseFloat(new String(kv.getValue()))*100)/100.0);
         		        map.put(new String(kv.getQualifier()),  new String(kv.getValue()));
      		    }
      		    //累计
      		    if(new String(kv.getQualifier()).equals("AccValue")){
            		   	 System.err.println( new String(kv.getQualifier()) + ":" + (int)(Float.parseFloat(new String(kv.getValue()))*100)/100.0);
            		   map.put(new String(kv.getQualifier()),  new String(kv.getValue()));
      		    }
      		    //时间
      		    if(new String(kv.getQualifier()).equals("MeasTime")){
            		   	 System.err.println( new String(kv.getQualifier()) + ":" + new String(kv.getValue()));
            		   map.put(new String(kv.getQualifier()),  new String(kv.getValue()));
      		    } 
      		  if(new String(kv.getQualifier()).equals("CH1")){
      		   	 System.err.println( new String(kv.getQualifier()) + ":" + new String(kv.getValue()));
 		    } 
      		  if(new String(kv.getQualifier()).equals("CH2")){
     		   	 System.err.println( new String(kv.getQualifier()) + ":" + new String(kv.getValue()));
		    } 
 		    if(new String(kv.getQualifier()).equals("CH3")){
     		   	 System.err.println( new String(kv.getQualifier()) + ":" + new String(kv.getValue()));
		    } 
 		    
 		   if(new String(kv.getQualifier()).equals("CH1")){
    		   	 System.err.println( new String(kv.getQualifier()) + ":" + new String(kv.getValue()));
		    } 
    		  if(new String(kv.getQualifier()).equals("CH2")){
   		   	 System.err.println( new String(kv.getQualifier()) + ":" + new String(kv.getValue()));
		    } 
		    if(new String(kv.getQualifier()).equals("CH3")){
   		   	 System.err.println( new String(kv.getQualifier()) + ":" + new String(kv.getValue()));
		    } 
 		    if(new String(kv.getQualifier()).equals("V0")){
     		   	 System.err.println( new String(kv.getQualifier()) + ":" + new String(kv.getValue()));
		    } 
                }
            }
            scanner1.close();
            list.add(maps);
            return list;
		}
以上是实现通过时间段查询hbase里的数据。

注意:rowkey必须是跟时间信息的。如: new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:00").format(new Date())+name形式。

你可能感兴趣的:(hbase)