1. 非分区表:
复制表结构: create table new_table as select * from exists_table where 1=0;
复制表结构和数据: create table new_table as select * from exists_table;
2. 分区表:
-- 创建一个分区表
drop table if exists kimbo_test;
create table kimbo_test
(
order_id int,
system_flag string
)
PARTITIONED BY(dt string )
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\001' LINES TERMINATED BY '\n'
STORED AS TEXTFILE
;
-- 插入数据
insert overwrite table kimbo_test partition(dt='20170601')
values (186000983,'A'),(286000983,'B') ;
insert overwrite table kimbo_test partition(dt='20170602')
values (386000983,'F'),(486000983,'W') ;
create table test_par like kimbo_test;
-- 用 as select 复制一个新表
create table test_par2 as select * from kimbo_test where dt='20170601' limit 0;
-- 用 like 复制一个新表
create table test_par3 like kimbo_test;
-- 注意差异: as select 复制的是一个非分区表, like 复制的是一个分区表。
View Code
表结构截图:
3. 将原表的数据复制到 新表(test_par3)
1. 创建新表: create table test_par3 like kimbo_test;
hive cmd模式下: dfs -cp -f /user/hive/warehouse/kimbo_test/* /user/hive/warehouse/test_par3/
hive cmd模式下: MSCK REPAIR TABLE test_par3;
结果查询:
-------> by kimbo_zhang