一:普通表同步
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;