基于python函数的udf日期处理函数
1、基于最近在学习python,就是试试用python进行一下的日期处理udf函数的输出,亲测可以上传去到hive中使用。后面开始学习使用python爬虫抓取黄色网页,和试试骗一下阅读量(笑)。最后,再去搞搞算法和机器学习。突然觉得自己搞得挺杂的。没办法,谁叫咱是码农呢?
2、使用办法-输入什么参数
这个py文件中包括一堆日期的计算函数。
其中包括
3、python上挂
1)、先把这堆文件上传到服务器。
2)、在使用的时候把python文件加载到缓存中
add file /home/hive/zyp/python/date.py
3)、最后在hive执行的时候,调用这个py函数即可
SELECT TRANSFORM ('n_day,20171003,3') USING 'python date.py';
4、逻辑分享
1)、python udf嵌套
原本是写成每个日期函数都是一个的,但是后来发现python在本地调用的时候是可以的,但是传上服务器之后就不好使了。这个也是确实的,谁叫别人java的udf都是封装好,需要注册的呢?
后来想想反正这函数也是写好在这里的需要调用才会用到的,于是干脆把所有函数都打包在一起,通过一个参数来调用了。
具体实现办法:
def func(argument,date_p,offset=1,tail='begin'):
try:
switcher={
'week_begin':week_begin(str(date_p),week_num(str(date_p))),
'week_end':week_end(str(date_p),week_num(str(date_p))),
'week_num':week_num(str(date_p)),
'month_begin':month_begin(str(date_p)),
'month_end':month_end(str(date_p)),
'quarter_begin':quarter_begin(str(date_p)),
'quarter_end':quarter_end(str(date_p)),
'n_day':n_day(str(date_p),offset),
'n_week':n_week(str(date_p),int(offset)*7),
'n_month':n_month(str(date_p),offset,tail),
'n_quarter':n_quarter(str(date_p),offset,tail),
}
func=switcher.get(argument)
return func
except:
return None
2)、参数补全
把东西都打包到一起之后就又发现问题了,有写函数不需要这么多参数啊。一旦你执行这些不需要那么多参数的函数的时候。硬硬的去取参数会导致报错的。
所以试了挺久,想到一个解决办法,不知道是不是最好实现了。办法就是给他补字段呗。
代码如下:
tempList = ['1', 'begin']
for line in sys.stdin:
day_p = line.strip().split(',')
if day_p[1][4]=='-':
date_p=(day_p[1][0:4])+(day_p[1][5:7])+(day_p[1][8:11])
else:
date_p=day_p[1]
day_p.extend(tempList[-int(4-len(day_p)%4):])
day_list=day_p[0:4]
print func(str(day_list[0]),date_p,day_list[2],day_list[3])
中间还有一段处理日期带杠的代码。
3)、程序源码
#!/home/tops/bin/python
import calendar
import math
import sys
import datetime
#test=['n_day,20171003,3','week_begin,2017-10-05','week_num,2017-01-23','month_begin,2017-12-24','month_end,2017-09-30','quarter_begin,2017-10-05,5,begin','quarter_end,2017-01-23,-7,begin','n_day,20171224,8,begin','n_week,2017-09-30,3,begin','n_month,2017-10-05,5,begin','n_quarter,2017-01-23,-7,begin']
def week_end(day_p,week_num):
try:
return n_day(str(day_p),(7-int(week_num)))
except:
return None
def week_begin(day_p,week_num):
try:
return n_day(str(day_p),-(int(week_num)%7))
except:
return None
def week_num(day_p):
try:
day_str = day_p[0:4]+'-'+day_p[4:6]+'-'+day_p[6:8]
day_before = datetime.datetime.strptime(day_str, '%Y-%m-%d')
return day_before.weekday()+1
except:
return None
def month_begin(day_p):
try:
return day_p[0:4]+'-'+day_p[4:6]+'-'+'01'
except:
return None
def month_end(day_p):
try:
monthRange = calendar.monthrange(int(day_p[0:4]),int(day_p[4:6]))
return day_p[0:4]+'-'+day_p[4:6]+'-'+str(monthRange[1])
except:
return None
def quarter_begin(day_p):
try:
Quarter_begin='0'+str(int(int(day_p[4:6])/3.1)*3+1)
return str(day_p[0:4])+'-'+str(Quarter_begin[-2:])+'-'+'01'
except:
return None
def quarter_end(day_p):
try:
Quarter_end='0'+str((int(int(day_p[4:6])/3.1)+1)*3)
return month_end(str(day_p[0:4])+str(Quarter_end[-2:])+str('01'))
except:
return None
def n_day(day_p,offset):
try:
day_str = day_p[0:4]+'-'+day_p[4:6]+'-'+day_p[6:8]
day_before = datetime.datetime.strptime(day_str, '%Y-%m-%d')
day_after = datetime.timedelta(days=int(offset))
n_days = day_before + day_after
return n_days.strftime('%Y-%m-%d')
except:
return None
def n_week(day_p,offset):
try:
date_p=week_begin(day_p,week_num(day_p))
date_p1=str(date_p[0:4]+date_p[5:7]+date_p[8:11])
return n_day(str(date_p1),int(offset)*7)
except:
return None
def n_month(day_p,offset,tail):
try:
year_m=int(day_p[0:4])+(int(offset)/12)
month_m='0'+str(int((int(day_p[4:6])+int(offset))%12))
if month_m=='00':month_m=12
if tail=='begin':
day_m='01'
else:
monthRange=calendar.monthrange(int(year_m),int(month_m))
day_m=str(monthRange[1])
return str(year_m)+'-'+str(month_m)[-2:]+'-'+str(day_m)
except:
return None
def n_quarter(day_p,offset,tail):
try:
date_p=quarter_begin(day_p)
return n_month(date_p,int(offset)*3,tail)
except:
return None
def func(argument,date_p,offset=1,tail='begin'):
try:
switcher={
'week_begin':week_begin(str(date_p),week_num(str(date_p))),
'week_end':week_end(str(date_p),week_num(str(date_p))),
'week_num':week_num(str(date_p)),
'month_begin':month_begin(str(date_p)),
'month_end':month_end(str(date_p)),
'quarter_begin':quarter_begin(str(date_p)),
'quarter_end':quarter_end(str(date_p)),
'n_day':n_day(str(date_p),offset),
'n_week':n_week(str(date_p),int(offset)*7),
'n_month':n_month(str(date_p),offset,tail),
'n_quarter':n_quarter(str(date_p),offset,tail),
}
func=switcher.get(argument)
return func
except:
return None
def main():
try:
tempList = ['1', 'begin']
for line in sys.stdin:
day_p = line.strip().split(',')
if day_p[1][4]=='-':
date_p=(day_p[1][0:4])+(day_p[1][5:7])+(day_p[1][8:11])
else:
date_p=day_p[1]
day_p.extend(tempList[-int(4-len(day_p)%4):])
day_list=day_p[0:4]
print func(str(day_list[0]),date_p,day_list[2],day_list[3])
except:
return None
if __name__ == "__main__":
main()
'''
作者:张宇鹏
简书:http://www.jianshu.com/p/5c70d4ade0df
博客园:http://www.cnblogs.com/Yuppy-Lotr/
欢迎转载使用。不要改作者名就行。
'''