按照日期与时间创建双分区hive表,再通过日期与时间分区将hdfs中的文件load进hive中

这里总结一下项目中遇到的一些问题与使用记录一下,做个总结,方便以后查阅。

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的步骤,并且按照日期与时间分区。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章