1

我想在“插入覆盖”之前检查某个分区是否已经存在。仅当该分区不存在时才需要插入。

如何修改此查询?

INSERT OVERWRITE TABLE myname.mytable PARTITION (ds='2019-07-19')
4

2 回答 2

1

您也可以使用动态分区插入和where partition column NOT in (select from myname.mytable). 像这样的东西:

INSERT OVERWRITE TABLE myname.mytable PARTITION (ds)
select col1, col2 ...
       '2019-07-19' ds --partition column
  from ... 
where ds not in (select distinct ds from myname.mytable where ds='2019-07-19')

如果分区存在,则选择不会为现有分区返回任何行,因此不会覆盖分区。您也可以使用 NOT EXIST。

于 2019-07-20T09:55:58.113 回答
1

如果您使用动态分区的 hive 表插入/覆盖您的 hive 表,那么它只会在您的 select 语句中获取该分区的情况下覆盖。

SET hive.exec.dynamic.partition = true;
SET hive.exec.dynamic.partition.mode = nonstrict;

INSERT OVERWRITE TABLE db.PartitionTable PARTITION (native_country)
select
 age
,workclass
,education
,marital
,occupation
,race
,sex
,native_country
from db.sometable
where native_country='Vietnam';

反过来是:

#!/bin/bash
table="db.PartitionTable"
partition_col="native_country=Vietnam"
partition_lookup="native_country='Vietnam'"
partition_exists=$(hive -e "show partitions $table" | grep "$partition_col");

echo $partition_exists

#check if partition_exists
 if [ "$partition_exists" = "" ];
 then echo "partition does not exists";
 else echo "partition exists"
 hive -e "select * from $table where ${partition_lookup}" > output.tmp;
 fi
于 2019-07-20T04:22:01.383 回答