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、代码:
- packageorg.myhbase;
- importjava.io.IOException;
- importjava.util.ArrayList;
- importjava.util.List;
- importorg.apache.hadoop.conf.Configuration;
- importorg.apache.hadoop.hbase.HBaseConfiguration;
- importorg.apache.hadoop.hbase.KeyValue;
- importorg.apache.hadoop.hbase.client.Get;
- importorg.apache.hadoop.hbase.client.HTable;
- importorg.apache.hadoop.hbase.client.Result;
- importorg.apache.hadoop.hbase.client.ResultScanner;
- importorg.apache.hadoop.hbase.client.Scan;
- importorg.apache.hadoop.hbase.filter.FilterList;
- importorg.apache.hadoop.hbase.filter.SingleColumnValueFilter;
- importorg.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
- importorg.apache.hadoop.hbase.io.Cell;
- importorg.apache.hadoop.hbase.util.Bytes;
- publicclassHBaseBasic03{
- privatestaticHBaseConfigurationhbaseConfig=null;
- static{
- Configurationconfig=newConfiguration();
- 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=newHBaseConfiguration(config);
- }
- /**
- *get方式,通过rowKey查询
- *@paramtablename
- *@paramrowKey
- *@throwsIOException
- */
- publicstaticvoidselectByRowKey(Stringtablename,StringrowKey)throwsIOException{
- HTabletable=newHTable(hbaseConfig,tablename);
- Getg=newGet(Bytes.toBytes(rowKey));
- Resultr=table.get(g);
- for(KeyValuekv:r.raw()){
- System.out.println("column:"+newString(kv.getColumn()));
- System.out.println("value:"+newString(kv.getValue()));
- }
- }
- /**
- *get方式,通过rowKey、column查询
- *@paramtablename
- *@paramrowKey
- *@paramcolumn
- *@throwsIOException
- */
- publicstaticvoidselectByRowKeyColumn(Stringtablename,StringrowKey,Stringcolumn)throwsIOException{
- HTabletable=newHTable(hbaseConfig,tablename);
- Getg=newGet(Bytes.toBytes(rowKey));
- g.addColumn(Bytes.toBytes(column));
- Resultr=table.get(g);
- for(KeyValuekv:r.raw()){
- System.out.println("column:"+newString(kv.getColumn()));
- System.out.println("value:"+newString(kv.getValue()));
- }
- }
- publicstaticvoidselectByFilter(Stringtablename,List<String>arr)throwsIOException{
- HTabletable=newHTable(hbaseConfig,tablename);
- FilterListfilterList=newFilterList();
- Scans1=newScan();
- for(Stringv:arr){//各个条件之间是“与”的关系
- String[]s=v.split(",");
- filterList.addFilter(newSingleColumnValueFilter(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);
- ResultScannerResultScannerFilterList=table.getScanner(s1);
- for(Resultrr=ResultScannerFilterList.next();rr!=null;rr=ResultScannerFilterList.next()){
- for(KeyValuekv:rr.list()){
- System.out.println("row:"+newString(kv.getRow()));
- System.out.println("column:"+newString(kv.getColumn()));
- System.out.println("value:"+newString(kv.getValue()));
- }
- }
- }
- publicstaticvoidmain(String[]args)throwsIOException{
- //按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=newArrayList<String>();
- arr.add("course,art,97");
- arr.add("course,math,100");
- HBaseBasic03.selectByFilter("scores",arr);
- }
- }