1. 程式人生 > >解決sqoop從mysql匯入到hive表的多分割槽問題

解決sqoop從mysql匯入到hive表的多分割槽問題

參考:http://blog.csdn.net/liweiwei71/article/details/23434189

對於分割槽表

drop table track_log;
create table track_log (
id                         string ,
url                        string ,
referer                    string ,
keyword                    string ,
type                       string ,
guid                       string ,
pageId                     string ,
moduleId                   string ,
linkId                     string ,
attachedInfo               string ,
sessionId                  string ,
trackerU                   string ,
trackerType                string ,
ip                         string ,
trackerSrc                 string ,
cookie                     string ,
orderCode                  string ,
trackTime                  string ,
endUserId                  string ,
firstLink                  string ,
sessionViewNo              string ,
productId                  string ,
curMerchantId              string ,
provinceId                 string ,
cityId                     string )  
PARTITIONED BY (ds string,hour string)  
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

不能再用--hive-table, --hive-partition-key, --hive-partition-value這三個引數。因為這樣只能向單個分割槽匯入資料,無法指定多個分割槽;其次,--where條件中沒有用and。

所以可以分兩步解決這個問題:

1)  首先確保目標分割槽中沒有資料,即//user/hive/warehouse/track_log/ds=20150827/hour=18目錄是空的; 2)  將資料直接上傳到指定的資料倉庫目錄中 sqoop import --connect  jdbc:mysql://localhost:3306/track_log --username root --password Nokia123  --table track_log18 

 -m 1  

--target-dir /user/hive/warehouse/track_log/ds=20150827/hour=18 

--fields-terminated-by '\t'  //這個也很重要,否則查詢一列時會返回多列

結果:

開始沒結果

hive> select id from track_log  limit 2;
OK
Time taken: 0.584 seconds

加入分割槽就好
hive> alter table track_log add partition(ds='20150828' ,hour='18') location '/user/hive/warehouse/track_log/ds=20150828/hour=18';


OK
Time taken: 0.759 seconds
hive> select id from track_log limit 2;
OK
121508281810000000
121508281810000001