久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

hive導(dǎo)入數(shù)據(jù)的四種方式介紹

本篇內(nèi)容主要講解“hive導(dǎo)入數(shù)據(jù)的四種方式介紹”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“hive導(dǎo)入數(shù)據(jù)的四種方式介紹”吧!

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)絡(luò)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、瓊海網(wǎng)站維護(hù)、網(wǎng)站推廣。

Hive的幾種常見(jiàn)的數(shù)據(jù)導(dǎo)入方式
這里介紹四種:
(1)、從本地文件系統(tǒng)中導(dǎo)入數(shù)據(jù)到Hive表;
(2)、從HDFS上導(dǎo)入數(shù)據(jù)到Hive表;
(3)、從別的表中查詢出相應(yīng)的數(shù)據(jù)并導(dǎo)入到Hive表中;
(4)、在創(chuàng)建表的時(shí)候通過(guò)從別的表中查詢出相應(yīng)的記錄并插入到所創(chuàng)建的表中。

一、從本地文件系統(tǒng)中導(dǎo)入數(shù)據(jù)到Hive表

先在Hive里面創(chuàng)建好表,如下:

  1. hive> create table wyp

  2.     > (id int, name string,

  3.     > age int, tel string)

  4.     > ROW FORMAT DELIMITED

  5.     > FIELDS TERMINATED BY '\t'

  6.     > STORED AS TEXTFILE;

  7. OK

  8. Time taken: 2.832 seconds

復(fù)制代碼


這個(gè)表很簡(jiǎn)單,只有四個(gè)字段,具體含義我就不解釋了。本地文件系統(tǒng)里面有個(gè)/home/wyp/wyp.txt文件,內(nèi)容如下:

  1. [wyp@master ~]$ cat wyp.txt

  2. 1       wyp     25      13188888888888

  3. 2       test    30      13888888888888

  4. 3       zs      34      899314121

復(fù)制代碼


wyp.txt文件中的數(shù)據(jù)列之間是使用\t分割的,可以通過(guò)下面的語(yǔ)句將這個(gè)文件里面的數(shù)據(jù)導(dǎo)入到wyp表里面,操作如下:

  1. hive> load data local inpath 'wyp.txt' into table wyp;

  2. Copying data from file:/home/wyp/wyp.txt

  3. Copying file: file:/home/wyp/wyp.txt

  4. Loading data to table default.wyp

  5. Table default.wyp stats:

  6. [num_partitions: 0, num_files: 1, num_rows: 0, total_size: 67]

  7. OK

  8. Time taken: 5.967 seconds

復(fù)制代碼


這樣就將wyp.txt里面的內(nèi)容導(dǎo)入到wyp表里面去了,可以到wyp表的數(shù)據(jù)目錄下查看,如下命令:

  1. hive> dfs -ls /user/hive/warehouse/wyp ;

  2. Found 1 items

  3. -rw-r--r--3 wyp supergroup 67 2014-02-19 18:23 /hive/warehouse/wyp/wyp.txt

復(fù)制代碼


需要注意的是:

和我們熟悉的關(guān)系型數(shù)據(jù)庫(kù)不一樣,Hive現(xiàn)在還不支持在insert語(yǔ)句里面直接給出一組記錄的文字形式,也就是說(shuō),Hive并不支持INSERT INTO …. VALUES形式的語(yǔ)句。

二、HDFS上導(dǎo)入數(shù)據(jù)到Hive表

從本地文件系統(tǒng)中將數(shù)據(jù)導(dǎo)入到Hive表的過(guò)程中,其實(shí)是先將數(shù)據(jù)臨時(shí)復(fù)制到HDFS的一個(gè)目錄下(典型的情況是復(fù)制到上傳用戶的HDFS home目錄下,比如/home/wyp/),然后再將數(shù)據(jù)從那個(gè)臨時(shí)目錄下移動(dòng)(注意,這里說(shuō)的是移動(dòng),不是復(fù)制?。┑綄?duì)應(yīng)的Hive表的數(shù)據(jù)目錄里面。既然如此,那么Hive肯定支持將數(shù)據(jù)直接從HDFS上的一個(gè)目錄移動(dòng)到相應(yīng)Hive表的數(shù)據(jù)目錄下,假設(shè)有下面這個(gè)文件/home/wyp/add.txt,具體的操作如下:

  1. [wyp@master /home/q/hadoop-2.2.0]$ bin/hadoop fs -cat /home/wyp/add.txt

  2. 5       wyp1    23      131212121212

  3. 6       wyp2    24      134535353535

  4. 7       wyp3    25      132453535353

  5. 8       wyp4    26      154243434355

