我已经安装了 hadoop-1.0.3,并在 14.04 中成功执行了一个示例程序 wordcount,如下所示此链接。
我尝试按照以下方法用 Python 编写程序这里。
我复制了以下代码,
#!/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)
粘贴到文本编辑器并将文件另存为,
/home/hadoop用户/mapper.py
注:我的hadoop用户名是hadoopuser
并获得许可,
hadoopuser@arul-PC:~$ sudo chmod +X /home/hadoopuser/mapper.py
并将以下代码保存到/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)
因此,获得执行许可,
hadoopuser@arul-PC:~$ sudo chmod +X /home/hadoopuser/reducer.py
当我尝试的时候,
hadoopuser@arul-PC:~$ echo "AAA ACC ABC AAA AAA ADD arul " | /home/hadoopuser/mapper.py
我得到的答复是,
-su: /home/hadoopuser/mapper.py: Permission denied
我也尝试过,但sudo
得到的答复相同。请给我一个解决方案。
答案1
首先必须通过在终端中输入以下命令来设置主路径,
export PATH=$PATH:/home/hadoopuser/
然后获得使用许可,
chmod 755 /home/hadoopuser/mapper.py
chmod 755 /home/hadoopuser/reducer.py
并得到如下结果:
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