1、基於最近在學習python,就是試試用python進行一下的日期處理udf函數的輸出,親測可以上傳去到hive中使用。後面開始學習使用python爬蟲抓取黃色網頁,和試試騙一下閱讀量(笑)。最後,再去搞搞演算法和機器學習。突然覺得自己搞得挺雜的。沒辦法,誰叫咱是碼農呢?
原本是寫成每個日期函數都是一個的,但是後來發現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
#!/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/
歡迎轉載使用。不要改作者名就行。
‘‘‘