问题
In the following HiveQL code, I want to add partition to an existing table:
-- my_table was defined and partitioned by `dt string`, which is date
-- now I want to add partition
alter table my_table add if not exists
partition (dt=current_date()); #FAILED: ParseException line 1:72 extraneous input '(' expecting ) near '<EOF>'
alter table my_table add if not exists
partition (dt=${current_date()}); # FAILED: ParseException line 1:60 cannot recognize input near '$' '{' 'current_date' in constant
However, the above code won't work, why? Any other way to do it?
回答1:
Hive expect literals in the alter partition
NoViableAltException(26@[215:1: constant : ( Number | dateLiteral | timestampLiteral | StringLiteral | stringLiteralSequence | BigintLiteral | SmallintLiteral | TinyintLiteral | DecimalLiteral | charSetStringLiteral | booleanValue );])
Also, the CURRENT_DATE is only available from Hive 2.0, so based in your error, probably you are working with a lower version.
Workaround.
Write a file with the following statement
ALTER TABLE my_table add if not exists partition(dt= '${hiveconf:mytime}')
and call hive like (in linux)
mydate=$(date +"%m-%d-%y")
hive -S -hiveconf mytime=$mydate-f test.hql
I hope that help.
PS: I would like to try the alter command with CURRENT_DATE in Hive 2.0, anyway you should use it like CURRENT_DATE instead of CURRENT_DATE()
来源:https://stackoverflow.com/questions/41825726/retrieve-udf-results-in-hive