-
Notifications
You must be signed in to change notification settings - Fork 4
/
finalRecordExtraction.py
46 lines (33 loc) · 1.28 KB
/
finalRecordExtraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from mrjob.job import MRJob
import mrjob
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
#inputs to this job will be:
#full records: (docId,fhrPayload)
#unlinkable docids: (docId,"u")
#head record docIds: (docId,"h")
class finalRecordExtractionJob(MRJob):
HADOOP_INPUT_FORMAT="org.apache.hadoop.mapred.TextInputFormat"
INPUT_PROTOCOL = mrjob.protocol.RawProtocol
INTERNAL_PROTOCOL = mrjob.protocol.JSONProtocol
OUTPUT_PROTOCOL = mrjob.protocol.RawProtocol
def mapper(self,docId, val):
self.increment_counter("MAPPER", "input k/v pair")
yield(docId,val)
def reducer(self,docId, iter_jsonOrFlag):
# not all docIds will be re-emitted; only emit jsons when there is a flag of either "u" or "h"
emitJson=False
for jsonOrFlag in iter_jsonOrFlag:
if jsonOrFlag[0] in ["u","h"]:
emitJson=True
elif jsonOrFlag[0]=="{":
jsonOut = jsonOrFlag
else:
raise ValueError()
if emitJson:
self.increment_counter("REDUCER", "final record out")
yield(docId,jsonOut)
else:
self.increment_counter("REDUCER", "final records dropped")
if __name__ == '__main__':
finalRecordExtractionJob.run()