日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

HBase应用笔记:通过Java Api与HBase交互(转自 Taobao QA Team)

發(fā)布時間:2024/4/17 java 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HBase应用笔记:通过Java Api与HBase交互(转自 Taobao QA Team) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

http://blog.sina.com.cn/s/blog_66474b1601017hvx.html

http://www.cnblogs.com/eprsoft/archive/2012/10/22/2734133.html

引言
HBase提供了Java Api的訪問接口,掌握這個就跟Java應(yīng)用使用RDBMS時需要JDBC一樣重要,本文將繼續(xù)前兩篇文章中blog表的示例,介紹常用的Api。
練習前的準備工作

創(chuàng)建一個Maven工程,加入以下依賴:

org.apache.hbase
hbase
0.90.2

如果你的Maven庫里還沒有hbase,還需要配置下repository


cloudera
https://repository.cloudera.com/content/groups/public

確保HBase環(huán)境已啟動且能連接到,將HBase環(huán)境的hbase-site.xml文件拷貝到上述工程的src/test/resources目錄

加載配置

Configuration conf =?new?Configuration();?//?conf.addResource("hbase-site-cluster.xml");//可以指定文件加載?conf = HBaseConfiguration.create(conf);

?


創(chuàng)建表

HTableDescriptor desc =?new?HTableDescriptor("blog"); desc.addFamily(newHColumnDescriptor("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 是一個字節(jié)數(shù)組(Java類型 byte[])


根據(jù)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循環(huán)來遍歷迭代。?


更新練習

//查詢更新前的值

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


//查詢更新結(jié)果

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負責跟表相關(guān)的操作如create,drop等?HTable table =?newHTable(conf, Bytes.toBytes("blog"));//HTabel負責跟記錄相關(guān)的操作如增刪改查等HTableDescriptor desc?=?new?HTableDescriptor("blog"); desc.addFamily(newHColumnDescriptor("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);?//查詢更新結(jié)果?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("一葉渡江"));?//刪除指定columnDelete 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的配置環(huán)境,如存儲地址,zookeeper等信息。這些信息通過Configuration對象來封裝,可通過如下代碼構(gòu)建該對象

????????Configuration config=HBaseConfiguration.create();

在調(diào)用HBaseConfiguration.create()方法時,HBase首先會在classpath下查找hbase-site.xml文件,將里面的信息解析出來封裝到Configuration對象中,如果hbase-site.xml文件不存在,則使用默認的hbase-core.xml文件。

除了將hbase-site.xml放到classpath下,開發(fā)人員還可通過config.set(name, value)方法來手工構(gòu)建Configuration對象。

????????Configuration.set(String name, String value)

2.HBaseAdmin

HBaseAdmin用于創(chuàng)建數(shù)據(jù)庫表格,并管理表格的元數(shù)據(jù)信息,通過如下方法構(gòu)建

????????HBaseAdmin admin=new HBaseAdmin(config);

常用方法:

????????addColumn(tableName,column):為表格添加欄位

????????deleteColumn(tableName,column):刪除指定欄位

????????balanceSwitch(boolean):是否啟用負載均衡

????????createTable(HTableDescriptor desc):創(chuàng)建表格

????????deleteTable(tableName):刪除表格

????????tableExists(tableName):判斷表格是否存在

示例:創(chuàng)建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封裝表格對象,對表格的增刪改查操作主要通過它來完成,構(gòu)造方法如下:

    ????????HTable table=new HTable(config,tableName);

    在構(gòu)建多個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操作來實現(xiàn)。

    示例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);//執(zhí)行掃描查找 ??
  • Iterator<Result>?res=scanner.iterator(?);//返回查詢遍歷器 ?

  • 總結(jié)

    以上是生活随笔為你收集整理的HBase应用笔记:通过Java Api与HBase交互(转自 Taobao QA Team)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。