Hive创建外部表造成数据重复

1.上传文件

hdfs dfs -put 1.txt /user/mars

2.创建表

create external table if not exists  temp.student(
school_name string comment "学校",
user_name string comment "姓名",
tel string comment "电话")
row format delimited
fields terminated by ','
stored as textfile;

3.加载数据

load data inpath '/user/mars/1.txt' into table temp.student;

4.删除外部表

drop table temp.mars

此时再重新创建外部表,然后加载数据时,报1.txt文件不存在

原因是/user/mars目录下的文件被移动到temp.mars表对应的目录下了

然后,再次上传1.txt文件到/user/mars(HDFS目录)

加载文件到外部表,查询数据时,发现数据时重复的,但原数据并未重复。原因是1.txt文件被加载到外部表时,外部表的目录下已经存在1.txt文件,此时待移动的文件会命名为1_copy_1.txt与1.txt同时存在,所以查询数据会重复。

解决方案:1.加载数据时,指定overwrite,如load data inpath '/user/mars/1.txt' overwrite into table temp.student; 2.也可以直接创建外部表,不加载数据,因为原来的数据已经存在表的目录下了。


版权声明:本文为u011095039原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。