I have a problem understand the real meaning behind this Apache Hive code, Can someone please explain to me whether this code is really doing anything?
ALTER TABLE a RENAME TO a_tmp;
DROP TABLE a;
CREATE TABLE a AS SELECT * FROM a_tmp;
ALTER TABLE a RENAME TO a_tmp;
This simply allows you to rename your table a
to a_tmp
.
Let's say your table a
initially points to /user/hive/warehouse/a
, then after executing this command your data will be moved to /user/hive/warehouse/a_tmp
and the content of /user/hive/warehouse/a
will no longer exist. Note that this behavior of moving HDFS directories only exist in more recent versions of Hive. Before that the RENAME
command was only updating the metastore and not moving directories in HDFS.
Similarly, if you do a show tables
after, you will see that a
doesn't exist anymore, but a_tmp
exists. You can no longer query a
at that point because it is no longer registered in the metastore.
DROP TABLE a;
This does basically nothing, because you already renamed a
to a_tmp
. So a
doesn't exist anymore in the metastore. This will still print "OK" because there's nothing to do.
CREATE TABLE a AS SELECT * FROM a_tmp;
You are asking to create a brand new table called a
and register it in the metastore. You are also asking to populate it with the same data that is in a_tmp
(which you already copied from a
before)
So in short you're moving your initial table to a new one, and then copying the new one back to the original, so the only thing these query do is duplicating your initial data into both a
and a_tmp
.