java實現Hbase中的查詢(一)Filter方式

來源:互聯網
上載者:User

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、代碼:

package org.myhbase; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.io.Cell; import org.apache.hadoop.hbase.util.Bytes; public class HBaseBasic03 { private static HBaseConfiguration hbaseConfig=null; static{ Configuration config=new Configuration(); config.set("hbase.zookeeper.quorum","192.168.10.149,192.168.10.44,192.168.10.49"); config.set("hbase.zookeeper.property.clientPort", "2181"); hbaseConfig=new HBaseConfiguration(config); } /** * get方式,通過rowKey查詢 * @param tablename * @param rowKey * @throws IOException */ public static void selectByRowKey(String tablename,String rowKey) throws IOException{ HTable table=new HTable(hbaseConfig,tablename); Get g = new Get(Bytes.toBytes(rowKey)); Result r=table.get(g); for(KeyValue kv:r.raw()){ System.out.println("column: "+new String(kv.getColumn())); System.out.println("value: "+new String(kv.getValue())); } } /** * get方式,通過rowKey、column查詢 * @param tablename * @param rowKey * @param column * @throws IOException */ public static void selectByRowKeyColumn(String tablename,String rowKey,String column) throws IOException{ HTable table=new HTable(hbaseConfig,tablename); Get g = new Get(Bytes.toBytes(rowKey)); g.addColumn(Bytes.toBytes(column)); Result r=table.get(g); for(KeyValue kv:r.raw()){ System.out.println("column: "+new String(kv.getColumn())); System.out.println("value: "+new String(kv.getValue())); } } public static void selectByFilter(String tablename,List<String> arr) throws IOException{ HTable table=new HTable(hbaseConfig,tablename); FilterList filterList = new FilterList(); Scan s1 = new Scan(); for(String v:arr){ // 各個條件之間是“與”的關係 String [] s=v.split(","); filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(s[0]), Bytes.toBytes(s[1]), CompareOp.EQUAL,Bytes.toBytes(s[2]) ) ); // 添加下面這一行後,則只返回指定的cell,同一行中的其他cell不返回 // s1.addColumn(Bytes.toBytes(s[0]), Bytes.toBytes(s[1])); } s1.setFilter(filterList); ResultScanner ResultScannerFilterList = table.getScanner(s1); for(Result rr=ResultScannerFilterList.next();rr!=null;rr=ResultScannerFilterList.next()){ for(KeyValue kv:rr.list()){ System.out.println("row : "+new String(kv.getRow())); System.out.println("column : "+new String(kv.getColumn())); System.out.println("value : "+new String(kv.getValue())); } } } public static void main(String [] args) throws IOException{ // 按rowkey查詢,查詢Tom行的所有cell HBaseBasic03.selectByRowKey("scores","Tom"); // 按rokey 和 column 來查詢,查詢Tom行course列族的所有列值 HBaseBasic03.selectByRowKeyColumn("scores","Tom","course"); // Filter多條件查詢,條件:查詢 course列族中art列值為97 ,且 course列族中math列值為100的行 List<String> arr=new ArrayList<String>(); arr.add("course,art,97"); arr.add("course,math,100"); HBaseBasic03.selectByFilter("scores",arr); } }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.