復(fù)制代碼


上面是需要插入數(shù)據(jù)的內(nèi)容,這個(gè)文件是存放在HDFS上/home/wyp目錄(和一中提到的不同,一中提到的文件是存放在本地文件系統(tǒng)上)里面,我們可以通過(guò)下面的命令將這個(gè)文件里面的內(nèi)容導(dǎo)入到Hive表中,具體操作如下:

  1. hive> load data inpath '/home/wyp/add.txt' into table wyp;

  2. Loading data to table default.wyp

  3. Table default.wyp stats:

  4. [num_partitions: 0, num_files: 2, num_rows: 0, total_size: 215]

  5. OK

  6. Time taken: 0.47 seconds

  7. hive> select * from wyp;

  8. OK

  9. 5       wyp1    23      131212121212

  10. 6       wyp2    24      134535353535

  11. 7       wyp3    25      132453535353

  12. 8       wyp4    26      154243434355

  13. 1       wyp     25      13188888888888

  14. 2       test    30      13888888888888

  15. 3       zs      34      899314121

  16. Time taken: 0.096 seconds, Fetched: 7 row(s)

復(fù)制代碼


從上面的執(zhí)行結(jié)果我們可以看到,數(shù)據(jù)的確導(dǎo)入到wyp表中了!請(qǐng)注意load data inpath ‘/home/wyp/add.txt’ into table wyp;里面是沒(méi)有l(wèi)ocal這個(gè)單詞的,這個(gè)是和一中的區(qū)別。

三、從別的表中查詢出相應(yīng)的數(shù)據(jù)并導(dǎo)入到Hive表中

假設(shè)Hive中有test表,其建表語(yǔ)句如下所示:

  1. hive> create table test(

  2.     > id int, name string

  3.     > ,tel string)

  4.     > partitioned by

  5.     > (age int)

  6.     > ROW FORMAT DELIMITED

  7.     > FIELDS TERMINATED BY '\t'

  8.     > STORED AS TEXTFILE;

  9. OK

  10. Time taken: 0.261 seconds

復(fù)制代碼


大體和wyp表的建表語(yǔ)句類似,只不過(guò)test表里面用age作為了分區(qū)字段。對(duì)于分區(qū),這里在做解釋一下:

分區(qū):在Hive中,表的每一個(gè)分區(qū)對(duì)應(yīng)表下的相應(yīng)目錄,所有分區(qū)的數(shù)據(jù)都是存儲(chǔ)在對(duì)應(yīng)的目錄中。比如wyp表有dt和city兩個(gè)分區(qū),則對(duì)應(yīng)dt=20131218,city=BJ對(duì)應(yīng)表的目錄為/user/hive/warehouse/dt=20131218/city=BJ,所有屬于這個(gè)分區(qū)的數(shù)據(jù)都存放在這個(gè)目錄中。


下面語(yǔ)句就是將wyp表中的查詢結(jié)果并插入到test表中:

  1. hive> insert into table test

  2.     > partition (age='25')

  3.     > select id, name, tel

  4.     > from wyp;

  5. #####################################################################

  6.            這里輸出了一堆Mapreduce任務(wù)信息,這里省略

  7. #####################################################################

  8. Total MapReduce CPU Time Spent: 1 seconds 310 msec

  9. OK

  10. Time taken: 19.125 seconds

  11. hive> select * from test;

  12. OK

  13. 5       wyp1    131212121212    25

  14. 6       wyp2    134535353535    25

  15. 7       wyp3    132453535353    25

  16. 8       wyp4    154243434355    25

  17. 1       wyp     13188888888888  25

  18. 2       test    13888888888888  25

  19. 3       zs      899314121       25

  20. Time taken: 0.126 seconds, Fetched: 7 row(s)

復(fù)制代碼

這里做一下說(shuō)明:
我們知道我們傳統(tǒng)數(shù)據(jù)塊的形式insert into table values(字段1,字段2),這種形式hive是不支持的。


