hive与hbase关联表

13 篇文章 0 订阅

hive创建 关联hbase表有2种形式:

一、建立hive内表,指向hbase:(数据是存在hbase中的)

通过下面的方式创建hive的内表,这种情况是hbase本来没有这张表,创建后会在hbase中同样创建一张表,将来数据也是存放在hbase中的;hdfs的hive表目录有hive文件夹,但是里面没有数据

create tablehive_hbase_test(id string,name string, age int) stored by'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties("hbase.columns.mapping" = ":key,cf:name,cf:age")tblproperties ("hbase.table.name" = "hive_hbase_test");

1、创建hive-hbase表:

hive> createtable hive_hbase_test(id string,name string, age int)

    > stored by'org.apache.hadoop.hive.hbase.HBaseStorageHandler'

    > with serdeproperties("hbase.columns.mapping" = ":key,cf:name,cf:age")

    > tblproperties("hbase.table.name" = "hive_hbase_test");

1这时,如果使用load data 命令导入数据会报错,因为该表是non-native table

hive> load datalocal inpath '/home/qytt/test' into table hive_habse_test;

FAILED:SemanticException [Error 10101]: A non-native table cannot be used as targetfor LOAD

 

2)创建普通hive内表,加载数据:

hive> createtable test_liu (id string,name string, age int)

    > ROW FORMAT DELIMITED

    > FIELDS TERMINATED BY '\t'

    > STORED AS TEXTFILE;

 

hive> load datalocal inpath '/home/qytt/test' into table test_liu;      

Copying data from file:/home/qytt/test

Copying file: file:/home/qytt/test

Loading data totable qytt.test_liu

OK

查看test_liu表在hdfs中查看数据文件:

$ hadoop fs -ls -R/hive/warehouse/qytt.db/test_liu

-rwxr-xr-x   3 qytt qytt         22 2017-04-09 11:33/hive/warehouse/qytt.db/test_liu/test

 

3导入数据到hive-hbase

hive> insertoverwrite table hive_hbase_test select * from test_liu;

Total jobs = 1

Time taken: 26.199seconds

查询数据:

hive> select *from hive_hbase_test;

OK

1        test        2

2        liuxiao        34

Time taken: 0.138seconds, Fetched: 2 row(s)

通过show create tablehive_hbase_test命令查看到hive_hbase_test表在hdfs上的目录,然后在hdfs中查看数据文件,结果并没有这样的数据文件,这是因为hive_hbase_test表中的数据是在hbase中存放的。

$hadoop fs -ls -R /hive/warehouse/qytt.db/hive_hbase_test

 

4当hive使用overwrite关键字进行插入数据时原本数据不会被删除,有同样的行健会被更新覆盖。因为数据是存在hbase中的,遵守hbase插入数据的规则。

 

2、在hbase中查询数据:

hbase(main):009:0>get 'hive_hbase_test',1

COLUMN                                 CELL                                                                                                         

 cf:age                               timestamp=1491708915228, value=2                                                                             

 cf:name                              timestamp=1491708915228, value=test                                                                          

2 row(s) in 0.1620seconds

 

3、其他:

  1. 当hive删除hive表时,hbase表也会删除
  2. 当先删除hbase的时候,先disabled table,然后drop table;这时hbase表就被删除了,zookeeper里面也就删除了。但是hive里面还在,用show tables还能查出来。mysql中TBLS里面还有hive表的信息。但是用select * from hive 查询的时候报错,表不存在(TableNotFoundException)然后删除hive里面的表的时候会报错TableNotFoundException)。继续show tables时,发现表已经不在了。TBLS里面也没有hive表了。

 

二、建立hive外表(external),引用hbase中一张已经存在的表:

这种情况是hbase里面已经有这张表了,创建一个hive表去管理这hbase表,方便使用sql来操作hbase中的数据(不用手工写mapreduce任务了)建立这样的外表,数据也是在hbase中存放,hive会在hdfs中创建目录,但没有数据文件。

 

1、创建hive外表,引用hbase中的表hive_hbase_test

hive> createexternal table ext_hive_hbase(id string,name string ,ct string)

    > stored by'org.apache.hadoop.hive.hbase.HBaseStorageHandler'

    > with serdeproperties("hbase.columns.mapping" = ":key,cf:name,cf:ct")

    > tblproperties("hbase.table.name" = "hive_hbase_test");

注意:在创建hive的外表时,可以引用hbase中不存在列(例如:hive_hbase_test表中不存在cf:ct列)

 

1)查询:

hive> select *from ext_hive_hbase;

OK

1        test        NULL

2        liuxiao        NULL

Time taken: 0.134seconds, Fetched: 2 row(s)

通过hadoop 只能看到对应的目录,没有数据文件:

$ hadoop fs -ls -R/hive/warehouse/qytt.db/ext_hive_hbase

 

2同样,不能使用load data命令给外表加载数据,因为该表使用HBaseStorageHandler创建的:

hive> load datalocal inpath '/home/qytt/test' into table ext_hive_hbase;  

FAILED:SemanticException [Error 10101]: A non-native table cannot be used as targetfor LOAD

 

2、导入数据:

1)查询一张hive本身的内表:

hive> select *from test_liu;

OK

1        test        20

2        liuxiao        30

3        xiaoli        29

 

2)导入数据到hive外表:

hive> insertoverwrite table ext_hive_hbase select * from test_liu;

Total jobs = 1

...

Time taken: 22.107seconds

 

3)查询hive外表:

hive> select *from ext_hive_hbase;

OK

1        test        20

2        liuxiao        30

3        xiaoli        29

Time taken: 0.117seconds, Fetched: 3 row(s)

 

4)查询hbase中的表:

hbase(main):015:0>scan 'hive_hbase_test'

ROW                                   COLUMN+CELL                                                                                                  

 1                                    column=cf:age, timestamp=1491708915228, value=2                                                              

 1                                    column=cf:ct, timestamp=1491713128051, value=20                                                              

 1                                    column=cf:name, timestamp=1491713128051, value=test                                                          

 2                                    column=cf:age, timestamp=1491708915228, value=34                                                             

 2                                    column=cf:ct, timestamp=1491713128051, value=30                                                              

 2                                    column=cf:name, timestamp=1491713128051, value=liuxiao                                                       

 3                                    column=cf:ct, timestamp=1491713128051, value=29                                                              

 3                                    column=cf:name, timestamp=1491713128051, value=xiaoli                                                        

3 row(s) in 0.0190seconds

可以发现,在hbase中的数据也发生了改变。

 

3、其他:

  1. 删除hive表对hbase没有影响
  2. 但是先删除hbase表hive就会报TableNotFoundException

 

总结:

hive+hbase这种方式,无论哪种,数据都是在hbase中存放的,hive只会在hdfs上创建目录,不会生成真正的数据文件;

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值