python日期函数udf-程序分享

基于python函数的udf日期处理函数

1、基于最近在学习python,就是试试用python进行一下的日期处理udf函数的输出,亲测可以上传去到hive中使用。后面开始学习使用python爬虫抓取黄色网页,和试试骗一下阅读量(笑)。最后,再去搞搞算法和机器学习。突然觉得自己搞得挺杂的。没办法,谁叫咱是码农呢?

2、使用办法-输入什么参数

这个py文件中包括一堆日期的计算函数。

其中包括

函数名 实现逻辑 使用说明 传入参数 example
week_begin 先获得本周是周几,再用7减周几,得出本日需要偏移的差值,再用偏移日期函数即可得出。 输入日期得出该日期所在周的周一日期 日期(date) '20171031' or '2017-10-31'
week_end 先获得本周是周几,然后用周几除以7的余数,得出向后的偏移量,再用偏移日期函数即可得出 输入日期得出该日期所在周的周末日期 日期(date) '20171031' or '2017-10-31'
week_num 利用python的内置日期函数得出 输入日期得出该日期是周几 日期(date) '20171031' or '2017-10-31'
month_begin 截取年月字段保留起来,把日期改成01再组合起来 输入日期得出该日期所在月份的一号日期 日期(date) '20171031' or '2017-10-31'
month_end 截取年月字段保留起来,月末日期用python内置函数生成,再组合起来 输入日期得出该日期所在月份的月末日期 日期(date) '20171031' or '2017-10-31'
quarter_begin 主要是处理月份的问题,用当前月份除以3.1再取整,得出当前是第n-1季,然后再*3(季度的偏移量) 输入日期得出该日期所在季度的季度初日期 日期(date) '20171031' or '2017-10-31'
quarter_end 原理同quarter_begin在第几季度那里做手脚 输入日期得出该日期所在季度的季度末日期 日期(date) '20171031' or '2017-10-31'
n_day 把字符串转换成日期格式,然后相加 输入日期及偏移量得出该日期加偏移量后的日期 日期(date),偏移量(offset) '20171031,3' or '2017-10-31,3'
n_week 先调用WEEK_BEGIN函数计算本周周一的函数,然后把再偏移量*7并调用N_DAY函数 输入日期及偏移量得出该日期偏移n周后的数据(得出的是周一) 日期(date),偏移量(offset) '20171031,3' or '2017-10-31,3'
n_month 截取年分除以12,截取月份除以12的余数计算偏移量,最后再处理天 输入日期及偏移量得出该日期偏移n月后的日期 日期(date),偏移量(offset),月末或月头(tail) '20171031,3,begin' or '2017-10-31,3,end'
n_quarter 调用quarter_begin计算季度的初始日期,然后再调用n_month*3计算偏移量 输入日期得出该日期偏移n个季度后的日期 日期(date),偏移量(offset),月末或月头(tail) '20171031,3,begin' or '2017-10-31,3,end'
         

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/

欢迎转载使用。不要改作者名就行。

'''

 

 

 

posted @ 2017-10-31 19:09  Yuppy在学习的路上  阅读(1344)  评论(0编辑  收藏  举报