Hbase指定规则扫描表

1.创建一个scan扫描对象

2. scan对象中有setStartRow方法和setStopRow方法,分别指向开始扫描的rowkey和结束扫描的rowkey

3.scan对象中的addColumn方法指向所要查询的列簇中的某个列

要注意点是在指向同一列簇中的列时,要按照字典顺序指定,如果跳着指定则会出现NullPoinException(空指针异常)

 1 public void Scan() throws IOException {
 2         HTableInterface student = connection.getTable("student");
 3 
 4         Scan scan = new Scan();
 5         //指定开始扫描的rowkey和结束扫描的rowkey
 6         scan.setStartRow("1500100018".getBytes());
 7         scan.setStopRow("1500100023".getBytes());
 8         //指定扫描指定列簇和列
 9         scan.addColumn("info".getBytes(),"name".getBytes());
10         scan.addColumn("info".getBytes(),"age".getBytes());
11         scan.addColumn("info".getBytes(),"gender".getBytes());
12         //扫描表
13         ResultScanner scanner = student.getScanner(scan);
14 
15         Result next;
16         while ((next = scanner.next()) != null) {
17             print(next);
18         }
19     }
20 
21     private void print(Result result) {
22         String id = Bytes.toString(result.getRow());
23 
24         String name = Bytes.toString(result.getValue("info".getBytes(),"name".getBytes()));
25         int age = Bytes.toInt(result.getValue("info".getBytes(),"age".getBytes()));
26         String gender = Bytes.toString(result.getValue("info".getBytes(),"gender".getBytes()));
27         String clazz = Bytes.toString(result.getValue("info".getBytes(),"clazz".getBytes()));
28 
29         System.out.println(id + "\t"+name + "\t"+age + "\t"+gender + "\t"+clazz);
30     }

 

posted @ 2020-06-09 17:12  会飞的猪、li  阅读(489)  评论(0编辑  收藏  举报