0

I have installed hadoop-1.0.3 and executed an sample program wordcount successfully in my 14.04 following this link.

I tried to write program in python by following here.

I have copied the following code,

#!/usr/bin/env python

import sys

# input comes from STDIN (standard input)
for line in sys.stdin:
    # remove leading and trailing whitespace
    line = line.strip()
    # split the line into words
    words = line.split()
    # increase counters
    for word in words:
        # write the results to STDOUT (standard output);
        # what we output here will be the input for the
        # Reduce step, i.e. the input for reducer.py
        #
        # tab-delimited; the trivial word count is 1
        print '%s\t%s' % (word, 1)

paste into a text editor and save the file as,

/home/hadoopuser/mapper.py

Note: My hadoop user name is hadoopuser

And given permission as,

hadoopuser@arul-PC:~$ sudo chmod +X /home/hadoopuser/mapper.py 

As well as saved the following code into /home/hadoopuser/reducer.py

#!/usr/bin/env python

from operator import itemgetter
import sys

current_word = None
current_count = 0
word = None

# input comes from STDIN
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()

# parse the input we got from mapper.py
word, count = line.split('\t', 1)

# convert count (currently a string) to int
try:
    count = int(count)
except ValueError:
    # count was not a number, so silently
    # ignore/discard this line
    continue

# this IF-switch only works because Hadoop sorts map output
# by key (here: word) before it is passed to the reducer
if current_word == word:
    current_count += count
else:
    if current_word:
        # write result to STDOUT
        print '%s\t%s' % (current_word, current_count)
    current_count = count
    current_word = word

# do not forget to output the last word if needed!
if current_word == word:
print '%s\t%s' % (current_word, current_count)

And so given permission to execute,

hadoopuser@arul-PC:~$ sudo chmod +X /home/hadoopuser/reducer.py

When I tried,

hadoopuser@arul-PC:~$ echo "AAA ACC ABC AAA AAA  ADD arul " | /home/hadoopuser/mapper.py

I got response as,

-su: /home/hadoopuser/mapper.py: Permission denied

I have also tried with sudo am getting same response. Please give me a solution.

0

1 Answer 1

1

First have to set home path by typing following command in terminal,

export PATH=$PATH:/home/hadoopuser/

And then given permission using,

chmod 755 /home/hadoopuser/mapper.py
chmod 755 /home/hadoopuser/reducer.py

And got result as,

echo "arul sijo sijo tijo tijo tijo sijo arul tijo" | /home/hadoopuser/mapper.py | sort -k1,1 | /home/hadoopuser/reducer.py
arul    2
sijo    3
tijo    4

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .