匯入(移動)資料到hive1 1 0表的方法

2022-05-06 12:15:11 字數 4015 閱讀 5755

hive資料匯入**格式(會移動原始檔位置):

load data [local] inpath 'filepath' [overwrite] into table tablename [partition (分割槽列名1=值1,分割槽列名2=值2,...)]

對以上公式細分,hive有好幾種常見的資料匯入方式,這裡介紹四種

(1) 從本地檔案系統中匯入資料到hive表;

(2) 從hdfs上匯入資料到hive表; 

(3) 從別的表中查詢出相應的資料並匯入到hive表中; 

(4) 在建立表的時候通過從別的表中查詢出相應的記錄並插入到所建立的表中。

hive> create table fefjay

> (id int, name string,

> age int, tel string)

> row format delimited

> fields terminated by '\t'

> stored as textfile;

ok time taken: 2.832 seconds

這個表有四個字段,行的各個列之間分隔符是製表符,結果儲存為檔案。

本地檔案系統裡面有個/home/fefjay/fefjay.txt檔案,行的各個列之間分隔符是製表符,內容如下:

[fefjay@master ~]$ cat fefjay.txt  

1 fefjay 25 13188888888888

2 test 30 13888888888888

3 zs 34 899314121

執行命令,把檔案匯入到hive表:

hive> load data local inpath '/home/fefjay/fefjay.txt' into table fefjay;
這樣就將fefjay.txt裡面的內容匯入到fefjay表裡面去了,可以到fefjay表的資料目錄下檢視,如下命令:

hive>dfs -ls /user/hive/warehouse/fefjay
和我們熟悉的關係型資料庫不一樣,hive現在還不支援在insert語句裡面直接給出一組記錄的文字形式,也就是說,hive並不支援insert into …. values形式的語句。

從本地檔案系統中將資料匯入到hive表的過程中,其實是先將資料臨時複製到hdfs的乙個目錄下(典型的情況是複製到上傳使用者的hdfs home目錄下,比如/home/fefjay/),然後再將資料從那個臨時目錄下移動(是移動,不是複製)到對應的hive表的資料目錄裡面。

hdfs有下面這個檔案/home/fefjay/add.txt,具體的操作如下:

hive> load data inpath '/home/fefjay/add.txt' into table fefjay;
這就把資料從hadoop匯入到hive表中了。

檢視hive表資料:

hive> select * from fefjay;
請注意load data inpath 『/home/fefjay/add.txt』 into table fefjay; 裡面是沒有local這個單詞的,這個是和從本地檔案匯入到hive表的區別。

假設hive中有test表,其建表語句如下所示:

hive>create table test(

>id int, name string,

>tel string

>partitioned by

>(age int)

>row format delimited

>fields terminated by '\t'

>stored as textfile;

大體和fefjay表的建表語句類似,只不過test表裡面用age作為了分割槽字段。

對於分割槽,這裡在做解釋一下:在hive中,表的每乙個分割槽對應表下的相應目錄,所有分割槽的資料都是儲存在對應的目錄中。比如fefjay表有dt和city兩個分割槽,則對應dt=20131218,city=bj對應表的目錄為/user/hive/warehouse/dt=20131218/city=bj,所有屬於這個分割槽的資料都存放在這個目錄中。

3.2.1 靜態分割槽插入(目標表有明確分割槽)

將fefjay表中的查詢結果並插入到test表中:

hive> insert into table test

> partition (age='25')

> select id, name, tel from fefjay;

檢視插入結果:

hive> select * from test;
這裡做一下說明: 我們知道我們傳統資料塊的形式insert into table values(欄位1,欄位2),這種形式hive是不支援的。

3.2.2 動態分割槽插入(目標表沒有明確分割槽)

如果目標表(test)中不存在分割槽字段,可以去掉partition (age=』25′)語句。當然,我們也可以在select語句裡面通過使用分割槽值來動態指明分割槽:

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

hive> insert into table test

> partition (age)

> select id, name,

> tel, age

> from fefjay;

這種方法叫做動態分割槽插入,但是hive中預設是關閉的,所以在使用前需要先把

hive.exec.dynamic.partition.mode設定為nonstrict。當然,hive也支援insert overwrite方式來插入資料,從字面我們就可以看出,overwrite是覆蓋的意思,是的,執行完這條語句的時候,相應資料目錄下的資料將會被覆蓋!而insert into則不會,注意兩者之間的區別。例子如下:

hive> insert overwrite table test 

> partition (age)

> select id, name, tel, age

> from fefjay;

更可喜的是,hive還支援多表插入,什麼意思呢?在hive中,我們可以把insert語句倒過來,把from放在最前面,它的執行效果和放在後面是一樣的,如下:

hive> show create table test3; 

create table test3( id int, name string)

hive>from fefjay

>insert into table test

>partition(age)

>select id, name, tel,age

>insert into table test3

>select id,name

>where age > 25;

可以在同乙個查詢中使用多個insert子句,這樣的好處是我們只需要掃瞄一遍源表就可以生成多個不相交的輸出。

在實際情況中,表的輸出結果可能太多,不適於顯示在控制台上,這時候,將hive的查詢輸出結果直接存在乙個新的表中是非常方便的,我們稱這種情況為ctas(create table .. as select 首字母大寫)。

建立表的時候通過從別的表中查詢出相應的記錄並插入到所建立的表中。

如下:

hive>create table test4

>as

>select id,name,tel

>from fefjay;

資料就插入到test4表中去了,ctas操作是原子的,因此如果select查詢由於某種原因而失敗,新錶不會建立的!

windows資料匯入到hive表

需求說明 例項 邏輯 建資料庫。hive建立資料庫testdb create database if not exists testdb 建表。hive建立外部表test create external table if not exists test id string,tit string,ts...

sqoop匯入mysql表資料到HIVE

匯入mysql表資料到hive 將關係型資料的表結構複製到hive中 bin sqoop create hive table connect jdbc mysql node 1 3306 userdb table emp add username root password hadoop hive ...

hive匯入資料到hbase

hive有一張表user tag detail,表中資料約1.3億,需要將改表資料匯入到hbase 嘗試了兩種方式 建立關聯表 create table hbase user tag detail id string,name string 插入資料 insert overwrite table h...