Repository navigation
Expand file tree
/
Copy pathCalculateFinalScores.java
More file actions
96 lines (62 loc) · 2.47 KB
/
Copy pathCalculateFinalScores.java
File metadata and controls
96 lines (62 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package aic.proj.test;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class CalculateFinalScores {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text>
{
private final static Text userPair = new Text();
private Text score = new Text();
public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
String line = value.toString();
try
{
JSONObject json = (JSONObject) new JSONParser().parse(line);
Iterator iter = json.keySet().iterator();
String userIdPair = (String) iter.next();
userPair.set(userIdPair);
score.set(json.get(userIdPair).toString());
output.collect(userPair, score);
}
catch(ParseException e)
{
e.printStackTrace();
}
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text>
{
private Text score = new Text();
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
int final_score = 0;
while (values.hasNext())
{
final_score += Integer.parseInt(values.next().toString());
}
score.set("?"+final_score);
key.set(key.toString()+":");
output.collect(key, score);
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(CalculateFinalScores.class);
conf.setJobName("calculatefinalscores");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(Text.class);
conf.setMapperClass(Map.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}