Apache hive create table

I have a problem understanding the real meaning of this Apache Hive code, can someone please explain to me if this code actually does something?

ALTER TABLE a RENAME TO a_tmp;
DROP TABLE a;
CREATE TABLE a AS SELECT * FROM a_tmp;

      

+3


source to share


1 answer


ALTER TABLE a RENAME TO a_tmp;

      

It just lets you rename the table a

to a_tmp

.

Let's say your table a

initially points to /user/hive/warehouse/a

, after executing this command, your data will be moved to /user/hive/warehouse/a_tmp

, and the content /user/hive/warehouse/a

will no longer exist. Please note that this behavior of HDFS moving directories only exists in later versions of Hive. Prior to this, the team was RENAME

updating the metastor and not moving directories to HDFS.

Likewise, if you do show tables

after, you will see that it a

no longer exists, but a_tmp

exists. You can no longer query a

at this point because it is no longer registered in the metastor.

DROP TABLE a;

      



It doesn't mean anything, because you've already renamed a

to a_tmp

. Thus, it a

no longer exists in the metastor. It will still print "OK" because it can't be helped.

CREATE TABLE a AS SELECT * FROM a_tmp;

      

You are asking to create a new table named a

and register it in the metastor. You are also asking to fill it with the same data as in a_tmp

(which you already copied from a

before)

In short, you move to a new source table, and then copy the new in the original, so the only thing that makes this request - is to duplicate your original data as a

well as on a_tmp

.

+4


source







All Articles