從零自學Hadoop(16):Hive資料匯入匯出,叢集資料遷移上

sinodzh發表於2016-01-08

閱讀目錄

本文版權歸mephisto和部落格園共有,歡迎轉載,但須保留此段宣告,並給出原文連結,謝謝合作。

文章是哥(mephisto)寫的,SourceLink

 

     上一篇,我們介紹了Hive的表操作做了簡單的描述和實踐。在實際使用中,可能會存在資料的匯入匯出,雖然可以使用sqoop等工具進行關係型資料匯入匯出操作,但有的時候只需要很簡便的方式進行匯入匯出即可

   下面我們開始介紹hive的資料匯入,匯出,以及叢集的資料遷移進行描述。

匯入檔案到Hive

一:語法

LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]

二:從本地匯入

  使用"LOCAL"就可以從本地匯入

三:從叢集匯入

  將語法中"LOCAL"去掉即可。

四:OVERWRITE

  使用該引數,如果被匯入的地方存在了相同的分割槽或者檔案,則刪除並替換,否者直接跳過。

五:實戰

  根據上篇我們建立的帶分割槽的score的例子,我們先構造兩個個文字檔案score_7和score_8分別代表7月和8月的成績,檔案會在後面附件提供下載。

  由於建表的時候沒有指定分隔符,所以這兩個文字檔案的分隔符。

  先將檔案放入到linux主機中,/data/tmp路徑下。

      匯入本地資料

load data local inpath '/data/tmp/score_7.txt' overwrite into table score PARTITION (openingtime=201507);

  我們發現001變成了1這是以為表的那一類為int形,所以轉成int了。

  將score_8.txt 放到叢集中

su hdfs
hadoop fs -put score_8.txt /tmp/input

  匯入叢集資料

load data inpath '/tmp/input/score_8.txt' overwrite into table score partition(openingtime=201508);

  

將其他表的查詢結果匯入表

一:語法

Standard syntax:

INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...) [IF NOT EXISTS]] select_statement1 FROM from_statement;

INSERT INTO TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 FROM from_statement;

 

Hive extension (multiple inserts):

FROM from_statement

INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...) [IF NOT EXISTS]] select_statement1

[INSERT OVERWRITE TABLE tablename2 [PARTITION ... [IF NOT EXISTS]] select_statement2] 

[INSERT INTO TABLE tablename2 [PARTITION ...] select_statement2] ...;

FROM from_statement

INSERT INTO TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1

[INSERT INTO TABLE tablename2 [PARTITION ...] select_statement2] 

[INSERT OVERWRITE TABLE tablename2 [PARTITION ... [IF NOT EXISTS]] select_statement2] ...;

 

Hive extension (dynamic partition inserts):

INSERT OVERWRITE TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) select_statement FROM from_statement;

INSERT INTO TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) select_statement FROM from_statement;

 

二:OVERWRITE

  使用該引數,如果被匯入的表或者分割槽中有相同的內容,則該內容被替換,否者直接跳過。

三:INSERT INTO

  該語法從0.80才開始支援,它會保持目標表,分割槽的原有的資料的完整性。

四:實戰

  我們構造一個和score表結構一樣的表score1

create table score1 (

  id                int,

  studentid       int,

  score              double

)

partitioned by (openingtime string);

  插入資料

insert into table score1 partition (openingtime=201509) values (21,1,'76'),(22,2,'45');

  我們將表score1的查詢結果匯入到score中,這裡指定了201509分割槽。

insert overwrite table score partition (openingtime=201509) select id,studentid,score from score1;

 

動態分割槽插入

一:說明

  本來動態分割槽插入屬於將其他表結果插入的內容,但是這個功能實用性很強,特將其單獨列出來闡述。該功能從Hive 0.6開始支援。

二:引數

  動態分割槽引數會在該命令生命週期內有效,所以一般講修改的引數命令放在匯入之前執行。

