天天看點

Hive快速入門系列(9) | Hive表中資料的加載與導出

  本次部落客為大家帶來的是Hive表中資料的加載與導出。希望能夠幫助到大家。

目錄

  • ​​一. Hive表中加載資料​​
  • ​​1.1 直接向分區表中插入資料​​
  • ​​1.2 通過查詢插入資料​​
  • ​​1.3 多插入模式​​
  • ​​1.4 查詢語句中建立表并加載資料(as select)​​
  • ​​1.5 建立表時通過location指定加載資料路徑​​
  • ​​二. Hive表中的資料導出(了解就行)​​
  • ​​2.1 insert導出​​
  • ​​2.2 Hadoop指令導出到本地​​
  • ​​2.3 hive shell 指令導出​​
  • ​​2.4 export導出到HDFS上(全表導出)​​
  • ​​三. 清空表資料​​

一. Hive表中加載資料

1.1 直接向分區表中插入資料

create table score3 like score;

insert into table score3 partition(month ='201807') values ('001','002','100');      

1.2 通過查詢插入資料

  • 1. 通過load方式加載資料
(linux) load data local inpath ‘/export/servers/hivedatas/score.csv’ overwrite into table score partition(month=‘201806’);

(HDFS) load data inpath ‘/export/servers/hivedatas/score.csv’ overwrite into table score
partition(month=‘201806’);      
  • 2. 通過查詢方式加載資料
create table score4 like score;
insert overwrite table score4 partition(month = '201806') select s_id,c_id,s_score from score;      

關鍵字overwrite 必須要有

1.3 多插入模式

常用于實際生産環境當中,将一張表拆開成兩部分或者多部分

  • 1. 給score表加載資料
load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score partition(month='201806');      
  • 2. 建立第一部分表:
create table score_first( s_id string,c_id  string) partitioned by (month string) row format delimited fields terminated by '\t' ;      
  • 3. 建立第二部分表:
create table score_second(c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by '\t';      
  • 4. 分别給第一部分與第二部分表加載資料
from score insert overwrite table score_first partition(month='201806') select s_id,c_id insert overwrite table score_second partition(month = '201806')  select c_id,s_score;      
Hive快速入門系列(9) | Hive表中資料的加載與導出

1.4 查詢語句中建立表并加載資料(as select)

将查詢的結果儲存到一張表當中去

create table score5 as select * from score;      

1.5 建立表時通過location指定加載資料路徑

  • 1. 建立表,并指定在hdfs上的位置
create external table score6 (s_id string,c_id string,s_score int) row format delimited fields terminated by '\t' location '/myscore6';      
  • 2. 上傳資料到hdfs上
hdfs dfs -mkdir -p /myscore6
  hdfs dfs -put score.csv /myscore6;      
  • 3. 查詢資料
select * from score6;      

二. Hive表中的資料導出(了解就行)

  将hive表中的資料導出到其他任意目錄,例如linux本地磁盤,例如hdfs,例如mysql等等

2.1 insert導出

  • 1. 将查詢的結果導出到本地
insert overwrite local directory '/export/servers/exporthive' select * from score;      
  • 2. 将查詢的結果格式化導出到本地
insert overwrite local directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection items terminated by '#' select * from student;      
  • 3. 将查詢的結果導出到HDFS上(沒有local)
insert overwrite directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection items terminated by '#' select * from score;      

2.2 Hadoop指令導出到本地

dfs -get /export/servers/exporthive/000000_0 /export/servers/exporthive/local.txt;      
Hive快速入門系列(9) | Hive表中資料的加載與導出

2.3 hive shell 指令導出

基本文法:(hive -f/-e 執行語句或者腳本 > file)

bin/hive -e "select * from myhive.score;" > /export/servers/exporthive/score.txt      
Hive快速入門系列(9) | Hive表中資料的加載與導出

2.4 export導出到HDFS上(全表導出)

export table score to '/export/exporthive/score';      
Hive快速入門系列(9) | Hive表中資料的加載與導出

三. 清空表資料

隻能清空管理表,也就是内部表

truncate table score6;      

清空這個表會報錯

本次的分享就到這裡了,

Hive快速入門系列(9) | Hive表中資料的加載與導出

  

\color{#FF0000}{看完就贊,養成習慣!!!}

看完就贊,養成習慣!!!^ _ ^ ❤️ ❤️ ❤️

繼續閱讀