您可以使用正则表达式:
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;
但是(如 Kishore Kumar Suthar 所述:
这不会创建分区表,因为 CTAS 不支持(将表创建为选择)。
我认为获取分区表的唯一方法是获取表的完整创建语句(如 Abraham 所述):
SHOW CREATE TABLE student;
更改它以在您想要的列上创建一个分区。之后,您可以在插入新表时使用带有正则表达式的选择。
如果您的分区列已经是此选择的一部分,那么您需要确保它是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=none”才能在查询的正则表达式部分使用反引号“`”。我在声明后将此参数设置为原始值:'hive.support.quoted.identifiers=column'