我正在使用Hadoop/MapReduce创建一个电影推荐。
现在只使用python的mapm进程实现reduce。在
所以我所要做的就是分别运行每个mapper和reducer,并使用mapper到reducer的控制台输出。在
我遇到的问题是python在终端中以字符串的形式输出值,因此如果我使用数字,则数字将打印为字符串,这使得简化过程变得困难,因为转换会增加服务器的负载。在
所以我如何解决这个问题,我希望使用纯python而不使用第三方lib来实现它。在
import sys
def mapper():
'''
From Mapper1 : we need only UserID , (MovieID , rating)
as output.
'''
#* First mapper
# Read input line
for line in sys.stdin:
# Strip whitespace and delimiter - ','
print line
data = line.strip().split(',')
if len(data) == 4:
# Using array to print out values
# Direct printing , makes python interpret
# values with comma in between as tuples
# tempout = []
userid , movieid , rating , timestamp = data
# tempout.append(userid)
# tempout.append((movieid , float(rating)))
# print tempout
#
print "{0},({1},{2})".format(userid , movieid , rating)
打印减速机声明:
^{pr2}$`
输入是:
671,(4973,4.5)\n671,(4993,5.0)\n670,(4995,4.0)
输出为:
671,['(4973,4.5)', '(4993,5.0)']
670,['(4995,4.0)']
我需要元组,没有字符串。在
事实上,数据是一个字符串,然后将
y
拆分并分配给它,它仍然是一个字符串。在如果你想要元组的原始值,作为数字,你需要解析它们。在
ast.literal_eval
可以帮上忙。在例如
现在,如果您想切换到PySpark,那么您将能够更好地控制变量/对象类型,而不是使用Hadoop流式处理的所有字符串
相关问题 更多 >
编程相关推荐