生活随笔
收集整理的這篇文章主要介紹了
java实现Hbase中的查询(一)Filter方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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、代碼:
[java:nogutter] view plaincopy
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);????????????????}????????}?
轉載于:https://www.cnblogs.com/end/archive/2012/08/23/2652455.html
總結
以上是生活随笔為你收集整理的java实现Hbase中的查询(一)Filter方式的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。