gpt4 book ai didi

sql - 如何动态地从配置单元表中删除分区?

转载 作者:可可西里 更新时间:2023-11-01 14:52:29 25 4
gpt4 key购买 nike

我是 hive 的新手。有人可以帮我解决这个要求吗?

我的需求是动态删除分区。我有一个结果不同区域的 SQL(SQL 如下:在 ALTER TABLE FROM 之后)。现在我想删除由我的 SQL 返回的区域(在我的配置单元表中分区)。

我尝试了以下方法:

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]

有人可以帮我吗?

提前致谢

最佳答案

Shell 脚本:

$ 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

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

test_1.sh:

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

hive 测试.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

关于sql - 如何动态地从配置单元表中删除分区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38857067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com