HBase学习之一: 创建hive和hbase关联表

背景:项目中需要使用HQL对源数据进行分析,分析的结果需要做近似于实时的查询,所以创建的表就需要在hive和hbase之间相关联,此为背景。

drop table tbl_hive_test;
create external table tbl_hive_test
(
  id   string,
  name string,
  age  string
)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' 
WITH SERDEPROPERTIES("hbase.columns.mapping" = ":key,info:name,info:age")
TBLPROPERTIES("hbase.table.name" = "tbl_hbase_test");

说明:默认第一个字段为rowkey,即为id。info为列簇,name为列名。hbase.table.name为hive表在habse中的名称,创建的是外部表(external),hbase该表必须存在,否则报错(内部表不必在hbase中先创建)
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:MetaException(message:HBase table tbl_hive_test doesn't exist while the table is declared as an external table.)
在hbase shell中先创建create 'tbl_hbase_test','info'
导入数据:
load data local inpath '/tmp/info.txt' overwrite into table tbl_hive_test partition(dt='20000101');
报错:
FAILED: SemanticException [Error 10101]: A non-native table cannot be used as target for LOAD
如下所示:
managed native: what you get by default with CREATE TABLE
external native: what you get with CREATE EXTERNAL TABLE when no STORED BY clause is specified
managed non-native: what you get with CREATE TABLE when a STORED BY clause is specified; Hive stores the definition in its metastore, but does not create any files itself; instead, 
it calls the storage handler with a request to create a corresponding object structure
external non-native: what you get with CREATE EXTERNAL TABLE when a STORED BY clause is specified; Hive registers the definition in its metastore and calls the storage handler to check 
that it matches the primary definition in the other system
可看出上述建表语句属于external non-native,不支持load,只能select另外一张表insert到此表中,建表语句如下:
create table tbl_hive_test1
(
  id   string,
  name string,
  age  string
)
partitioned by(dt string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
用hive的load命令装载数据:
load data local inpath '/tmp/a.txt' overwrite into table tbl_hive_test1 partition(dt='20000101');
a.txt的内容如下:
1001,zhangsan,19
1002,lisi,25
1003,wangwu,26
1004,zhaoliu,18
2001,limei,22
2002,hangmeimei,24
3005,lineng,17
4001,liqi,20
要在hive查看到数据,要在hbase中事先put一条数据才能使用hql查询到hive表中的数据。
put 'tbl_hbase_test','302','info:name','jxz'
put 'tbl_hbase_test','302','info:age','25'
在hbase可查看 scan 'tbl_hbase_test'或get 'tbl_hbase_test','302'
最后再insert到目标表中
insert overwrite table tbl_hive_test select id,name,age from tbl_hive_test1 where dt='20000101';

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值