當我需要將列更改爲分區(convert normal column as partition column in hive)時,我想創建一個新表來複制除一個之外的所有列。我目前在原表中有> 50列。有沒有乾淨的方式做到這一點?HIVE:如何創建另一個表中的所有列的表格除了其中之一?
喜歡的東西:
CREATE student_copy LIKE student EXCEPT age and hair_color;
謝謝!
當我需要將列更改爲分區(convert normal column as partition column in hive)時,我想創建一個新表來複制除一個之外的所有列。我目前在原表中有> 50列。有沒有乾淨的方式做到這一點?HIVE:如何創建另一個表中的所有列的表格除了其中之一?
喜歡的東西:
CREATE student_copy LIKE student EXCEPT age and hair_color;
謝謝!
CREATE TABLE student_copy LIKE student;
它只是複製源表定義。
CREATE TABLE student_copy AS select name, age, class from student;
我用下面的命令來獲得現有表的create語句。
SHOW CREATE TABLE student;
將結果複製並根據您對新表的要求進行修改,然後運行修改後的命令以獲取新表。
您可以使用正則表達式: CTAS using REGEX column spec.:
set hive.support.quoted.identifiers=none;
CREATE TABLE student_copy AS SELECT `(age|hair_color)?+.+` FROM student;
set hive.support.quoted.identifiers=column;
BUT(由Kishore Kumar Suthar提到: 這不會創建一個分區表,如不與CTAS支持(CREATE TABLE AS選擇)。
我看到你只有這樣,才能讓你的分區表是通過獲取完整的創建表的語句(如Abraham提及):
SHOW CREATE TABLE student;
將其更改爲在所需列上創建分區。之後,您可以在插入新表格時使用select和regex。 如果您的分區列已經是此選擇的一部分,那麼您需要確保它是last column you insert。如果不是的話,你可以在正則表達式中排除該列,並將其作爲最後一個列入。此外,如果你希望幾個分區中創建基於您插入語句您需要啓用「動態分區」:
set hive.support.quoted.identifiers=none;
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
INSERT INTO TABLE student_copy PARTITION(partcol1) SELECT `(age|hair_color|partcol1)?+.+`, partcol1 FROM student;
set hive.support.quoted.identifiers=column;
的「hive.support.quoted.identifiers =無」使用反引號「需要' '在查詢的正則表達式部分。我在聲明後將此參數設置爲它的原始值:'hive.support.quoted.identifiers =列'
您是否也想要移動數據?或者簡單地創建一個空表? – luoluo
也想複製數據來創建一個新的分區 – LeonGG