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
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); -
} -
-
-
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())); -
} -
} -
-
-
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); -
-
} -
- }