I am running map reduce locally.
My command line command looks like this:
cat testfile | python ./mapper.py | python ./reducer.py
and this works fine. However, when my command looks like this:
cat testfile | ./mapper.py | ./reducer.py
I get the following error:
./mapper.py: line 1: import: command not found
./mapper.py: line 3: syntax error near unexpected token `('
./mapper.py: line 3: `def mapper():
This makes sense since the command line is reading my python file as bash and getting confused by the pythonic syntax.
But all of the online examples I look at (e.g. http://www.michael-noll.com/tutorials/writing-an-hadoop-mapreduce-program-in-python/) don't include the python
before the .py files. How can I configure my machine to run the pipe without specifying python
before mapper.py and reducer.py?
Just in case it helps, here's my mapper code:
import sys
def mapper():
for line in sys.stdin:
data = line.strip().split('\t')
if len(data) == 6:
category = data[3]
sales = data[4]
print '{0}\t{1}'.format(category, sales)
if __name__ == "__main__":
mapper()
here's my reducer code:
import sys
def reducer():
current_total = 0
old_key = None
for line in sys.stdin:
data = line.strip().split('\t')
if len(data) == 2:
current_key, sales = data
sales = float(sales)
if old_key and current_key != old_key:
print "{0}\t{1}".format(old_key, current_total)
current_total = 0
old_key = current_key
current_total += sales
print "{0}\t{1}".format(current_key, current_total)
if __name__ == "__main__":
reducer()
And my data looks like this:
2012-01-01 09:01 Anchorage DVDs 6.38 Amex
2012-01-01 09:01 Aurora Electronics 117.81 MasterCard
2012-01-01 09:01 Philadelphia DVDs 351.31 Cash