http://blog.sina.com.cn/s/blog_66474b1601017hvx.html
http://www.cnblogs.com/eprsoft/archive/2012/10/22/2734133.html
引言 HBase提供了Java Api的訪問接口,掌握這個就跟Java應用使用RDBMS時需要JDBC一樣重要,本文將繼續前兩篇文章中blog表的示例,介紹常用的Api。 練習前的準備工作
創建一個Maven工程,加入以下依賴: org.apache.hbase hbase 0.90.2 如果你的Maven庫里還沒有hbase,還需要配置下repository cloudera https://repository.cloudera.com/content/groups/public
確保HBase環境已啟動且能連接到,將HBase環境的hbase-site.xml文件拷貝到上述工程的src/test/resources目錄
加載配置
Configuration conf =?
new ?Configuration();? // ?conf.addResource("hbase-site-cluster.xml"); // 可以指定文件加載 ?conf = HBaseConfiguration.create(conf);
?
創建表
HTableDescriptor desc =?
new ?HTableDescriptor(" blog " ); desc.addFamily( new HColumnDescriptor(" article " )); desc.addFamily( new ?HColumnDescriptor(" author " )); admin.createTable(desc );
?
增加記錄
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 和 ColumnName 是二進制值(Java 類型 byte[]),value 是一個字節數組(Java類型 byte[])
根據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(); }
?
可以看到上面代碼我們用了兩次for循環來遍歷迭代。?
更新練習 //查詢更新前的值
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 ));
完整代碼示例
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等 ?HTable table =?new HTable(conf, Bytes.toBytes(" blog " ));// HTabel負責跟記錄相關的操作如增刪改查等 HTableDescriptor desc? =?
new ?HTableDescriptor(" blog " ); desc.addFamily( new HColumnDescriptor(" article " )); desc.addFamily( new ?HColumnDescriptor(" author " )); admin.createTable(desc );? 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);? 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://hi.baidu.com/cpuramdisk/item/007bb0d35bc7d9322b35c723
HBase之Java API
1.Configuration
在使用Java API時,Client端需要知道HBase的配置環境,如存儲地址,zookeeper等信息。這些信息通過Configuration對象來封裝,可通過如下代碼構建該對象
????????Configuration config=HBaseConfiguration.create();
在調用HBaseConfiguration.create()方法時,HBase首先會在classpath下查找hbase-site.xml文件,將里面的信息解析出來封裝到Configuration對象中,如果hbase-site.xml文件不存在,則使用默認的hbase-core.xml文件。
除了將hbase-site.xml放到classpath下,開發人員還可通過config.set(name, value)方法來手工構建Configuration對象。
????????Configuration.set(String name, String value)
2.HBaseAdmin
HBaseAdmin用于創建數據庫表格,并管理表格的元數據信息,通過如下方法構建
????????HBaseAdmin admin=new HBaseAdmin(config);
常用方法:
????????addColumn(tableName,column):為表格添加欄位
????????deleteColumn(tableName,column):刪除指定欄位
????????balanceSwitch(boolean):是否啟用負載均衡
????????createTable(HTableDescriptor desc):創建表格
????????deleteTable(tableName):刪除表格
????????tableExists(tableName):判斷表格是否存在
示例:創建test表格,并為其指定columnFamily為cf
[java] view plaincopyprint?
HBaseAdmin?admin=new?HBaseAdmin(config);?? If(!admin.tableExists(“test”)){?? ????HTableDescriptor?tableDesc=new?HTableDescriptor(“test”);?? ????HColumnDescriptor?cf=new?HColumnDescriptor(“cf”);?? ????tableDesc.addFamily(cf);?? ????admin.createTable(tableDesc);?? }?? HBaseAdmin admin=new HBaseAdmin(config); If(!admin.tableExists(“test”)){ HTableDescriptor tableDesc=new HTableDescriptor(“test”); HColumnDescriptor cf=new HColumnDescriptor(“cf”); tableDesc.addFamily(cf); admin.createTable(tableDesc);}
3.HTable
在HBase中,HTable封裝表格對象,對表格的增刪改查操作主要通過它來完成,構造方法如下:
????????HTable table=new HTable(config,tableName);
在構建多個HTable對象時,HBase推薦所有的HTable使用同一個Configuration。這樣,HTable之間便可共享HConnection對象、zookeeper信息以及Region地址的緩存信息。
示例1:Get操作
[java] view plaincopyprint?
Get?get=new?Get(rowKey);?? Result?res=table.get(get);?? Get get=new Get(rowKey); Result res=table.get(get);示例2:Put操作
[java] view plaincopyprint?
Put?put=new?Put(rowKey);?? put.add(columnFamily,column,value);?? table.put(put);?? Put put=new Put(rowKey); put.add(columnFamily,column,value);table.put(put); 注:在HBase中,實體的新增和更新都是通過Put操作來實現。
示例3:Delete操作
[java] view plaincopyprint?
Delete?delete=new?Delete();?? table.delete(delete);?? Delete delete=new Delete(); table.delete(delete);示例4:Scan操作
[java] view plaincopyprint?
Scan?scan=new?Scan(?);?? scan.addColumn(columnFamily,column);//指定查詢要返回的column ?? SingleColumnValueFilter?filter=new?SingleColumnValueFilter(?? ????????columnFamily,column,//指定要過濾的column ?? ????????CompareOp.EQUAL,value//指定過濾條件 ?? );?? //更多的過濾器信息請查看org.apache.hadoop.hbase.filter包 ?? scan.setFilter(filter);//為查詢指定過濾器 ?? ResultScanner?scanner=table.getScanner(scan);//執行掃描查找 ?? Iterator<Result>?res=scanner.iterator(?);//返回查詢遍歷器 ?
總結
以上是生活随笔 為你收集整理的HBase应用笔记:通过Java Api与HBase交互(转自 Taobao QA Team) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。