1. 程式人生 > >sql:insert into table 與 insert overwrite table 區別

sql:insert into table 與 insert overwrite table 區別

建立測試表,來測試看看測試結果:

create table test(name string,pwd string,createdate string)row format delimited fields terminated by ',';

第一步:使用insert into 插入資料到表中:

insert into test(name,pwd,createdate)values('name1','pwd1','2017-06-20 14:14:09');
insert into test(name,pwd,createdate)values('name1','pwd1','2017-06-20 14:14:09');
insert into test(name,pwd,createdate)values('name2','pwd2','2017-06-20 14:14:09');
insert into test(name,pwd,createdate)values('name2','pwd2','2017-06-20 14:14:09');
0: jdbc:hive2://10.78.152.52:21066/> select * from test;
+------------+-----------+----------------------+--+
| test.name  | test.pwd  |   test.createdate    |
+------------+-----------+----------------------+--+
| name1      | pwd1      | 2017-06-20 14:14:09  |
| name1      | pwd1      | 2017-06-20 14:14:09  |
| name2      | pwd2      | 2017-06-20 14:14:09  |
| name2      | pwd2      | 2017-06-20 14:14:09  |
+------------+-----------+----------------------+--

第二步:不清理以上插入的記錄,直接執行insert overwirte,並查詢分析結果:

insert overwrite table test select 'name1' as name,'pwd1' as pwd,'2017-06-20 14:14:09' as createdate;
insert overwrite table test select 'name2' as name,'pwd2' as pwd,'2017-06-20 14:14:09' as createdate;
insert overwrite table test select 'name2' as name,'pwd2' as pwd,'2017-06-20 14:14:09' as createdate;
insert overwrite table test select 'name3' as name,'pwd3' as pwd,'2017-06-20 14:14:09' as createdate;
0: jdbc:hive2://10.78.152.62:21066/> select * from test;
+------------+-----------+----------------------+--+
| test.name  | test.pwd  |   test.createdate    |
+------------+-----------+----------------------+--+
| name3      | pwd3      | 2017-06-20 14:14:09  |
+------------+-----------+----------------------+--+

第三步:不清理上邊步驟執行後的結果,接著執行以下sql語句,並查詢結果:

insert overwrite table test select 'name2' as name,'pwd2' as pwd,'2017-06-20 14:14:09' as createdate;
0: jdbc:hive2://10.78.152.62:21066/> select * from test;
+------------+-----------+----------------------+--+
| test.name  | test.pwd  |   test.createdate    |
+------------+-----------+----------------------+--+
| name2      | pwd2      | 2017-06-20 14:14:09  |
+------------+-----------+----------------------+--+