1、從linux fs和hdfs中加載
load data [local] inpath 'path' [overwrite] into table tblName [partition_sepc];
[local]:如果加上表示本地地址,如果沒有表示HDFS上的地址。 [overwrite]:如果加上表示覆蓋之前的數(shù)據(jù),如果沒有表示追加之前的數(shù)據(jù)。 [partition_sepc]:如果加上表示加載進(jìn)相應(yīng)的分區(qū)。 ? 2、從其他表中裝載 insert table tblName select columns... from otherTblName;
關(guān)于overwrite的含義和上述是一致的。 需要注意的是: tblName表中的字段要和后面select查詢的字段要保持一致?。?! eg:hive> from history
> insert overwrite sales select * where dt=’2016-01-16’
> insert overwrite credits select * where dt=’2016-01-17’;
? 3、通過動(dòng)態(tài)分區(qū)裝載數(shù)據(jù)【以下字段要搞清楚】 INSERT overwrite TABLE t3 PARTITION(province='gs', city)?
SELECT t.province, t.city FROM temp t WHERE t.province='gs';
注意:靜態(tài)分區(qū)必須出現(xiàn)在動(dòng)態(tài)分區(qū)之前!如果要都用動(dòng)態(tài)分區(qū)需要開啟動(dòng)態(tài)分區(qū)的支持。 sethive.exec.dynamic.partition=true; sethive.exec.dynamic.partition.mode=nonstrict; sethive.exec.max.dynamic.partitions=1000; 在創(chuàng)建分區(qū)的時(shí)候,最好不要?jiǎng)?chuàng)建過多的分區(qū),如果分區(qū)過多的話,查詢也是非常的慢的,就像在window下一個(gè)文件夾下面的文件過多會(huì)對(duì)我們的使用造成非常的不便的。 ? hive能支持多大的分區(qū)數(shù)呢,可以使用命令sethive.exec.max.dynamic.partitions獲取 在這里可能會(huì)遇到的問題: 1)Dynamic partition cannot be the parentof a static partition 'year'
原因:加載數(shù)據(jù)的時(shí)候和分區(qū)的順序有關(guān)聯(lián),動(dòng)態(tài)分區(qū)不能作為靜態(tài)的父目錄 2)Cannot insert into target tablebecause column number/types are different 'school': Tableinsclause-0 has 3 columns, but query has 4 columns.
原因:兩張表的結(jié)構(gòu)是相似的,插入的時(shí)候字段發(fā)生沖突了,需要加表的別名來(lái)區(qū)分。 ? 4、創(chuàng)建表的同時(shí)裝載數(shù)據(jù) 和之前創(chuàng)建視圖的時(shí)候的語(yǔ)法非常相似,把view改成table就可以 eg:create table newTblName as select columns from other_tblName;
? 5、使用Import命令導(dǎo)入hdfs上的數(shù)據(jù) eg:hive> import table tblName from 'hdfs_uri';
? 6、同一份數(shù)據(jù)多種處理 hive有一個(gè)特性是,hive中可以對(duì)同一份數(shù)據(jù)進(jìn)行多種處理。Hive本身提供了一個(gè)獨(dú)特的語(yǔ)法,她可以從一個(gè)數(shù)據(jù)源產(chǎn)生多個(gè)數(shù)據(jù)聚合,而無(wú)需每次聚合都要重新掃描一次。對(duì)于大的數(shù)據(jù)輸入集來(lái)說,這個(gè)優(yōu)化可以節(jié)約非常可觀的時(shí)間。 eg:hive> from history
> insert overwrite sales select * where dt=’2016-01-16’
> insert overwrite credits select * where dt=’2016-01-17’;
? 7、Hive和MySQL之間利用sqoop的數(shù)據(jù)傳輸 查看mysql 中的數(shù)據(jù)庫(kù):
sqoop list-databases --connect jdbc:mysql://localhost:3306 --username root --password root
? 一、MySQL向Hive進(jìn)行數(shù)據(jù)遷移 1°、Sqoop 在 hive 上創(chuàng)建與 mysql 相同的表結(jié)構(gòu)
sqoop create-hive-table --connect jdbc:mysql:///test --table test --username root --password root --hive-table mydb1.test
? 2°、將 mysql 中的數(shù)據(jù)導(dǎo)入到 hive 中
sqoop import --connect jdbc:mysql:///test --table test --username root --password root --hive-import --hive-table mydb1.test
? 二、Hive 向 向 MySQL
sqoop export --connect jdbc:mysql:///test --username root --password root --table test1 --export-dir /user/hive/warehouse/mydb1.db/test --input-fields-terminated-by '\t'