通過(guò)上面的輸出,我們可以看到從wyp表中查詢出來(lái)的東西已經(jīng)成功插入到test表中去了!如果目標(biāo)表(test)中不存在分區(qū)字段,可以去掉partition (age=’25′)語(yǔ)句。當(dāng)然,我們也可以在select語(yǔ)句里面通過(guò)使用分區(qū)值來(lái)動(dòng)態(tài)指明分區(qū):

  1. hive> set hive.exec.dynamic.partition.mode=nonstrict;

  2. hive> insert into table test

  3.     > partition (age)

  4.     > select id, name,

  5.     > tel, age

  6.     > from wyp;

  7. #####################################################################

  8.            這里輸出了一堆Mapreduce任務(wù)信息,這里省略

  9. #####################################################################

  10. Total MapReduce CPU Time Spent: 1 seconds 510 msec

  11. OK

  12. Time taken: 17.712 seconds

  13. hive> select * from test;

  14. OK

  15. 5       wyp1    131212121212    23

  16. 6       wyp2    134535353535    24

  17. 7       wyp3    132453535353    25

  18. 1       wyp     13188888888888  25

  19. 8       wyp4    154243434355    26

  20. 2       test    13888888888888  30

  21. 3       zs      899314121       34

  22. Time taken: 0.399 seconds, Fetched: 7 row(s)

復(fù)制代碼


這種方法叫做動(dòng)態(tài)分區(qū)插入,但是Hive中默認(rèn)是關(guān)閉的,所以在使用前需要先把hive.exec.dynamic.partition.mode設(shè)置為nonstrict。當(dāng)然,Hive也支持insert overwrite方式來(lái)插入數(shù)據(jù),從字面我們就可以看出,overwrite是覆蓋的意思,是的,執(zhí)行完這條語(yǔ)句的時(shí)候,相應(yīng)數(shù)據(jù)目錄下的數(shù)據(jù)將會(huì)被覆蓋!而insert into則不會(huì),注意兩者之間的區(qū)別。例子如下:

  1. hive> insert overwrite table test

  2.     > PARTITION (age)

  3.     > select id, name, tel, age

  4.     > from wyp;

復(fù)制代碼


更可喜的是,Hive還支持多表插入,什么意思呢?在Hive中,我們可以把insert語(yǔ)句倒過(guò)來(lái),把from放在最前面,它的執(zhí)行效果和放在后面是一樣的,如下:

  1. hive> show create table test3;

  2. OK

  3. CREATE  TABLE test3(

  4.   id int,

  5.   name string)

  6. Time taken: 0.277 seconds, Fetched: 18 row(s)

  7. hive> from wyp

  8.     > insert into table test

  9.     > partition(age)

  10.     > select id, name, tel, age

  11.     > insert into table test3

  12.     > select id, name

  13.     > where age>25;

  14. hive> select * from test3;

  15. OK

  16. 8       wyp4

  17. 2       test

  18. 3       zs

  19. Time taken: 4.308 seconds, Fetched: 3 row(s)

復(fù)制代碼


可以在同一個(gè)查詢中使用多個(gè)insert子句,這樣的好處是我們只需要掃描一遍源表就可以生成多個(gè)不相交的輸出。這個(gè)很酷吧!

四、在創(chuàng)建表的時(shí)候通過(guò)從別的表中查詢出相應(yīng)的記錄并插入到所創(chuàng)建的表中

在實(shí)際情況中,表的輸出結(jié)果可能太多,不適于顯示在控制臺(tái)上,這時(shí)候,將Hive的查詢輸出結(jié)果直接存在一個(gè)新的表中是非常方便的,我們稱這種情況為CTAS(create table .. as select)如下:

  1. hive> create table test4

  2.     > as

  3.     > select id, name, tel

  4.     > from wyp;

  5. hive> select * from test4;

  6. OK

  7. 5       wyp1    131212121212

  8. 6       wyp2    134535353535

  9. 7       wyp3    132453535353

  10. 8       wyp4    154243434355

  11. 1       wyp     13188888888888

  12. 2       test    13888888888888

  13. 3       zs      899314121

  14. Time taken: 0.089 seconds, Fetched: 7 row(s)

復(fù)制代碼

數(shù)據(jù)就插入到test4表中去了,CTAS操作是原子的,因此如果select查詢由于某種原因而失敗,新表是不會(huì)創(chuàng)建的!

到此,相信大家對(duì)“hive導(dǎo)入數(shù)據(jù)的四種方式介紹”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

本文題目:hive導(dǎo)入數(shù)據(jù)的四種方式介紹
URL地址:http://sd-ha.com/article38/ggeppp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、ChatGPT、建站公司、網(wǎng)站策劃、標(biāo)簽優(yōu)化、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)