标签:
Row Key | Timestamp | Column Family | |
URI | Parser | ||
r1 | t3 | url=http://www.taobao.com | title=天天特价 |
t2 | host=taobao.com | ||
t1 | |||
r2 | t5 | url=http://www.alibaba.com | content=每天… |
t4 | host=alibaba.com |
create ‘person‘,‘username‘,‘userid‘,‘birth‘,‘phone‘,‘sex‘
put ‘person‘,‘1‘,‘username‘,‘Aran‘
truncate ‘person‘
disable ‘person‘
drop ‘person‘
get ‘person‘,‘1‘
scan ‘person‘,FILTER=>"ValueFilter(=,‘binary:1992-2-12‘)"
hbase org.apache.hadoop.hbase.mapreduce.ImportTsv-Dimporttsv.separator=","-Dimporttsv.columns=HBASE_ROW_KEY,username,userid,birth,phone,sex person hdfs://192.168.70.28:9000/dataImport/HbaseTable-1.cvs
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
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.HTablePool;
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.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
publicclassHbaseAPI{
privatestaticConfiguration conf =null;
static{
conf =HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort","2181");
conf.set("hbase.zookeeper.quorum","192.168.70.29");
conf.set("hbase.master","192.168.70.28:60000");
//timeout time
conf.setLong(HConstants.HBASE_REGIONSERVER_LEASE_PERIOD_KEY,1200000);
}
//query by rowkey
publicstaticvoidQueryByrowKey(String tableName,String rowKey){
try{
HTable table =newHTable(conf,tableName);
Get g =newGet(rowKey.getBytes());
long start =System.currentTimeMillis();
Result r = table.get(g);
/*System.out.println("column:rowkey"
+ "====value:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("column:" + new String(keyValue.getFamily())
+ "====value:" + new String(keyValue.getValue()));
}*/
long end = System.currentTimeMillis();
System.out.println(end-start);
}catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//query by condition
publicstaticvoidQueryByCondition(String tableName,String columnName,String columnValue){
try{
HTable table =newHTable(conf,tableName);
Filter filter =newSingleColumnValueFilter(columnName.getBytes(),null,CompareOp.EQUAL,columnValue.getBytes());
Scan s =newScan();
s.setFilter(filter);
long start =System.currentTimeMillis();
ResultScanner rs = table.getScanner(s);
int hang =0;
for(Result r : rs){
/*System.out.println(new String(r.getRow()));
System.out.println("column:rowkey"
+ "====value:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("column:" + new String(keyValue.getFamily())
+ "====value:" + new String(keyValue.getValue()));
}*/
hang++;
}
long end =System.currentTimeMillis();
System.out.println("rownum:"+hang);
System.out.println(end-start);
}catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
publicstaticvoid main(String[] args){
//HbaseAPI.QueryByrowKey("person1000", "12307999");
//HbaseAPI.QueryByCondition("person1000", "username", "User2312397");
HbaseAPI.QueryByCondition("person1000","sex","0");
}
}
publicstaticvoidQueryByCondition(String tableName,String familyName,String columnName,String columnValue){
try{
HTable table =newHTable(conf,tableName);
Filter filter =newSingleColumnValueFilter(familyName.getBytes(),columnName.getBytes(),CompareOp.EQUAL,columnValue.getBytes());
Scan s =newScan();
s.setCaching(1000);
s.setFilter(filter);
long start =System.currentTimeMillis();
ResultScanner rs = table.getScanner(s);
int hang =0;
for(Result r : rs){
System.out.println(newString(r.getRow()));
System.out.println("column:rowkey"
+"====value:"+newString(r.getRow()));
for(KeyValue keyValue : r.raw()){
System.out.println("column:"+newString(keyValue.getFamily())+":"+newString(keyValue.getQualifier())
+"====value:"+newString(keyValue.getValue()));
}
hang++;
}
long end =System.currentTimeMillis();
System.out.println("rownum:"+hang);
System.out.println(end-start);
}catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
标签:
原文地址:http://www.cnblogs.com/goalishman/p/4535905.html