一、hive相關
1、Hive複製分區表和數據
1)非分區表
複製表結構: create table new_table as select * from exists_table where 1=0;
複製表結構和數據: create table new_table as select * from exists_table;
2)分區表
創建一個分區表:
create table if not exists test_table_syt
(
id string COMMENT '學號',
name string COMMENT '姓名',
sex string COMMENT '性別'
)
COMMENT '拍賣離線10分鐘下鑽補齊10分鐘序列'
PARTITIONED BY ( dt string )
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
WITH SERDEPROPERTIES( 'field.delim' = '\t', 'serialization.null.format' = '' )
STORED AS
INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';
插入數據:
insert overwrite table test_table_syt partition(dt='2018-08-16')
values ('1','張三','男'),('2','李華','女') ;
複製表:
注意差異: as select 複製的是一個非分區表, like 複製的是一個分區表。
-- 用 like 複製一個新表
create table test_tab1 like test_table_syt;
-- 用 as select 複製一個新表
create table test_tab2 as select * from test_table_syt where dt='2018-08-16' limit 0;
3)複製數據
(1)創建新表: create table test_table like test_table_syt;
(2)將HDFS的數據文件複製一份到新表目錄;
hive cmd模式下:dfs -cp -f /user/hive/warehouse/test_table_syt/* /user/hive/warehouse/test_table/
(3)修復分區元數據信息;
hive cmd模式下: MSCK REPAIR TABLE test_table;