轉載:https://blog.csdn.net/qq_26442553/article/details/80380590
轉載:https://blog.csdn.net/weixin_43681796/article/details/106537339
https://blog.csdn.net/hao495430759/article/details/80529456
1、load data 導入數據到hive中
1.將本地數據文件導入到hive非分區表中,如下文件可以是個目錄,會導入目錄中所有的文件
load data local inpath '/home/robot/'
overwrite into table fdm_sor.personinfo
2.將本地數據文件導入到hive分區表中
load data local inpath '/home/robot/'
overwrite into table fdm_sor.personinfo
partition(country='china',city='nanjing')
注意文件格式需要跟建表時指定的一致
3、如果文件是在hdfs里的,去掉local即可
注意:
1.inpath里只要填目錄即可,不用具體到文件,會加載目錄下所有問題,但該目錄下不能再有子目錄,否則報錯。
2.overwrite 可以不加,加的話會將表中所有數據覆蓋掉(分區表只覆蓋當前分區數據),into talbe 將數據追加到表中。
3.into talbe 如果表里數據已經存在了,會再次到導入,底層文件存儲會給同文件名加序列號然后存儲。
3.將分布式文件系統上的數據導入的hive中,比如講hdfs上數據導入到hive中
load data inpath '/user/robot/'
overwrite into table fdm_sor.personinfo
注意:去掉local,則默認的路徑是分布式文件系統上的路徑,如hdfs上的路徑。
2、從本地
2、通過查詢insert ....select的形式往hive中導入數據
1.通過查詢將數據覆蓋導入的分區表中(或者用into追加結果,往動態分區表中插入數據,請參考本系列其他博客。)
insert overwrite table fdm_sor.personinfo
partition(statis_date='${staits_date}'
select a.id,a.name,b.address
from person a left join address b
on a.id = b.id
2.多次插入,從一張表中讀數據,下面這種方式效率最高,只需要掃描一次表即可。注意中間沒有分號;
from T_DEDUCT_SIGN_D_external t
insert into table t1
select 123 ,sign_no string,null
insert into table t2
select 345 ,null ,bp_no string
insert into table t3
select 678 ,sign_no string,bp_no string
where t.statis_date = '20180101';
可能出現的錯誤以及解決
問題:
記錄一次Hive導入數據找不到文件的錯誤
load data local inpath '/data/test/ftp/test.txt' into table test;
FAILED: SemanticException Line 1:23 Invalid path ''/data/test/ftp/test.txt'': No files matching path file:/data/test/ftp/test.txt
報錯找不到文件,確定當前機器確實存在這個文件。
環境:實存在這個文件。在Hive的cli端執行load語句,加載測試文件;Hive版本2.3.7,Hadoop版本2.x
問題分析:可以參考這篇博客https://blog.csdn.net/hao495430759/article/details/80529456
When using the JDBC driver, the command executes on the HiveServer2 side. The file is evaluated to locally exist on the server, which is not true in your case (it exists on the local client program machine). Try instead to load the file to HDFS first, and use a HDFS URI in the LOAD DATA statement to make the server find it.
從上面的解釋可知, hive導入數據語句 load data [local] inpath ,是一個服務器端的指令,它是在服務器端執行。因此指定local時表明加載的文件為本地文件,但是這里的local,在hive中指的是 hiveserver 服務所在的機器,而不是hivecli 或 beeline客戶端所在的機器(生產環境大都是 hiveserver 和 hivecli不在同一個機器)。
問題解決:
1)將文件上傳到metastore服務端,在metastore服務端進行操作
將文件上傳到metastore服務端
scp /data/test/ftp/test.txt node003:/data/test/ftp/
登陸metastore服務端
ssh node003
登陸beeline后執行load語句
beeline -u jdbc:hive2://node003:10000 -n root
load data local inpath '/data/test/ftp/test.txt' into table test;
查詢結果
select * from test;
2)將文件上傳到hdfs,通過load進行加載
重新打開一個shell窗口,上產物文件到HDFS
hdfs dfs -put /data/test/ftp/test.txt /data/test/RAW/
在已經登陸beeline的窗口加載數據
load data inpath '/data/test/RAW/test.txt' into table test;
查看結果
select * from test;