日期:2014-05-16  浏览次数:20582 次

HBase0.89常用API接口使用
HTable:这个是table对象,通过他来完成对hbase表的各种操作,删除,更新,创建,查询

RowFilter:对rowkey过滤查询的类

QualifierFilter:对子列做过滤查询的类

FilterList:复合filter的实现类

Get:rowkey定位器

Scan:查询扫描器

ResultScanner:查询结果扫描器

Result:单个查询结果

KeyValue:查询结果中的keyvalue原子项

//场景一,通过rowkey,列族,子列直接定位value

Configuration conf = Configuration.create();

HTable table = new HTable(conf,”mytable”);

Get get = new Get(“rowkey1”.getBytes());

get.addFamily(“myfamily”.getBytes());

Result rs = table.get(get);

If(rs.isEmpty()){

System.out.println(“no record”);

}else{

List<KeyValue> kvList = rs.list();

for(KeyValue kv : kvList){

System.out.println(new String(kv.getRow(),”UTF-8”) + “->”);

System.out.println(new String(kv.getQualifier (),”UTF-8”) + “:”);

System.out.println(new String(kv.getValue(),”UTF-8”) );

}

}

//场景二,使用rowkey的模糊匹配来查询

Configuration conf = Configuration.create();

HTable table = new HTable(conf,”mytable”);

Scan sc = new Scan(“somewords”)

sc.addFamily(“myfamily”.getBytes());

ResultScanner rsan = table.getScanner(sc);

Resullt rs = rsan.next();

If(rs==null){

System.out.println(“no record”);

}else{

While(rs!=null){

List<KeyValue> kvList = rs.list();

for(KeyValue kv : kvList){

System.out.println(new String(kv.getRow(),”UTF-8”) + “->”);

System.out.println(new String(kv.getQualifier (),”UTF-8”) + “:”);

System.out.println(new String(kv.getValue(),”UTF-8”) );

}

rs = rsan.next();

}

}

//场景三,使用rowkey和子列的filter进行条件查询

Configuration conf = Configuration.create();

HTable table = new HTable(conf,”mytable”);

Scan sc = new Scan()

sc.addFamily(“myfamily”.getBytes());

RowFilter filter1 = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(“^”+accountlike+”~(.*)”));//利用正则表达式来查找rowkey

QualifierFilter filter2 = new QualifierFilter(CompareOp.LESS_OR_EQUAL,new BinaryComparator(“stop”.getBytes()));

QualifierFilter filter3 = new QualifierFilter(CompareOp.GREATER_OR_EQUAL,new BinaryComparator(“start”.getBytes()));

FilterList flist = new FilterList(FilterList.Operator.MUST_PASS_ALL);

flist.addFilter(filter1);

flist.addFilter(filter2);

flist.addFilter(filter3);

sc.addFilter(flist);

ResultScanner rsan = table.getScanner(sc);

Resullt rs = rsan.next();

If(rs==null){

System.out.println(“no record”);

}else{

While(rs!=null){

List<KeyValue> kvList = rs.list();

for(KeyValue kv : kvList){

System.out.println(new String(kv.getRow(),”UTF-8”) + “->”);

System.out.println(new String(kv.getQualifier (),”UTF-8”) + “:”);

System.out.println(new String(kv.getValue(),”UTF-8”) );

}

rs = rsan.next();

}

}

未完待续。。。