java统计hbase条数(开始rowkey,结束rowkey,字符反转,模糊匹配)
代码如下,不需要配置文件
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.*; import java.io.IOException; import java.util.ArrayList; import java.util.List; /*todo 查hbase数据条数 */ public class HBaseLocalTest { public static Configuration conf; public static Connection conn; static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.quorum", "*"); try { conn = ConnectionFactory.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } //将传入的日期参数转换成rowkey的反转 public static String Fdate(String s) { char[] array = s.toCharArray(); String reverse = ""; //倒叙拿出 for (int i = array.length - 1; i >= 0; i--) reverse += array[i]; return reverse; } //补齐传入的字符串 public static String Complement(String busNo) {//预先定义一个6位0 StringBuilder zero =new StringBuilder("000000"); String result = zero.substring(0, zero.length() - busNo.length()) + busNo; return result; } /** * 根据startRowKey和endRowKey筛选出区间,然后根据regxKey正则匹配和num查出最终的结果 * @param tableName 表名 * @param startRowKey 开始的范围 * @param endRowKey 结束的范围 * @param regxKey 正则匹配 * @param num 查询的条数 * @return List<Result> */ public static List<Result> getNumRegexRow(String tableName,String startRowKey,String endRowKey, String regxKey,int num) { HTableInterface table = null; List<Result> list = null; try { table = new HTable(conf, tableName); //创建一个过滤器容器,并设置其关系(AND/OR) FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL); //设置正则过滤器 RegexStringComparator rc = new RegexStringComparator(regxKey); RowFilter rf = new RowFilter(CompareFilter.CompareOp.EQUAL, rc); //过滤获取的条数 if (num != 0) { Filter filterNum = new PageFilter(num);//展示条数 fl.addFilter(filterNum); } //过滤器的添加 fl.addFilter(rf); Scan scan = new Scan(); //设置取值范围 scan.setStartRow(startRowKey.getBytes());//开始的key scan.setStopRow(endRowKey.getBytes());//结束的key scan.setFilter(fl);//为查询设置过滤器的list ResultScanner scanner = table.getScanner(scan) ; list = new ArrayList<Result>() ; for (Result rs : scanner) { list.add(rs) ; } } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return list; } public static void main(String[] args) throws Exception { String startDate= "181017"; String endDate ="181017"; String tablename = "Table"; String zao="00"; //这里是根据一天24小时匹配查询 String wan="23"; String wei = "000000001"; String wei2= "999999999"; String startRowkey = Fdate(startDate)+zao+wei; String endRowkey = Fdate(endDate)+wan+wei2; List<Result> list = getNumRegexRow(tablename,startRowkey,endRowkey,"[^\\\\\\/\\^]",0); //后面的0,是查询所有符合的。如果改为5,只会打印符合条件的前5条数据 for (int j = 0; j < list.size(); j++) { // System.out.print("PROID:"); //下面是打印字段的值 // System.out.print(Bytes.toString(list.get(j).getValue(Bytes.toBytes("YB"),Bytes.toBytes("PROID")))); // System.out.print(" ORIDATA:"); // System.out.println(Bytes.toString(list.get(j).getValue(Bytes.toBytes("YB"),Bytes.toBytes("ORIDATA")))); //// System.out.print(" SITE_TIME:"); //// System.out.print(Bytes.toString(list.get(j).getValue(Bytes.toBytes("f"),Bytes.toBytes("SITE_TIME")))); //// System.out.print(" DOWN:"); //// System.out.print(Bytes.toString(list.get(j).getValue(Bytes.toBytes("f"),Bytes.toBytes("IS_UP+DOWN")))); //// System.out.print(" LAT:"); //// System.out.print(Bytes.toString(list.get(j).getValue(Bytes.toBytes("f"),Bytes.toBytes("LAT")))); //// System.out.print(" LNG:"); //// System.out.println(Bytes.toString(list.get(j).getValue(Bytes.toBytes("f"),Bytes.toBytes("LNG")))); } System.out.println("数据量:"+list.size()); conn.close(); } }