Hbase practise

In this article, I’ll introduce about how to import data into hbase from hdfs .

  • Create ‘hive_hbase_nba’ table on hbase via hbase shell .

hbase(main) > create 'hive_hbase_nba', 'details'

  • Create external table ‘hive_nba’ on hdfs via hive commands .

hive> create external table hive_nba(key string, name string, score string)
> row format delimited fields terminated by ','
> stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
> WITH SERDEPROPERTIES
> ("hbase.columns.mapping" = ":key,details:name,details:score")
> TBLPROPERTIES ("hbase.table.name" = "hive_hbase_nba");

  •  Create external table ‘hive_nba_load’ on hdfs via hive commands . (which used to load original data)

hive> create table hive_nba_load(key string, name string, score string)
> row format delimited fields terminated by ',';

  • Load data ‘huston_rocket.txt’ into table ‘hive_nba_load’ table.

hive > load data local inpath '/tmp/huston_rocket.txt' into table hive_nba_load;

  • Check ‘huston_rocket.txt’ details.

$ cat /tmp/huston_rocket.txt

1,Yao,99
2,Mac,98
3,Harden,97
4,Howard,96
5,Martin,95

  •  Insert data into table ‘hive_nba’ from table ‘hive_nba_load’

hive > insert overwrite table hive_nba select * from hive_nba_load;

  • Check the results of ‘hive_nba’.

hive > select * from hive_nba;

Results like the following from hive commands:

hive> select * from hive_nba;
OK
1 Yao 99
2 Mac 98
3 Harden 97
4 Howard 96
5 Martin 95
Time taken: 0.209 seconds, Fetched: 5 row(s)

Results from hbase commands:

hbase(main):001:0> list
TABLE
alogs_hbase
hive_hbase_nba
2 row(s) in 1.7540 seconds

=> ["alogs_hbase", "hive_hbase_nba"]
hbase(main):002:0> scan “hive_hbase_nba”
ROW COLUMN+CELL
1 column=details:name, timestamp=1419057367825, value=Yao
1 column=details:score, timestamp=1419057367825, value=99
2 column=details:name, timestamp=1419057367825, value=Mac
2 column=details:score, timestamp=1419057367825, value=98
3 column=details:name, timestamp=1419057367825, value=Harden
3 column=details:score, timestamp=1419057367825, value=97
4 column=details:name, timestamp=1419057367825, value=Howard
4 column=details:score, timestamp=1419057367825, value=96
5 column=details:name, timestamp=1419057367825, value=Martin
5 column=details:score, timestamp=1419057367825, value=95
5 row(s) in 0.1720 seconds

  •  Insert data into hbase from hbase shell .

hbase(main)> put 'hive_hbase_nba', '8', 'details:name', 'Ivan'

hbase(main)> put 'hive_hbase_nba', '8', 'details:score', '90'

  • Check results in hive command and hbase shell.

hbase(main):005:0> list
TABLE
alogs_hbase
hive_hbase_nba
2 row(s) in 0.0240 seconds

=> ["alogs_hbase", "hive_hbase_nba"]
hbase(main):006:0> scan "hive_hbase_nba"
ROW COLUMN+CELL
1 column=details:name, timestamp=1419057367825, value=Yao
1 column=details:score, timestamp=1419057367825, value=99
2 column=details:name, timestamp=1419057367825, value=Mac
2 column=details:score, timestamp=1419057367825, value=98
3 column=details:name, timestamp=1419057367825, value=Harden
3 column=details:score, timestamp=1419057367825, value=97
4 column=details:name, timestamp=1419057367825, value=Howard
4 column=details:score, timestamp=1419057367825, value=96
5 column=details:name, timestamp=1419057367825, value=Martin
5 column=details:score, timestamp=1419057367825, value=95
8 column=details:name, timestamp=1419058748363, value=Ivan
8 column=details:score, timestamp=1419058760524, value=90
6 row(s) in 0.0510 seconds


hive> select * from hive_nba;
OK
1 Yao 99
2 Mac 98
3 Harden 97
4 Howard 96
5 Martin 95
8 Ivan 90
Time taken: 0.092 seconds, Fetched: 6 row(s)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>