在Hive中建好表之后及汉,需要將數(shù)據(jù)加載進(jìn)來钢悲,以便做后續(xù)查詢分析陶缺,本文介紹向Hive表中加載數(shù)據(jù)的幾種方式钾挟。
6.1 建表時候直接指定
如果你的數(shù)據(jù)已經(jīng)在HDFS上存在,已經(jīng)為結(jié)構(gòu)化數(shù)據(jù)饱岸,并且數(shù)據(jù)所在的HDFS路徑不需要維護(hù)掺出,那么可以直接在建表的時候使用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;">
- CREATE [EXTERNAL] TABLE t_lxw1234 (
- day STRING,
- url STRING)
- ROW FORMAT DELIMITED
- FIELDS TERMINATED BY ' '
- stored as textfile
- location '/tmp/lxw1234/';
</pre>
這里內(nèi)部表和外部表都可以指定苫费,但需要注意汤锨,如果是內(nèi)部表,那么在DROP該表的時候黍衙,同時會將LOCATION所指定的目錄一起刪除。
6.2 從本地文件系統(tǒng)或者HDFS的一個目錄中加載
如果數(shù)據(jù)在本地荠诬,或者HDFS的某一個目錄下琅翻,需要加載到目標(biāo)中或分區(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é)果插入到目標(biāo)表或分區(qū)中:
INSERT overwrite TABLE t_lxw1234 PARTITION (day = ‘2015-06-15’)
SELECT day,url from source_table;
6.4 導(dǎo)出Hive中的數(shù)據(jù)到文件系統(tǒng)
這里也介紹一下從Hive中導(dǎo)出數(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;">
- INSERT OVERWRITE [LOCAL] DIRECTORY directory1
- [ROW FORMAT row_format] [STORED AS file_format]
- SELECT ... FROM ...
</pre>
如果指定了LOCAL關(guān)鍵字,則為導(dǎo)出到本地文件系統(tǒng)钧嘶,否則棠众,導(dǎo)出到HDFS。
使用ROW FORMAT關(guān)鍵字可以指定導(dǎo)出的文件分隔符有决,比如:
<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;">
- INSERT OVERWRITE LOCAL DIRECTORY '/tmp/lxw1234/'
- ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
- SELECT * FROM t_lxw1234;
</pre>
該語句將t_lxw1234表的所有數(shù)據(jù)導(dǎo)出到本地文件系統(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ù)加載和導(dǎo)出的介紹书幕,請參考官方文檔:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML#LanguageManualDML-Loadingfilesintotables
Hive相關(guān)文章(持續(xù)更新):
—-Hive中的數(shù)據(jù)庫(Database)和表(Table)
hive優(yōu)化之——控制hive任務(wù)中的map數(shù)和reduce數(shù)
如果覺得本博客對您有幫助,請 贊助作者 台汇。
轉(zhuǎn)載請注明:lxw的大數(shù)據(jù)田地 ? [一起學(xué)Hive]之七-向Hive表中加載數(shù)據(jù)