hive的create、insert、drop、truncate


1. hive建表:create

create table if not exists db_name.test_tb(id string, name string, age string, province string, score string)partitioned by (str_date string) row format delimited fields terminated by '\1'

-- db_name:為數據庫名稱

-- partitioned by (str_date string):設置分區字段

2. 追加插入記錄:insert into

insert into table db_name.test_tb partition(str_date='2020-04-20') values('1','花木蘭','24','北京','98')

insert into table db_name.test_tb partition(str_date='2020-04-20') values('2','李白','28','南京','90')

insert into table db_name.test_tb partition(str_date='2020-04-21') values('3','妲己','26','南京','95')

insert into table db_name.test_tb partition(str_date='2020-04-22') values('4','王昭君','22','上海','96')

當然,也可以查詢插入數據表:

insert into table db_name.test_tb partition(str_date='2020-04-22') select * from db_name.tb_name   #從其他數據表或查詢插入數據

如果沒有分區:

insert into db_name.test_tb values('','','','','')
# 無分區插入數據時,與mysql不同,因為在values前面沒有使用字段

3. 查詢:select

select * from db_name.test_tb

4. 通過select表記錄進行建表

create table if not exists db_name.test_tb_2 select * from db_name.test_tb

select * from db_name.test_tb_2

5. insert overwrite:擦掉原數據,寫入新數據

insert overwrite table db_name.test_tb_2 partition(str_date='2020-04-24') values('5','陳咬金','30','北京','85')  # 不行,因為這種建表方式沒有獲得原表的分區信息

show partitions
db_name.test_tb #獲取分區

既然沒有復制分區,就按照一般表格式進行,此時把str_date字段當做一般字段

insert overwrite table db_name.test_tb_2 values('5','陳咬金','30','北京','85','2020-04-24')

select * from db_name.test_tb_2

同理,也可以查詢插入表記錄:

insert overwrite table db_name.test_tb_2 select * from db_name.tb_name   #從其他數據表或查詢插入數據

# ---------------------------------------------------------------------------------------------------------------------------

以上是沒有分區的insert overwrite,如果有分區呢

insert overwrite table db_name.test_tb partition(str_date='2020-04-24') values('6','狄仁傑','27','北京','96')

select * from db_name.test_tb

是否很好奇,明明是insert overwrite,為啥其他記錄還存在?讓我們再執行一次。

insert overwrite table db_name.test_tb partition(str_date='2020-04-24') values('7','李元芳','24','武漢','92')

select * from db_name.test_tb

可以發現,數據確實改變了,只不過改變的是(str_date='2020-04-24')這個分區的數據,而之前還存在的數據並不是這個分區的。

所以總結

insert overwrite在有分區的情況下,是刷寫的該分區的內容(先擦除原數據,再寫新數據),沒有對其他分區的數據造成影響。

# ------------------------------------------------------------------------------------------------------------------------

注:同樣可以select數據表進行,例如我們繼續刷寫 '2020-04-24' 的分區,利用之前的那個表 test_tb_2:

insert overwrite table db_name.test_tb partition(str_date='2020-04-24') select id, name, age, province, score from db_name.test_tb_2
#沒有用 * 的原因是:str_date已經作為分區字段出現了,那么插入的數據中就不應該有這個字段

6. 刪除表數據,但是不刪除表結構truncate

truncate table db_name.test_tb_2

select * from db_name.test_tb_2

此時,只是返回空表,不會報錯,因為表還存在。

7. 刪除表 (表數據和表結構):drop

drop table db_name.test_tb_2

select * from db_name.test_tb_2  # 報錯,因為表已經不存在了

8. 刪除某分區數據 (alter + drop)

alter table db_name.tb_name drop partition(partdate=20190101);

##

參考:

https://my.oschina.net/sshuj/blog/852596

https://blog.csdn.net/gdkyxy2013/article/details/81200924


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM