HBase简单代码实例(Java)
下面是學(xué)生的成績(jī)表:
name grad? ?? ?course:math? ?course:art
Tom? ? 1? ? ? ?? ?? ?? ?87? ?? ?? ?? ?? ?? ???97
Jerry? ?2? ?? ?? ?? ?100? ?? ?? ?? ?? ?? ?80
? ? ? ? 這里grad對(duì)于表來(lái)說(shuō)是一個(gè)列,course對(duì)于表來(lái)說(shuō)是一個(gè)列族,這個(gè)列族由兩個(gè)列組成:math和art,當(dāng)然我們可以根據(jù)我們的需要在course中建立更多的列族,如computer,physics等相應(yīng)的列添加入course列族.
? ? ? ? 有了上面的想法和需求,我們就可以在HBase中建立相應(yīng)的數(shù)據(jù)表啦!
1, 建立一個(gè)表格 scores 具有兩個(gè)列族grad 和courese
hbase(main):002:0> create 'scores', 'grade', 'course'
0 row(s) in 4.1610 seconds
2,查看當(dāng)先HBase中具有哪些表
hbase(main):003:0> list
scores
1 row(s) in 0.0210 seconds
3,查看表的構(gòu)造
hbase(main):004:0> describe 'scores'
{NAME => 'scores', IS_ROOT => 'false', IS_META => 'false', FAMILIES => [{NAME => 'course', BLOOMFILTER => 'false', IN_MEMORY => 'false', LENGTH => '2147483647', BLOCKCACHE => 'false', VERSIONS => '3', TTL => '-1', COMPRESSION => 'NONE'}, {NAME => 'grade', BLOOMFILTER => 'false', IN_MEMORY => 'false', LENGTH => '2147483647', BLOCKCACHE => 'false', VERSIONS => '3', TTL => '-1', COMPRESSION => 'NONE'}]}
1 row(s) in 0.0130 seconds
4, 加入一行數(shù)據(jù),行名稱(chēng)為 Tom 列族grad的列名為”” 值位1
hbase(main):005:0> put 'scores', 'Tom', 'grade:', '1'
0 row(s) in 0.0070 seconds
5,給Tom這一行的數(shù)據(jù)的列族添加一列?
hbase(main):006:0> put 'scores', 'Tom', 'course:math', '87'
0 row(s) in 0.0040 seconds
6,給Tom這一行的數(shù)據(jù)的列族添加一列?
hbase(main):007:0> put 'scores', 'Tom', 'course:art', '97'
0 row(s) in 0.0030 seconds
7, 加入一行數(shù)據(jù),行名稱(chēng)為 Jerry 列族grad的列名為”” 值位2
hbase(main):008:0> put 'scores', 'Jerry', 'grade:', '2'
0 row(s) in 0.0040 seconds
8,給Jerry這一行的數(shù)據(jù)的列族添加一列?
hbase(main):009:0> put 'scores', 'Jerry', 'course:math', '100'
0 row(s) in 0.0030 seconds
9,給Jerry這一行的數(shù)據(jù)的列族添加一列?
hbase(main):010:0> put 'scores', 'Jerry', 'course:art', '80'
0 row(s) in 0.0050 seconds
10,查看scores表中Tom的相關(guān)數(shù)據(jù)
hbase(main):011:0> get 'scores', 'Tom'
COLUMN? ?? ?? ?? ?? ?? ?? ???CELL
course:art? ?? ?? ?? ?? ?? ?timestamp=1224726394286, value=97
course:math? ?? ?? ?? ?? ???timestamp=1224726377027, value=87
grade:? ?? ?? ?? ?? ?? ?? ? timestamp=1224726360727, value=1
3 row(s) in 0.0070 seconds
11,查看scores表中所有數(shù)據(jù)
hbase(main):012:0> scan 'scores'
ROW? ?? ?? ?? ?? ?? ?? ?? ???COLUMN+CELL
Tom? ?? ?? ?? ?? ?? ?? ?? ? column=course:art, timestamp=1224726394286, value=97
Tom? ?? ?? ?? ?? ?? ?? ?? ? column=course:math, timestamp=1224726377027, value=87
Tom? ?? ?? ?? ?? ?? ?? ?? ? column=grade:, timestamp=1224726360727, value=1
Jerry? ?? ?? ?? ?? ?? ?? ?? ?column=course:art, timestamp=1224726424967, value=80
Jerry? ?? ?? ?? ?? ?? ?? ?? ?column=course:math, timestamp=1224726416145, value=100
Jerry? ?? ?? ?? ?? ?? ?? ?? ?column=grade:, timestamp=1224726404965, value=2
6 row(s) in 0.0410 seconds
12,查看scores表中所有數(shù)據(jù)courses列族的所有數(shù)據(jù)
hbase(main):013:0> scan 'scores', ['course:']
ROW? ?? ?? ?? ?? ?? ?? ?? ???COLUMN+CELL
Tom? ?? ?? ?? ?? ?? ?? ?? ? column=course:art, timestamp=1224726394286, value=97
Tom? ?? ?? ?? ?? ?? ?? ?? ? column=course:math, timestamp=1224726377027, value=87
Jerry? ?? ?? ?? ?? ?? ?? ?? ?column=course:art, timestamp=1224726424967, value=80
Jerry? ?? ?? ?? ?? ?? ?? ?? ?column=course:math, timestamp=1224726416145, value=100
4 row(s) in 0.0200 seconds
? ? ? ? 上面就是HBase的基本shell操作的一個(gè)例子,可以看出,hbase的shell還是比較簡(jiǎn)單易用的,從中也可以看出HBase shell缺少很多傳統(tǒng)sql中的一些類(lèi)似于like等相關(guān)操作,當(dāng)然,HBase作為BigTable的一個(gè)開(kāi)源實(shí)現(xiàn),而B(niǎo)igTable是作為 google業(yè)務(wù)的支持模型,很多sql語(yǔ)句中的一些東西可能還真的不需要.
? ? ? ? 當(dāng)然,通過(guò) 程序我們也可以對(duì)HBase進(jìn)行相關(guān)的操作.下面的程序就完成了上面shell操作的內(nèi)容:
import?java.io.IOException;
import?java.io.ByteArrayOutputStream;
import?java.io.DataOutputStream;
import?java.io.ByteArrayInputStream;
import?java.io.DataInputStream;
import?java.util.Map;
import?org.apache.hadoop.io.Writable;
import?org.apache.hadoop.io.IntWritable;
import?org.apache.hadoop.hbase.HBaseConfiguration;
import?org.apache.hadoop.hbase.HTableDescriptor;
import?org.apache.hadoop.hbase.HColumnDescriptor;
import?org.apache.hadoop.hbase.client.HBaseAdmin;
import?org.apache.hadoop.hbase.client.HTable;
import?org.apache.hadoop.hbase.io.BatchUpdate;
import?org.apache.hadoop.hbase.io.RowResult;
import?org.apache.hadoop.hbase.io.Cell;
import?org.apache.hadoop.hbase.util.Writables;
public?class?HBaseBasic?{
????public?static?void?main(String[]?args)?throws?Exception?{
????????HBaseConfiguration?config?=?new?HBaseConfiguration();
????????HBaseAdmin admin?=?new?HBaseAdmin(config);
????????if?(admin.tableExists("scores"))?{
????????????System.out.println("drop table");
????????????admin.disableTable("scores");
????????????admin.deleteTable("scores");
????????}
????????System.out.println("create table");
????????HTableDescriptor tableDescripter?=?new?HTableDescriptor("scores".getBytes());
????????tableDescripter.addFamily(new?HColumnDescriptor("grade:"));
????????tableDescripter.addFamily(new?HColumnDescriptor("course:"));
????????admin.createTable(tableDescripter);
????????HTable?table?=?new?HTable(config,?"scores");
????????System.out.println("add Tom's data");
????????BatchUpdate tomUpdate?=?new?BatchUpdate("Tom");
????????tomUpdate.put("grade:",?Writables.getBytes(new?IntWritable(1)));
????????tomUpdate.put("course:math",?Writables.getBytes(new?IntWritable(87)));
????????tomUpdate.put("course:art",?Writables.getBytes(new?IntWritable(97)));
????????table.commit(tomUpdate);
????????System.out.println("add Jerry's data");
????????BatchUpdate jerryUpdate?=?new?BatchUpdate("Jerry");
????????jerryUpdate.put("grade:",?Writables.getBytes(new?IntWritable(2)));
????????jerryUpdate.put("course:math",?Writables.getBytes(new?IntWritable(100)));
????????jerryUpdate.put("course:art",?Writables.getBytes(new?IntWritable(80)));
????????table.commit(jerryUpdate);
????????for?(RowResult row?:?table.getScanner(new?String[]?{?"course:"?}))?{
????????????System.out.format("ROW\t%s\n",?new?String(row.getRow()));
????????????for?(Map.Entry<byte[],?Cell>?entry?:?row.entrySet())?{
????????????????String?column?=?new?String(entry.getKey());
????????????????Cell cell?=?entry.getValue();
????????????????IntWritable?value?=?new?IntWritable();
????????????????Writables.copyWritable(cell.getValue(),?value);
????????????????System.out.format(" COLUMN\t%s\t%d\n",?column,?value.get());
????????????}
????????}
????}
}
? ? ? ? 輸出如下:
drop table
09/07/11 08:51:59 INFO client.HBaseAdmin: Disabled scores
09/07/11 08:51:59 INFO client.HBaseAdmin: Deleted scores
create table
add Tom's data
add Jerry's data
ROW? ???Tom
??COLUMN? ?? ???course:art? ?? ?97
??COLUMN? ?? ???course:math? ???87
ROW? ???Jerry
??COLUMN? ?? ???course:art? ?? ?80
??COLUMN? ?? ???course:math? ???100
總結(jié)
以上是生活随笔為你收集整理的HBase简单代码实例(Java)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Hadoop核心架构HDFS+MapRe
- 下一篇: Hbase API中常用类介绍和使用