Skip to content

Instantly share code, notes, and snippets.

@bepcyc
Last active January 19, 2017 13:32
Show Gist options
  • Save bepcyc/615b6fcd35f6ff10f14e26eb5bfcc2f6 to your computer and use it in GitHub Desktop.
Save bepcyc/615b6fcd35f6ff10f14e26eb5bfcc2f6 to your computer and use it in GitHub Desktop.
When you need the partitions.. for CONCATENATE in Hive or other operations
import datetime
today = datetime.datetime.today()
# redefine parameters
days_delta = 365
month_partiton = 'month'
day_partition = 'day'
date_range = [today - datetime.timedelta(days=x) for x in range(0, days_delta)]
# pairs of ('YYYYMM', 'DD')
partitions = [(str(d.year*100+d.month), str(100+d.day)[1:]) for d in date_range ]
for m, d in partitions:
print 'month={0}/day={1}'.format(m, d)
# print "PARTITION (month='{0}', day='{1}')".format(m, d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment