我一直在尝试使用java计算我的map reduce程序中员工的总登录时间。我的reducer没有提供任何输出,因此我尝试按原样输出键值对。我无法根据日期在减速器中组织阵列列表以执行总时间计算。 非常感谢您的帮助。
我的日志文件包含员工的登录详细信息,如下所示。
101 IN 9:00
102 IN 9:15
103 IN 9:16
104 IN 9:25
103 OUT 10:00
105 IN 10:30
101 OUT 11:30
101 IN 11:45
102 OUT 12:30
101 OUT 13:00
105 OUT 13:05
104 OUT 14:30
我的地图功能在下面给出
public class login_map extends MapReduceBase implements Mapper<LongWritable,Text,Text,Text>{
public void map (LongWritable key,Text value,OutputCollector <Text,Text>output, Reporter reporter)throws IOException{
String line = value.toString();
String emp_id = line.substring(0, 3);
String punch = line.substring(4);
output.collect(new Text (emp_id), new Text(punch));
}
}
相应的减速器如下所示
public class login_reduce extends MapReduceBase implements Reducer<Text,Text,Text,Text>{
public void reduce(Text key,Iterator <Text> values,OutputCollector <Text,Text>output,Reporter reporter)throws IOException{
ArrayList<Date> a = new ArrayList<Date>();
SimpleDateFormat punch = new SimpleDateFormat("HH:mm");
while (values.hasNext()){
String entry = values.next().toString();
try{a.add(punch.parse(entry.substring(7)));}
catch(java.text.ParseException e){
e.printStackTrace();
}
} //end of while
Iterator itr = a.iterator();
while (itr.hasNext()){
output.collect(key, new Text(itr.next().toString()));
}
}
}
预期产量
101 3:45
102 3:15
103 0:44
104 4:55
105 2:35
谢谢。