gpt4 book ai didi

python - python : os. environ ["map_input_file"中的 MapReduce ] 在 map.py 中不起作用

转载 作者:可可西里 更新时间:2023-11-01 15:10:09 30 4
gpt4 key购买 nike

这是我第一次用python学习Hadoop MapReduce。

为了学习如何连接两个文件,我写了一个 map.py 来获取两个文件的文件名。这里有两个 CSV 文件:

worksheet1.csv

sno,name
1,name1
2,name2
3,name3
4,name4

worksheet2.csv

sno,courseno,grade
1,1,80
1,2,90
2,1,82
2,2,95

map .py:

#!/bin/bash
# -*- coding: utf-8 -*-
import os
import sys

def mapper():

filepath = os.environ["map_input_file"]
filename = os.path.split(filepath)[-1] #get the names
for line in sys.stdin:
if line.strip()=="":
continue
fields = line[:-1].split("\t")
sno = fields[0] #get student ID

if filename == 'worksheet1':
#get student ID and name, mark 0
name = fields[1]
print '\t'.join((sno,'0',name))
elif filename == 'worksheet2':
#get student ID, course number, grade, mark 1
courseno = fields[1]
grade = fields[2]
print '\t'.join((sno,'1',courseno,grade))


if __name__=='__main__':
mapper()

然后我用

    $cat worksheet1 worksheet2 |python map.py

测试程序。

报错如下:

Traceback (most recent call last):
File "map.py", line 30, in <module>
mapper()
File "map.py", line 11, in mapper
filepath = os.environ['map_input_file']
File "/usr/lib64/python2.7/UserDict.py", line 23, in __getitem__
raise KeyError(key)
KeyError: 'map_input_file'

请告诉我为什么以及如何修改代码。非常感谢!

最佳答案

您还没有设置map_input_file 环境变量。此外,您正在将数据文件通过管道传输到脚本,以便它们在脚本中作为 sys.stdin 可用,但是用于发现当前正在读取其中哪些文件的代码是完全错误的。我建议只使用 fileinput 模块。

关于python - python : os. environ ["map_input_file"中的 MapReduce ] 在 map.py 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44878991/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com