liquiior 2024-02-20 14:13 采纳率: 66.7%
浏览 2

hadoop python reduce运行出错code 1

hadoop python reduce运行出错code 1

下面是我的mapperG.py

import sys

for line in sys.stdin:
    if not line:  # Skip empty lines
        continue
    line = line.strip()
    words = line.strip().split('->')
    for i in range(0,len(words)-1):
        if words[i]<words[i+1]:
            print("{}\t{}".format(words[i]+words[i+1],0))
        else:
            print("{}\t{}".format(words[i + 1]+words[i], 1))

下面是我的reducerG.py

from operator import itemgetter
import sys

current_nodes = None
current_direct = -1

for line in sys.stdin:
    line = line.strip()
    try:
        nodes, direct = line.split('\t', 1)
    except ValueError:
        # This handles lines that do not conform to the expected format
        print(f"Error processing line: {line}", file=sys.stderr)
        continue  # Skip this line and move to the next
    
    # Check if we're still processing the same pairs
    if current_nodes == nodes:
        if current_direct!=int(direct):
            current_direct=2
    else:
        # Output the previous pairs
        if current_nodes:
            if current_direct!=2:
                s = "{0} and {1} are in a one-way relationship".format(current_nodes[0], current_nodes[
                    1]) if current_direct == 0 else "{} and {} are in a one-way relationship".format(
                    current_nodes[1], current_nodes[0])
                print(s)
        # Reset for the new pairs
        current_nodes = nodes
        current_direct=int(direct)

# Output the last pairs after finishing all lines
if current_direct != 2:
    s = "{0} and {1} are in a one-way relationship".format(current_nodes[0], current_nodes[
        1]) if current_direct == 0 else "{} and {} are in a one-way relationship".format(
        current_nodes[1], current_nodes[0])
    print(s)

这是我的命令:

hadoop jar $HADOOP_HOME/hadoop-streaming-3.2.3.jar -input relations.txt -output outputG3 -mapper "python3 mapperG.py" -reducer "python3 reducerG.py" -file mapperG.py -file reducerG.py

input文件长这样

img


我用如下的命令运行是可以出结果的:

cat relations.txt|python mapperG.py|sort|python reducerG.py

img


然而一使用hadoop就报错:

img

img

  • 写回答

2条回答 默认 最新

  • GISer Liu 2024-02-20 15:21
    关注

    该回答引用自GPT-3.5,由博主GIS_Liu编写:

    根据你提供的信息,报错是在使用Hadoop时出现的,而在本地运行相同的逻辑却没有问题。这可能是由于Hadoop环境中的一些限制或配置问题导致的。

    解决思路:

    1. 查看Hadoop日志: 首先,你可以查看Hadoop的日志以获取更详细的错误信息,这有助于更好地理解问题所在。

    2. 确认输入输出格式: 确保你的输入和输出格式与Hadoop Streaming所期望的格式一致。特别是在mapper和reducer之间的数据交换格式。

    3. 环境变量设置: 确保Hadoop环境变量已正确设置,并且Python版本与Hadoop兼容。有时候Hadoop环境使用的Python版本可能与你在本地使用的版本不同,这可能导致一些不一致性。

    4. 调试代码: 你可以在代码中添加一些打印语句,以便在Hadoop集群上运行时查看中间结果,进而找出问题所在。

    5. 排查权限问题: 确保Hadoop集群中你的用户有足够的权限来执行MapReduce作业,包括对输入和输出路径的读写权限。

    解决方案:

    根据你提供的代码,我注意到你的reducer部分存在一个潜在的问题:在比较节点关系时,你尝试访问一个元组(tuple)的索引来获取节点,但你没有将输入的文本进行解析以获得这样的元组。这可能是导致报错的原因之一。

    我建议你修改reducer部分的代码,以正确解析mapper输出并比较节点关系。具体来说,你需要将节点关系解析为元组形式,类似于 (node1, node2) 的形式。你可以通过在mapper中以适当的方式输出这样的格式来实现。

    此外,请确保mapper和reducer的输入输出格式与Hadoop Streaming所期望的格式一致,这也是常见的问题导致作业运行失败的原因之一。

    修改后的代码示例:

    # 修改后的reducerG.py
    
    from operator import itemgetter
    import sys
    
    current_nodes = None
    current_direct = -1
    
    for line in sys.stdin:
        line = line.strip()
        try:
            nodes, direct = line.split('\t', 1)
        except ValueError:
            # This handles lines that do not conform to the expected format
            print(f"Error processing line: {line}", file=sys.stderr)
            continue  # Skip this line and move to the next
    
        # Convert nodes to a tuple
        nodes = tuple(nodes.split())
    
        # Check if we're still processing the same pairs
        if current_nodes == nodes:
            if current_direct != int(direct):
                current_direct = 2
        else:
            # Output the previous pairs
            if current_nodes:
                if current_direct != 2:
                    s = "{0} and {1} are in a one-way relationship".format(current_nodes[0], current_nodes[1]) if current_direct == 0 else "{} and {} are in a one-way relationship".format(current_nodes[1], current_nodes[0])
                    print(s)
            # Reset for the new pairs
            current_nodes = nodes
            current_direct = int(direct)
    
    # Output the last pairs after finishing all lines
    if current_direct != 2:
        s = "{0} and {1} are in a one-way relationship".format(current_nodes[0], current_nodes[1]) if current_direct == 0 else "{} and {} are in a one-way relationship".format(current_nodes[1], current_nodes[0])
        print(s)
    

    请将这些修改应用到你的代码中,然后重新运行作业,看看是否解决了问题。如有其他问题,可以根据Hadoop的日志进一步排查。

    如果该回答解决了您的问题,请采纳!如果没有,请参考以下方案进行修订

    用户答题指南

    评论

报告相同问题?

问题事件

  • 创建了问题 2月20日

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)