如何动态删除hive表中的分区?

时间:2016-08-09 17:29:15

标签: sql hadoop hive hdfs

我是新手。有人可以帮我解决这个问题吗?

我的要求是动态删除分区。我有一个SQL导致各个区域(SQL在下面:ALTER TABLE FROM之后)。现在我想删除我的SQL返回的区域(在我的hive表中分区)。

我尝试了以下方式:

ALTER TABLE <TableName> PARTITION(region=tab.region)
FROM
select tab.region from 
(SELECT * from Table1) tab  join
(select filename from  Table2) tab1
on tab1.filename = tab.filename

它抛出以下异常:

'1:21:13  [ALTER - 0 row(s), 0.008 secs]  [Error Code: 40000, SQL State: 42000]  Error while compiling statement: FAILED: ParseException 
line 1:77 cannot recognize input near 'tab' '.' 'region' in constant
... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.008/0.000 sec  [0 successful, 0 warnings, 1 errors]

有人能帮帮我吗?

先谢谢

1 个答案:

答案 0 :(得分:1)

<强> 壳脚本:

$ cat test
#!/bin/sh

IFS=$'\n' 

#get partitions 
part=`hive -e "select tab.region from (SELECT * from Table1) tab  join (select filename from  Table2) tab1 on tab1.filename = tab.filename"`

for p in $part
do

   partition=`echo $p|cut -d '=' -f2`

   echo Drooping partitions .... $partition

   #drop partitions
   hive -e "ALTER TABLE test_2 DROP PARTITION(region=\"$partition\")"

done

<强> 输出:

$ ./test
OK
Time taken: 1.343 seconds, Fetched: 2 row(s)
Drooping partitions .... 2016-07-26 15%3A00%3A00
Dropped the partition region=2016-07-26 15%3A00%3A00
OK
Time taken: 2.686 seconds
Drooping partitions .... 2016-07-27
Dropped the partition region=2016-07-27
OK
Time taken: 1.612 seconds

更新:~~&gt;从hql运行shell脚本(这只是一个POC,根据您的要求进行更改。) 使用! <command> - Executes a shell command from the Hive shell.

<强> test_1.sh:

#!/bin/sh
echo " This massage is from $0 file"

<强>蜂房test.hql:

! echo showing databases... ; 
show databases;

! echo showing tables...;
show tables;

! echo runing shell script...;
! /home/cloudera/test_1.sh

<强>输出:

$ hive -v -f hive-test.hql
showing databases...

    show databases
OK
default
retail_edw
sqoop_import
Time taken: 0.997 seconds, Fetched: 3 row(s)
showing tables...

    show tables
OK
scala_departments
scaladepartments
stack
stackover_hive
Time taken: 0.062 seconds, Fetched: 4 row(s)
runing shell script...
 This massage is from /home/cloudera/test_1.sh file