hbase中filter查询例子

http://blog.youkuaiyun.com/karen_wang/archive/2011/03/28/6284154.aspx 

1、需要的jar包:

commons-codec-1.4.jar

commons-logging-1.0.4.jar

hadoop-0.20.2-core.jar

hbase-0.20.6.jar

log4j-1.2.15.jar

zookeeper-3.2.2.jar

2、已有表结构:

1、表名:scores

2、列族:

course:art

course:math

grade:

   

3、scan 'scores'的内容:

ROW                          COLUMN+CELL                                                                      
 Jerry                       column=course:art, timestamp=1301294630194, value=80                             
 Jerry                       column=course:math, timestamp=1301294630132, value=100                           
 Jerry                       column=grade:, timestamp=1301294630073, value=2                                  
 Jim                         column=course:art, timestamp=1301294630363, value=97                             
 Jim                         column=course:math, timestamp=1301294630305, value=100                           
 Jim                         column=grade:, timestamp=1301294630247, value=3                                  
 Tom                         column=course:art, timestamp=1301294630015, value=97                             
 Tom                         column=course:math, timestamp=1301294629987, value=87                            
 Tom                         column=grade:, timestamp=1301294629931, value=1

4、代码:

 


  1. package org.myhbase;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import org.apache.hadoop.conf.Configuration;  
  8. import org.apache.hadoop.hbase.HBaseConfiguration;  
  9. import org.apache.hadoop.hbase.KeyValue;  
  10. import org.apache.hadoop.hbase.client.Get;  
  11. import org.apache.hadoop.hbase.client.HTable;  
  12. import org.apache.hadoop.hbase.client.Result;  
  13. import org.apache.hadoop.hbase.client.ResultScanner;  
  14. import org.apache.hadoop.hbase.client.Scan;  
  15. import org.apache.hadoop.hbase.filter.FilterList;  
  16. import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;  
  17. import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;  
  18. import org.apache.hadoop.hbase.io.Cell;  
  19. import org.apache.hadoop.hbase.util.Bytes;  
  20.   
  21. public class HBaseBasic03  
  22.     private static HBaseConfiguration hbaseConfig=null;  
  23.     static{  
  24.         Configuration config=new Configuration();  
  25.         config.set("hbase.zookeeper.quorum","192.168.10.149,192.168.10.44,192.168.10.49");  
  26.         config.set("hbase.zookeeper.property.clientPort", "2181");  
  27.         hbaseConfig=new HBaseConfiguration(config);  
  28.      
  29.       
  30.       
  31.     public static void selectByRowKey(String tablename,String rowKey) throws IOException{  
  32.         HTable table=new HTable(hbaseConfig,tablename);  
  33.         Get new Get(Bytes.toBytes(rowKey));  
  34.         Result r=table.get(g);  
  35.         for(KeyValue kv:r.raw()){  
  36.             System.out.println("column: "+new String(kv.getColumn()));  
  37.             System.out.println("value: "+new String(kv.getValue()));  
  38.          
  39.      
  40.       
  41.       
  42.     public static void selectByRowKeyColumn(String tablename,String rowKey,String column) throws IOException{  
  43.         HTable table=new HTable(hbaseConfig,tablename);  
  44.         Get new Get(Bytes.toBytes(rowKey));  
  45.         g.addColumn(Bytes.toBytes(column));  
  46.         Result r=table.get(g);  
  47.         for(KeyValue kv:r.raw()){  
  48.             System.out.println("column: "+new String(kv.getColumn()));  
  49.             System.out.println("value: "+new String(kv.getValue()));  
  50.          
  51.      
  52.       
  53.       //通过表中的值来查询
  54.     public static void selectByFilter(String tablename,List<String> arr) throws IOException{  
  55.         HTable table=new HTable(hbaseConfig,tablename);  
  56.         FilterList filterList new FilterList();  
  57.         Scan s1 new Scan();  
  58.         for(String v:arr){ // 各个条件之间是“与”的关系  
  59.             String [] s=v.split(",");  
  60.             filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(s[0]),  
  61.                                                              Bytes.toBytes(s[1]),  
  62.                                                              CompareOp.EQUAL,Bytes.toBytes(s[2])  
  63.                                                               
  64.             );  
  65.             // 添加下面这一行后,则只返回指定的cell,同一行中的其他cell不返回  
  66. //          s1.addColumn(Bytes.toBytes(s[0]), Bytes.toBytes(s[1]));  
  67.          
  68.         s1.setFilter(filterList);  
  69.         ResultScanner ResultScannerFilterList table.getScanner(s1);  
  70.         for(Result rr=ResultScannerFilterList.next();rr!=null;rr=ResultScannerFilterList.next()){  
  71.             for(KeyValue kv:rr.list()){  
  72.                 System.out.println("row "+new String(kv.getRow()));  
  73.                 System.out.println("column "+new String(kv.getColumn()));  
  74.                 System.out.println("value "+new String(kv.getValue()));  
  75.              
  76.          
  77.      
  78.       
  79.     public static void main(String [] args) throws IOException{  
  80.           
  81.         // 按rowkey查询,查询Tom行的所有cell  
  82.         HBaseBasic03.selectByRowKey("scores","Tom");  
  83.           
  84.         // 按rokey 和 column 来查询,查询Tom行course列族的所有列值  
  85.         HBaseBasic03.selectByRowKeyColumn("scores","Tom","course");  
  86.           
  87.         // Filter多条件查询,条件:查询 course列族中art列值为97 ,且 course列族中math列值为100的行  
  88.         List<String> arr=new ArrayList<String>();  
  89.         arr.add("course,art,97");  
  90.         arr.add("course,math,100");  
  91.         HBaseBasic03.selectByFilter("scores",arr);  
  92.           
  93.      
  94.       
  95. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值