pyhon 脚本实现批量日志信息导入外部表分区

来源:互联网 发布:手机淘宝要怎样开直播 编辑:程序博客网 时间:2024/05/26 20:24
#!/usr/bin/pythonimport osimport timedef 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 cmdif __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"))

0 0
原创粉丝点击