Rocking chief
Rocking chief

Reputation: 1069

Hive truncate table only when it's not empty

I have a hive job that is scheduled to be executed. At the beginning of the job, I want to truncate a table by doing:

TRUNCATE TABLE SOMETABLE

The problem is that the table may be empty. In that case, I don't want to perform the truncate operation which will raise an exception. I know in MySQL you can do something like:

IF EXISTS(SELECT * FROM SOMETABLE)
BEGIN 
TRUNCATE SOMETABLE
END

Is there a way I can achieve something similar in hive? Thanks a lot for your help!

Upvotes: 0

Views: 2169

Answers (1)

notNull
notNull

Reputation: 31490

If the table is empty also hive won't raise any exception.

You can also make use of Temporary tables in hive so that these tables only accessible through the session that established and very useful to manage intermediate data. Once the session got closed then Hive deletes all temporary tables.

Please refer this and this links regarding hive temporary tables.

Upvotes: 1

Related Questions