#!/usr/bin/python
import os
import time
def cmdmk(dir,year,month,day):
'''创建根据年月日导入分区的命令'''
path=dir+'%04d'%year+'/%02d'%month+'/%02d'%day+'/'
cmd="ALTER TABLE gntable ADD IF NOT EXISTS PARTITION(year="+'"'+'%04d'%year+'"'+",month="+'"'+'%02d'%month+'"'+",day="+'"'+'%02d'%day+'"'+") LOCATION "+'"'+path+'";'
return cmd
if __name__ == '__main__':
year=2014;
days=[31,28,31,30,31,30,31,31,30,31,30,31]
fo = open("hive_temp.q", "w")
fo.write(("use gnhive;"+"\n").encode())
#循环的范式产生批量分区数据导入的命令
for month in range(1,13):
for day in range(1,days[month-1]+1):
hive_cmd=cmdmk("/user/Gn/",year,month,day)
print(hive_cmd)
fo.write((hive_cmd+"\n").encode())
fo.close
#执行分区导入命令
print(os.system("hive -f hive_temp.q"))