[一起學Hive]之七-向Hive表中加載數(shù)據(jù)

在Hive中建好表之后,需要將數(shù)據(jù)加載進來,以便做后續(xù)查詢分析,本文介紹向Hive表中加載數(shù)據(jù)的幾種方式。

6.1 建表時候直接指定

如果你的數(shù)據(jù)已經(jīng)在HDFS上存在,已經(jīng)為結(jié)構(gòu)化數(shù)據(jù),并且數(shù)據(jù)所在的HDFS路徑不需要維護,那么可以直接在建表的時候使用location指定數(shù)據(jù)所在的HDFS路徑即可。

比如:

<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. CREATE [EXTERNAL] TABLE t_lxw1234 (
  2. day STRING,
  3. url STRING)
  4. ROW FORMAT DELIMITED
  5. FIELDS TERMINATED BY ' '
  6. stored as textfile
  7. location '/tmp/lxw1234/';

</pre>

這里內(nèi)部表和外部表都可以指定,但需要注意,如果是內(nèi)部表,那么在DROP該表的時候,同時會將LOCATION所指定的目錄一起刪除。

6.2 從本地文件系統(tǒng)或者HDFS的一個目錄中加載

如果數(shù)據(jù)在本地,或者HDFS的某一個目錄下,需要加載到目標中或分區(qū)中,那么使用LOAD DATA命令即可加載數(shù)據(jù):

  • 加載本地文件

LOAD DATA LOCAL INPATH ‘/home/lxw1234/t_lxw1234/’

INTO TABLE t_lxw1234 PARTITION (day = ‘2015-06-15’);

  • 加載HDFS文件

LOAD DATA INPATH ‘/user/lxw1234/t_lxw1234/’

INTO TABLE t_lxw1234 PARTITION (day = ‘2015-06-15’);

6.3 從一個子查詢中加載數(shù)據(jù)

這個比較簡單,就是將一個查詢結(jié)果插入到目標表或分區(qū)中:

INSERT overwrite TABLE t_lxw1234 PARTITION (day = ‘2015-06-15’)

SELECT day,url from source_table;

6.4 導出Hive中的數(shù)據(jù)到文件系統(tǒng)

這里也介紹一下從Hive中導出數(shù)據(jù)到文件系統(tǒng)(HDFS和本地文件系統(tǒng))。

語法為:

<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. INSERT OVERWRITE [LOCAL] DIRECTORY directory1
  2. [ROW FORMAT row_format] [STORED AS file_format]
  3. SELECT ... FROM ...

</pre>

如果指定了LOCAL關(guān)鍵字,則為導出到本地文件系統(tǒng),否則,導出到HDFS。
使用ROW FORMAT關(guān)鍵字可以指定導出的文件分隔符,比如:

<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. INSERT OVERWRITE LOCAL DIRECTORY '/tmp/lxw1234/'
  2. ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
  3. SELECT * FROM t_lxw1234;

</pre>

該語句將t_lxw1234表的所有數(shù)據(jù)導出到本地文件系統(tǒng)/tmp/lxw1234/目錄,字段間的分隔符為逗號。

cat /tmp/lxw1234/000000_0
2015-05-10,url1
2015-05-10,url2
2015-06-14,url1
2015-06-14,url2
2015-06-15,url1
2015-06-15,url2

更多關(guān)于Hive數(shù)據(jù)加載和導出的介紹,請參考官方文檔:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML#LanguageManualDML-Loadingfilesintotables

Hive相關(guān)文章(持續(xù)更新)

一起學Hive系列

—-Hive概述,Hive是什么

—-Hive函數(shù)大全-完整版

—-Hive中的數(shù)據(jù)庫(Database)和表(Table)

—-Hive的安裝配置

—-Hive的視圖和分區(qū)

—-Hive的動態(tài)分區(qū)

Hive分析函數(shù)系列

Hive索引

hive優(yōu)化之——控制hive任務中的map數(shù)和reduce數(shù)

如果覺得本博客對您有幫助,請 贊助作者 。

轉(zhuǎn)載請注明:lxw的大數(shù)據(jù)田地 ? [一起學Hive]之七-向Hive表中加載數(shù)據(jù)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容