Property Default Note
hive.error.on.empty.partition false Whether to throw an exception if dynamic partition insert generates empty results
hive.exec.dynamic.partition false Needs to be set to true to enable dynamic partition inserts
hive.exec.dynamic.partition.mode strict In strict mode, the user must specify at least one static partition in case the user accidentally overwrites all partitions, in nonstrict mode all partitions are allowed to be dynamic
hive.exec.max.created.files 100000 Maximum number of HDFS files created by all mappers/reducers in a MapReduce job
hive.exec.max.dynamic.partitions 1000 Maximum number of dynamic partitions allowed to be created in total
hive.exec.max.dynamic.partitions.pernode 100 Maximum number of dynamic partitions allowed to be created in each mapper/reducer node

三:官網例子

  我們可以下看hive官網的例子

FROM page_view_stg pvs
INSERT OVERWRITE TABLE page_view PARTITION(dt='2008-06-08', country)
       SELECT pvs.viewTime, pvs.userid, pvs.page_url, pvs.referrer_url, null, null, pvs.ip, pvs.cnt

  在這裡country分割槽將會根據pva.cut的值,被動態的建立。注意,這個分割槽的名字是沒有被使用過的,在nonstrict 模式,dt這個分割槽也可以被動態建立。

四:實戰

  我們先清空score表的資料(3個分割槽)

insert overwrite table score partition(openingtime=201507,openingtime=201508,openingtime=201509) select id,studentid,score from score where 1==0;

  將7月8月資料插入到score1

load data local inpath '/data/tmp/score_7.txt' overwrite into table score1 partition(openingtime=201507);
load data local inpath '/data/tmp/score_8.txt' overwrite into table score1 partition(openingtime=201508);

  

  設定自動分割槽等引數

set  hive.exec.dynamic.partition=true;   
set  hive.exec.dynamic.partition.mode=nonstrict;   
set  hive.exec.max.dynamic.partitions.pernode=10000; 

  將score1的資料自動分割槽的匯入到score

insert overwrite table score partition(openingtime) select id,studentid,score,openingtime from score1;

  圖片

 

 

將SQL語句的值插入到表中

一:說明

  該語句可以直接將值插入到表中。

二:語法

Standard Syntax:
INSERT INTO TABLE tablename [PARTITION (partcol1[=val1], partcol2[=val2] ...)] VALUES values_row [, values_row ...]
 
Where values_row is:
( value [, value ...] )
where a value is either null or any valid SQL literal

三:官網例子

CREATE TABLE students (name VARCHAR(64), age INT, gpa DECIMAL(3, 2))
  CLUSTERED BY (age) INTO 2 BUCKETS STORED AS ORC;
 
INSERT INTO TABLE students
  VALUES ('fred flintstone', 35, 1.28), ('barney rubble', 32, 2.32);
 
 
CREATE TABLE pageviews (userid VARCHAR(64), link STRING, came_from STRING)
  PARTITIONED BY (datestamp STRING) CLUSTERED BY (userid) INTO 256 BUCKETS STORED AS ORC;
 
INSERT INTO TABLE pageviews PARTITION (datestamp = '2014-09-23')
  VALUES ('jsmith', 'mail.com', 'sports.com'), ('jdoe', 'mail.com', null);
 
INSERT INTO TABLE pageviews PARTITION (datestamp)
  VALUES ('tjohnson', 'sports.com', 'finance.com', '2014-09-23'), ('tlee', 'finance.com', null, '2014-09-21');

四:實戰

  在將其他表資料匯入到表中的例子中,我們新建了表score1,並且通過SQL語句將資料插入到score1中。這裡就只是將上面的步驟重新列舉下。

  插入資料

insert into table score1 partition (openingtime=201509) values (21,1,'76'),(22,2,'45');

 

--------------------------------------------------------------------

  到此,本章節的內容講述完畢。

模擬資料檔案下載

Github https://github.com/sinodzh/HadoopExample/tree/master/2016/hive%20test%20file

系列索引

  【源】從零自學Hadoop系列索引

 

 

 

 

本文版權歸mephisto和部落格園共有,歡迎轉載,但須保留此段宣告,並給出原文連結,謝謝合作。

文章是哥(mephisto)寫的,SourceLink

 

相關文章