hbase快速入門-- java api 操作

public class HBase {  
public static void main(String[] args) throws IOException {  
Configuration conf = new Configuration();  
// conf.addResource("hbase-site-cluster.xml");//指定文件加載  
conf = HBaseConfiguration.create(conf);  
HBaseAdmin admin = new HBaseAdmin(conf);//HBaseAdmin負責跟表相關的操作如create,drop等   
  
/**========創建表=========*/  
HTableDescriptor desc = new HTableDescriptor("blog");  
desc.addFamily(new HColumnDescriptor("article"));  
desc.addFamily(new HColumnDescriptor("author"));  
admin.createTable(desc );  
HTable table = new HTable(conf, Bytes.toBytes("blog"));//HTabel負責跟記錄相關的操作如增刪改查等  
  
/**=========插入數據=========*/  
  
Put put = new Put(Bytes.toBytes("1"));  
put.add(Bytes.toBytes("article"), Bytes.toBytes("title"), Bytes.toBytes("Head First HBase"));  
put.add(Bytes.toBytes("article"), Bytes.toBytes("content"), Bytes.toBytes("HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data."));  
put.add(Bytes.toBytes("article"), Bytes.toBytes("tags"), Bytes.toBytes("Hadoop,HBase,NoSQL"));  
put.add(Bytes.toBytes("author"), Bytes.toBytes("name"), Bytes.toBytes("hujinjun"));  
put.add(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("一葉渡江"));  
table.put(put);  
/**=========根據rowkey 
Get get = new Get(Bytes.toBytes("1")); 查詢數據=========*/  
Result result = table.get(get);  
  for(KeyValue kv :result.list()){  
  System.out.println("family:" +Bytes.toString(kv.getFamily()));  
  System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));  
  System.out.println("value:" +Bytes.toString(kv.getValue()));  
  System.out.println("Timestamp:" +kv.getTimestamp());  
}  
/**=========遍歷查詢=========*/  
Scan scan = new Scan();  
ResultScanner rs =null;  
try {  
  rs = table.getScanner(scan);  
  for (Result r : rs) {  
    for(KeyValue kv :r.list()){  
      System.out.println("family:" +Bytes.toString(kv.getFamily()));  
      System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));  
      System.out.println("value:" +Bytes.toString(kv.getValue()));  
    }  
  }  
} finally {  
  rs.close();  
}  
/**=========更新=========*/  
//查詢更新前的值  
Get get2 = new Get(Bytes.toBytes("1"));  
get2.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));  
assertThat(Bytes.toString(table.get(get2).list().get(0).getValue()),is("一葉渡江"));  
//更新nickname爲yedu  
Put put2 = new Put(Bytes.toBytes("1")); :  
put2.add(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("yedu"));  
table.put(put2);  
//查詢更新結果  
Get get3 = new Get(Bytes.toBytes("1"));  
get3.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));  
assertThat(Bytes.toString(table.get(get3).list().get(0).getValue()),is("yedu"));  
//查詢nickname的多個(本示例爲2個)版本值  
Get get4 = new Get(Bytes.toBytes("1"));  
get4.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));  
get4.setMaxVersions(2);  
List results = table.get(get4).list();  
assertThat(results.size(),is(2));  
assertThat(Bytes.toString(results.get(0).getValue()),is("yedu"));  
assertThat(Bytes.toString(results.get(1).getValue()),is("一葉渡江"));  
/**=========刪除記錄=========*/  
//刪除指定column  
Delete deleteColumn = new Delete(Bytes.toBytes("1"));  
deleteColumn.deleteColumns(Bytes.toBytes("author"),Bytes.toBytes("nickname"));  
table.delete(deleteColumn);  
assertThat( table.get(get4).list(),nullValue());  
//刪除所有column  
Delete deleteAll = new Delete(Bytes.toBytes("1"));  
table.delete(deleteAll);  
assertThat(table.getScanner(scan).next(),nullValue());  
/**=========刪除表=========*/  
admin.disableTable("blog");  
admin.deleteTable("blog");  
assertThat(admin.tableExists("blog"),is(false));  
}  
}

http://wangmored.iteye.com/blog/1727774

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章