這裏總結一下項目中遇到的一些問題與使用記錄一下,做個總結,方便以後查閱。
1、首先查看對應表在數據庫中的表結構
這裏我就舉其中一個表爲例(GXTS_MJSKXX-門禁刷卡信息表):
2、在hive中創建對應的表結構,爲load數據做鋪墊(創建時按照日期與時間創建雙分區)
CREATE TABLE SJBZK.GXTS_MJSKXX_copy (
ID string,
YHM string,
XM string,
BM string,
SKSJ string,
SKDD string,
LKSJ string,
RQ string,
NF string,
YF string,
BZ string,
JCBZ string
)partitioned by (datet string,hours string);
這裏就要注意在創建表語句後面加上 partitioned by (***); 如果有兩個分區則中間用逗號隔開即可。
3、按照日期與時間分區load數據
在 https://blog.csdn.net/JJBOOM425/article/details/105534482 中我已將各個時間段的數據按照日期和小時import到hdfs中。可以在對應路徑找到當時import的數據:
然後就可以load數據,指令如下:
load data inpath "/user/jcxydb/GXTS_MJSKXX/${date_today}/${hour}" overwrite into table SJBZK.GXTS_MJSKXX_copy partition (datet='${date_today}',hours='${hour}')
注意:
1、因爲是雙分區的hive表,所以這裏要注意後面分區要加上 partition (datet='${date_today}',hours='${hour}') ,而我一開始寫的是 partition (datet=${date_today},hours=${hour}),會報以下錯誤:
Error while compiling statement: FAILED: ParseException line 1:122 mismatched input '-' expecting ) near '2020' in load statement
沒有單引號不會識別爲字符串,所以會報錯。
2、注意月份這裏如果是個位數的月份則記得帶上前面的0
會找不到具體的路徑,這個因爲使用sqoop導入時候使用 date_today=`date -d "today" +%Y-%m-%d` 獲取當前日期,所以也可以在shell中查看時間格式:
然後再測試就成功了。
可以看到數據以及加載進hive中了。這裏主要講解了從hdfs中將文件load進hive的步驟,並且按照日期與時間分區。