HDFS数据同步到Hive

一:普通表同步

1.首先使用关键字external创建外部表

create external table if not exists test(
  id string COMMENT '编号',
  name string COMMENT '名称'
)
row format delimited fields terminated by '\t'  
location 'hdfs存储数据的路径';

2.location位置指向hdfs存储数据的路径

3.只要创建的是外部表,location指向hdfs存储路径,数据就可以直接映射到hive表中

4.可以在创建见一些临时表(内部表),通过查询写入数据

create external table if not exists tmp_test(
  id string COMMENT '编号',
  name string COMMENT '名称'
)
row format delimited fields terminated by '\t';

#第一次可以使用覆盖写入
insert overwrite table tmp_test select * from test;

#第二次追加写入即可
insert into table  tmp_test select * from test;

5.后续操作尽可能在临时表操作,初始化数据保持不变

二:分区表同步

执行完《普通表同步》1-3步骤,hdfs有数据但是hive查不到数据

create external table if not exists test(
  id string COMMENT '编号',
  name string COMMENT '名称'
)
partitioned by (dt string) --dt为分区字段,表中不存在的字段
row format delimited fields terminated by '\t'  
location 'hdfs存储数据的路径';

解决办法:因为创建的是分区表,所以需要进行分区修复,才能成功显示数据

注意:hdfs上一个文件夹对应hive中一个分区

#手动添加分区
alter table test add if not exists partition (dt='xxx') location 'hdfs存储数据路径';

#查询
select * from test where dt='xxx' limit 1;

#查询分区
describe partitions test;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值