[hbase]使用HBase的Java api查询HBase

查询操作,不管是关系型数据库,还是非关系型数据库,相对于增删改,都占了很大的比重。

查询不易,且行且珍惜。

使用HBase的java api来进行查询,主要有两种方式,一种是直接通过rowkey(类似于主键)来查询;另一种是scan,扫描表(扫描某一列族,扫描某一colume,以rowkey的开始和结束做为一个范围进行扫描)
1、通过rowkey来查询一条记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//根据rowkey查询一条记录
public void queryByRowKey(Configuration conf,String tableName,String rowKey){
     HTable hTable = null;
     try {
         hTable =  new HTable(conf, tableName);
         Get get =  new Get( rowKey.getBytes());
         Result result = hTable.get(get);
         System.err.println( "rowkey为:" + rowKey);
         HashMap<String, String> hm =  new HashMap<String, String>();
         for (KeyValue kv: result.raw()) {
             hm.put( new String(kv.getFamily()) ,  new String(kv.getValue()));
             System.err.println( "rowkey:" new String(kv.getKey()));
             System.err.println( "-------------------------------" );
             System.err.println( "columnFamily:" new String(kv.getFamily()) + "===column:" new String(kv.getQualifier()) +  "===getValue:" new String(kv.getValue()));
         }
         System.err.println(hm.size());
         System.err.println(hm.toString());
     catch (IOException e) {
         e.printStackTrace();
     } finally {
         try {
             hTable.close();
         catch (IOException e) {
             e.printStackTrace();
         }
     }
}

2、扫描整个表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//扫描hbase表,获取所有记录
public void getAllRow(Configuration conf, String tableName){
     HTable hTable = null;
     ResultScanner rs = null;
     try {
         hTable =  new HTable(conf,  "DANIU" );
         rs = hTable.getScanner( new Scan());
         //循环rowkey
         for (Result result : rs) {
             for (KeyValue kv : result.raw()) {
                 System.err.println( "rowkey:" new String(kv.getKey()));
                 System.err.println( "-------------------------------" );
                 System.err.println( "columnFamily:" new String(kv.getFamily()) + "===column:" + new String(kv.getQualifier()) +  "===getValue:" new String(kv.getValue()));
             }
         }
     catch (IOException e) {
         e.printStackTrace();
     finally {
         rs.close();
         try {
             hTable.close();
         catch (IOException e) {
             e.printStackTrace();
         }
     }
}

3、根据一个条件,扫描。使用一个filter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//根据查询多条记录,一个filter的使用
public void filterSingleColumnValueFilter(Configuration conf,String tableName){
     HTable hTable = null;
     ResultScanner rs = null;
     try {
         hTable =  new HTable(conf, tableName);
         Filter filter =  new SingleColumnValueFilter( "columnfamily1" .getBytes(), null, CompareOp.EQUAL,  "value1" .getBytes());
         Scan scan =  new Scan();
         scan.setFilter(filter);
         rs = hTable.getScanner(scan);
         for (Result result : rs) {
             for (KeyValue kv : result.raw()) {
                 System.err.println( "rowkey:" new String(kv.getKey()));
                 System.err.println( "-------------------------------" );
                 System.err.println( "columnFamily:" new String(kv.getFamily()) + "===column:" + new String(kv.getQualifier()) +  "===getValue:" new String(kv.getValue()));
             }
         }
     catch (IOException e) {
         e.printStackTrace();
     } finally {
         rs.close();
         try {
             hTable.close();
         catch (IOException e) {
             e.printStackTrace();
         }
     }
}

4、使用多个条件来查询数据。多个filter的使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//更多条件,查询更多数据,多个filter的使用。
public void filterMore(Configuration conf,String tableName){
     HTable hTable = null;
     ResultScanner rs = null;
     try {
         hTable =  new HTable(conf, tableName);
         List<Filter> filters =  new ArrayList<Filter>();
         
         Filter filter1 =  new SingleColumnValueFilter( "columnfamily1" .getBytes(), null, CompareOp.EQUAL,  "value1" .getBytes());
         filters.add(filter1);
         
         Filter filter2 =  new SingleColumnValueFilter( "columnfamily2" .getBytes(), null, CompareOp.EQUAL,  "value2" .getBytes());
         filters.add(filter2);
         
         //还可以添加更多的filter
         Scan scan =  new Scan();
         rs = hTable.getScanner(scan);
         for (Result result : rs) {
             for (KeyValue kv : result.raw()) {
                 System.err.println( "rowkey:" new String(kv.getKey()));
                 System.err.println( "-------------------------------" );
                 System.err.println( "columnFamily:" new String(kv.getFamily()) + "===column:" + new String(kv.getQualifier()) +  "===getValue:" new String(kv.getValue()));
             }
         }
     catch (IOException e) {
         e.printStackTrace();
     } finally {
         rs.close();
         try {
             hTable.close();
         catch (IOException e) {
             e.printStackTrace();
         }
     }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值