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

标签 sql hadoop hive hdfs

我是 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/

相关文章:

Mysql SQL更新没有www的网址

php - 将不同行中的值连接到一列中

mysql - SQL:如何在 Sql 中使用条件 AND 和 OR

java - 为什么这个使用 Combiner 类的 Hadoop 示例不能正常工作? (不要执行Combiner提供的 "local reduction")

hadoop - 未设置JAVA_HOME,无法找到。安装HADOOP时出现错误

sql - 如何在以下情况下在Spark和Hive查询中写入

c# - 执行存储为资源的 SQL 脚本

hadoop - 在Hadoop文件系统上执行更新的最佳方法是什么?

java - Hive 中的 Statement.setMaxRows 与 Statement.setFetchsize 之间有什么区别

apache-spark - Spark : load or select Hive table of ORC format