hive overwrite into不会删除旧文件的一种情况

    技术2023-11-28  95

    hive overwrite into不会删除旧文件的一种情况

    近期工作中发现一个hive overwrite into table的一个bug,如下

    近期工作中发现一个hive overwrite into table的一个bug,如下

    1、创建两张表,指定同一目录 1.1 创建表aaa:

    use tmp; drop table tmp.aaa; CREATE EXTERNAL TABLE tmp.aaa( id string comment ‘id’ ) comment ‘aaa’ partitioned by (dt string) ROW FORMAT SERDE ‘org.openx.data.jsonserde.JsonSerDe’ with SERDEPROPERTIES ( “ignore.malformed.json” = “true”) stored as TEXTFILE LOCATION ‘/user/hive/warehouse/tmp.db/djaaa’;

    1.2 创建表bbb:

    use tmp; drop table tmp.bbb; CREATE EXTERNAL TABLE tmp.aaa( id string comment ‘id’ ,name string comment ‘name’ ) comment ‘bbb’ partitioned by (dt string) ROW FORMAT SERDE ‘org.openx.data.jsonserde.JsonSerDe’ with SERDEPROPERTIES ( “ignore.malformed.json” = “true”) stored as TEXTFILE LOCATION ‘/user/hive/warehouse/tmp.db/djaaa’;

    1.3 如上,两个表LOCATION到了同一目录:"/user/hive/warehouse/tmp.db/djaaa"

    2、下面进行bug测试 2.1 首先使用overwrite into 向表aaa导入数据:

    load data local inpath ‘/home/supdev/dianjun.gao/qq.txt’ overwrite into table tmp.aaa partition (dt=‘2020-06-31’); Loading data to table tmp.aaa partition (dt=2020-06-31) OK Time taken: 1.043 seconds

    查看aaa的源文件目录 看到已经将本地文件“qq.txt”上传到hdfs了 2.2 接下来向表bbb导入数据:

    hive> load data local inpath ‘/home/supdev/dianjun.gao/shanbudiao.txt’ overwrite into table tmp.bbb partition (dt=‘2020-06-31’); Loading data to table tmp.bbb partition (dt=2020-06-31) OK Time taken: 1.109 seconds

    3、BUG出现 现在来观察源文件目录,新文件导入成功了 但是本应被覆盖掉的“qq.txt”依旧存在,也就是说overwrite并没有覆盖掉原有分区下的文件

    4、总结:经过多次测试,指定了相同文件位置的两张表: 1、表aaa第一次插入的数据,如果没有被overwrite过的话,表bbb第一次插入的数据会和表aaa的历史数据共存。 2、经过一次overwrite后,不再有此问题

    Processed: 0.010, SQL: 9