Created
September 29, 2014 05:39
-
-
Save jgmize/0229567331b6bd7cc2af to your computer and use it in GitHub Desktop.
correlated locale redirects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
import mrjob.job | |
import mrjob.protocol | |
IP = 1 | |
DOMAIN = 2 | |
DATETIME = 4 | |
METHOD = 5 | |
URL = 6 | |
STATUS_CODE = 7 | |
UA = 10 | |
LOCALE_REGEX = re.compile(r'/[a-z]{2,3}(-[a-zA-Z]{2})?/') | |
class WeblogJob(mrjob.job.MRJob): | |
OUTPUT_PROTOCOL = mrjob.protocol.RawValueProtocol | |
HADOOP_INPUT_FORMAT = ( | |
'org.apache.hadoop.mapred.SequenceFileAsTextInputFormat') | |
def mapper(self, _, line): | |
try: | |
fields = line.split('\t') | |
if (fields[DOMAIN] == 'www.mozilla.org' and | |
fields[METHOD] == 'GET' and | |
fields[STATUS_CODE] == '301'): | |
yield 'redirects', 1 | |
if LOCALE_REGEX.match(fields[URL]): | |
yield 'locale-redirects', 1 | |
else: | |
yield 'non-locale-redirects', 1 | |
except: | |
self.increment_counter("errors", "all") | |
def combiner(self, key, vals): | |
yield key, sum(vals) | |
def reducer(self, key, vals): | |
yield '', '%s\t%d' % (key, sum(vals)) | |
if __name__ == '__main__': | |
WeblogJob.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment