這里介紹四種: (1)、從本地文件系統中導入數據到Hive表; (2)、從HDFS上導入數據到Hive表; (3)、從別的表中查詢出相應的數據并導入到Hive表中; (4)、在創建表的時候通過從別的表中查詢出相應的記錄并插入到所創建的表中。 一、從本地文件系統中導入數據到Hive表
先在Hive里面創建好表,如下: - hive> create table wyp
- ? ? > (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/wyp/wyp.txt文件,內容如下: - [wyp@master ~]$ cat wyp.txt
- 1? ?? ? wyp? ???25? ?? ?13188888888888
- 2? ?? ? test? ? 30? ?? ?13888888888888
- 3? ?? ? zs? ?? ?34? ?? ?899314121
復制代碼 wyp.txt文件中的數據列之間是使用\t分割的,可以通過下面的語句將這個文件里面的數據導入到wyp表里面,操作如下: - hive> load data local inpath 'wyp.txt' into table wyp;
- Copying data from file:/home/wyp/wyp.txt
- Copying file: file:/home/wyp/wyp.txt
- Loading data to table default.wyp
- Table default.wyp stats:
- [num_partitions: 0, num_files: 1, num_rows: 0, total_size: 67]
- OK
- Time taken: 5.967 seconds
復制代碼 這樣就將wyp.txt里面的內容導入到wyp表里面去了,可以到wyp表的數據目錄下查看,如下命令: - hive> dfs -ls /user/hive/warehouse/wyp ;
- Found 1 items
- -rw-r--r--3 wyp supergroup 67 2014-02-19 18:23 /hive/warehouse/wyp/wyp.txt
復制代碼 需要注意的是: 和我們熟悉的關系型數據庫不一樣,Hive現在還不支持在insert語句里面直接給出一組記錄的文字形式,也就是說,Hive并不支持INSERT INTO …. VALUES形式的語句。 二、HDFS上導入數據到Hive表
從本地文件系統中將數據導入到Hive表的過程中,其實是先將數據臨時復制到HDFS的一個目錄下(典型的情況是復制到上傳用戶的HDFS home目錄下,比如/home/wyp/),然后再將數據從那個臨時目錄下移動(注意,這里說的是移動,不是復制!)到對應的Hive表的數據目錄里面。既然如此,那么Hive肯定支持將數據直接從HDFS上的一個目錄移動到相應Hive表的數據目錄下,假設有下面這個文件/home/wyp/add.txt,具體的操作如下: - [wyp@master /home/q/hadoop-2.2.0]$ bin/hadoop fs -cat /home/wyp/add.txt
- 5? ?? ? wyp1? ? 23? ?? ?131212121212
- 6? ?? ? wyp2? ? 24? ?? ?134535353535
- 7? ?? ? wyp3? ? 25? ?? ?132453535353
- 8? ?? ? wyp4? ? 26? ?? ?154243434355
復制代碼 上面是需要插入數據的內容,這個文件是存放在HDFS上/home/wyp目錄(和一中提到的不同,一中提到的文件是存放在本地文件系統上)里面,我們可以通過下面的命令將這個文件里面的內容導入到Hive表中,具體操作如下: - hive> load data inpath '/home/wyp/add.txt' into table wyp;
- Loading data to table default.wyp
- Table default.wyp stats:
- [num_partitions: 0, num_files: 2, num_rows: 0, total_size: 215]
- OK
- Time taken: 0.47 seconds
-
- hive> select * from wyp;
- OK
- 5? ?? ? wyp1? ? 23? ?? ?131212121212
- 6? ?? ? wyp2? ? 24? ?? ?134535353535
- 7? ?? ? wyp3? ? 25? ?? ?132453535353
- 8? ?? ? wyp4? ? 26? ?? ?154243434355
- 1? ?? ? wyp? ???25? ?? ?13188888888888
- 2? ?? ? test? ? 30? ?? ?13888888888888
- 3? ?? ? zs? ?? ?34? ?? ?899314121
- Time taken: 0.096 seconds, Fetched: 7 row(s)
復制代碼 從上面的執行結果我們可以看到,數據的確導入到wyp表中了!請注意load data inpath ‘/home/wyp/add.txt’ into table wyp;里面是沒有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;
- OK
- Time taken: 0.261 seconds
復制代碼 大體和wyp表的建表語句類似,只不過test表里面用age作為了分區字段。對于分區,這里在做解釋一下: 分區:在Hive中,表的每一個分區對應表下的相應目錄,所有分區的數據都是存儲在對應的目錄中。比如wyp表有dt和city兩個分區,則對應dt=20131218,city=BJ對應表的目錄為/user/hive/warehouse/dt=20131218/city=BJ,所有屬于這個分區的數據都存放在這個目錄中。
下面語句就是將wyp表中的查詢結果并插入到test表中: - hive> insert into table test
- ? ? > partition (age='25')
- ? ? > select id, name, tel
- ? ? > from wyp;
- #####################################################################
- ? ?? ?? ???這里輸出了一堆Mapreduce任務信息,這里省略
- #####################################################################
- Total MapReduce CPU Time Spent: 1 seconds 310 msec
- OK
- Time taken: 19.125 seconds
-
- hive> select * from test;
- OK
- 5? ?? ? wyp1? ? 131212121212? ? 25
- 6? ?? ? wyp2? ? 134535353535? ? 25
- 7? ?? ? wyp3? ? 132453535353? ? 25
- 8? ?? ? wyp4? ? 154243434355? ? 25
- 1? ?? ? wyp? ???13188888888888??25
- 2? ?? ? test? ? 13888888888888??25
- 3? ?? ? zs? ?? ?899314121? ?? ? 25
- Time taken: 0.126 seconds, Fetched: 7 row(s)
復制代碼 這里做一下說明:
我們知道我們傳統數據塊的形式insert into table values(字段1,字段2),這種形式hive是不支持的。
通過上面的輸出,我們可以看到從wyp表中查詢出來的東西已經成功插入到test表中去了!如果目標表(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 wyp;
- #####################################################################
- ? ?? ?? ???這里輸出了一堆Mapreduce任務信息,這里省略
- #####################################################################
- Total MapReduce CPU Time Spent: 1 seconds 510 msec
- OK
- Time taken: 17.712 seconds
-
-
- hive> select * from test;
- OK
- 5? ?? ? wyp1? ? 131212121212? ? 23
- 6? ?? ? wyp2? ? 134535353535? ? 24
- 7? ?? ? wyp3? ? 132453535353? ? 25
- 1? ?? ? wyp? ???13188888888888??25
- 8? ?? ? wyp4? ? 154243434355? ? 26
- 2? ?? ? test? ? 13888888888888??30
- 3? ?? ? zs? ?? ?899314121? ?? ? 34
- Time taken: 0.399 seconds, Fetched: 7 row(s)
復制代碼 這種方法叫做動態分區插入,但是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 wyp;
復制代碼 更可喜的是,Hive還支持多表插入,什么意思呢?在Hive中,我們可以把insert語句倒過來,把from放在最前面,它的執行效果和放在后面是一樣的,如下: - hive> show create table test3;
- OK
- CREATE??TABLE test3(
- ??id int,
- ??name string)
- Time taken: 0.277 seconds, Fetched: 18 row(s)
-
- hive> from wyp
- ? ? > insert into table test
- ? ? > partition(age)
- ? ? > select id, name, tel, age
- ? ? > insert into table test3
- ? ? > select id, name
- ? ? > where age>25;
-
- hive> select * from test3;
- OK
- 8? ?? ? wyp4
- 2? ?? ? test
- 3? ?? ? zs
- Time taken: 4.308 seconds, Fetched: 3 row(s)
復制代碼 可以在同一個查詢中使用多個insert子句,這樣的好處是我們只需要掃描一遍源表就可以生成多個不相交的輸出。這個很酷吧! 四、在創建表的時候通過從別的表中查詢出相應的記錄并插入到所創建的表中
在實際情況中,表的輸出結果可能太多,不適于顯示在控制臺上,這時候,將Hive的查詢輸出結果直接存在一個新的表中是非常方便的,我們稱這種情況為CTAS(create table .. as select)如下: - hive> create table test4
- ? ? > as
- ? ? > select id, name, tel
- ? ? > from wyp;
-
- hive> select * from test4;
- OK
- 5? ?? ? wyp1? ? 131212121212
- 6? ?? ? wyp2? ? 134535353535
- 7? ?? ? wyp3? ? 132453535353
- 8? ?? ? wyp4? ? 154243434355
- 1? ?? ? wyp? ???13188888888888
- 2? ?? ? test? ? 13888888888888
- 3? ?? ? zs? ?? ?899314121
- Time taken: 0.089 seconds, Fetched: 7 row(s)
復制代碼 數據就插入到test4表中去了,CTAS操作是原子的,因此如果select查詢由于某種原因而失敗,新表是不會創建的!