使用Python运行MapReduce流作业时出错

时间:2014-04-23 02:24:09

标签: hadoop mapreduce hadoop-streaming

我正在尝试运行此映射器和缩减器代码(*免责声明 - 培训课程解决方案的一部分)

mapper.py

import sys

for line in sys.stdin:
    data = line.strip().split("\t")
    if len(data) == 6:
        date, time, store, item, cost, payment = data
        print "{0}\t{1}".format(1, cost)

reducer.py

import sys

sTotal = 0
trans = 0

for line in sys.stdin:
    data_mapped = line.strip().split("\t")
    if len(data_mapped) != 2:
        continue

    sTotal += float(data_mapped[1])
    trans += 1

print transactions, "\t", salesTotal

继续抛出此错误:

UNDEF/bin/hadoop job  -Dmapred.job.tracker=0.0.0.0:8021 -kill job_201404041914_0012
14/04/04 23:13:53 INFO streaming.StreamJob: Tracking URL: http://0.0.0.0:50030/jobdetails.jsp?jobid=job_201404041914_0012
14/04/04 23:13:53 ERROR streaming.StreamJob: Job not successful. Error: NA
14/04/04 23:13:53 INFO streaming.StreamJob: killJob...
Streaming Command Failed!

我已经尝试过显式调用python函数,也指定了python解释器。 (即/ usr / bin / env python)

知道哪里出错了?

1 个答案:

答案 0 :(得分:0)

作业失败,因为您的reducer.py语法错误。

问题在于这一行:

print transactions, "\t", salesTotal

没有名称为transactionssalesTotal的变量。

如果我在本地执行它,我会收到此错误:

Traceback (most recent call last):
  File "r.py", line 14, in <module>
    print transactions, "\t", salesTotal
NameError: name 'transactions' is not defined

正确的行应该是:

print trans, "\t", sTotal