我有表格中的数据
id, movieid , date, time
3710100, 13502, 2012-09-10, 12:39:38.000
现在基本上我想做的就是这个..
我想知道,在上午7点到11点之间以30分钟的间隔观看特定电影的次数
所以基本上......
之间观看了多少次电影
6 and 6:30
6:30 and 7
7 and 7:30
...
10:30-11
所以我写了mapper和reducer来实现这个目标。
mapper.py
#!/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
line = line.split(",")
#print line
print '%s\t%s' % (line[1], line)
reducer.py
#!/usr/bin/env python
import sys
import datetime
from collections import defaultdict
def convert_str_to_date(time_str):
try:
timestamp = datetime.datetime.strptime(time_str, '%Y-%m-%d:%H:%M:%S.000') #00:23:51.000
return timestamp
except Exception,inst:
pass
def is_between(time, time1,time2):
return True if time1 <= time < time2 else False
def increment_dict(data_dict, se10,date_time):
start_time = datetime.datetime(date_time.year,date_time.month,date_time.day, 07,00,00)
times = [start_time]
for i in range(8):
start_time += datetime.timedelta(minutes = 30 )
times.append(start_time)
for i in range(len(times) -1 ):
if is_between(date_time, times[i], times[i+1]):
data_dict[se10][i] += 1
keys = [0,1,2,3,4,5,6,7]
data_dict = defaultdict(dict)
# input comes from STDIN
def initialize_entry(se10):
for key in keys:
data_dict[se10][key] = 0
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# parse the input we got from mapper.py
se10, orig_data = line.split('\t')
initialize_entry(se10)
parse_line = orig_data.split(",")
datestr = parse_line[2].replace(" ","").replace("'","")
timestr = parse_line[3].replace(" ","").replace("'","")
date_time = datestr + ":" + timestr
time_stamp = convert_str_to_date(date_time)
increment_dict(data_dict, se10,time_stamp)
for key, secondary_key in data_dict.items():
for skey, freq in secondary_key.items():
print key,"," ,skey,",",freq
如果我执行
,上面的代码运行正常 cat input.txt | python mapper.py | sort | python reducer.py
但是当我将它部署在集群上时。它没有说这项工作已经被杀死了......这个原因是未知的。
请帮忙。
感谢。
答案 0 :(得分:0)
好的,我想出了这件事......
主要问题是我的工作本地机器是基于Windows的......而集群是基于Linux的..
所以我不得不将用dos编写的文件转换为unix格式..
答案 1 :(得分:0)
通常好主意阅读JobHistory中的日志,如https://stackoverflow.com/a/24509826/1237813中所述。它应该为您提供更多详细信息,说明作业失败的原因。
关于行结尾,Hadoop Streaming类默认使用分割行为TextInputFormat。它used to break与Windows换行,但自2006年以来它应该可以正常工作。
这会使您的mapper和reducer脚本成为问题的可能来源。 Python 3使用了一个名为universal newlines的东西,它应该是Unix和Windows换行的Just Work Out Of The Box。在Python 2.7中,您需要明确地将其打开。
在Linux和Mac OS X上,您可以使用通用换行符重新打开stdin,例如sys.stdin = open('/dev/stdin', 'U')
。我手边没有Windows计算机可以尝试,但以下内容应适用于所有三个系统:
import os
import sys
# reopen sys.stdin
os.fdopen(sys.stdin.fileno(), 'U')
for line in sys.stdin:
…