-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubtask8.java
More file actions
114 lines (90 loc) · 3.27 KB
/
subtask8.java
File metadata and controls
114 lines (90 loc) · 3.27 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package test2;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.json.JSONArray;
import org.json.JSONObject;
public class subtask8
{
public static void main( String[] args ) throws Exception
{
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "winning match wrt gold");
job.setJarByClass(subtask8.class);
job.setMapperClass(st8Mapper.class);
job.setCombinerClass(st8Reducer.class);
job.setReducerClass(st8Reducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setNumReduceTasks(20);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
static class st8Mapper extends Mapper<Object, Text, Text, IntWritable> {
private Text champion = new Text();
private Text write = new Text();
private IntWritable gold;
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String jsonString = value.toString();
JSONObject obj = new JSONObject(jsonString);
JSONObject obj1 = new JSONObject(jsonString);
int winner = obj.getInt("winner");
ArrayList<Integer> winList = new ArrayList<Integer>();
ArrayList<Integer> loseList = new ArrayList<Integer>();
JSONArray players = obj.getJSONArray("players");
for (int i = 0; i < players.length(); i++)
{
JSONObject o = players.getJSONObject(i);
int teamID = o.getInt("teamID");
int championID = o.getInt("championID");
if(teamID==winner) winList.add(championID);
else loseList.add(championID);
}
JSONObject data = (JSONObject)obj1.get("data");
JSONArray goldEarnedEvents = data.getJSONArray("goldEarnedEvents");
for (int j = 0; j < goldEarnedEvents.length(); j++)
{
JSONObject ob = goldEarnedEvents.getJSONObject(j);
double time = ob.getDouble("time");
if(time<300.0)
{
int unitID = ob.getInt("unitID");
int goldEarned = ob.getInt("goldEarned");
gold= new IntWritable(goldEarned);
if(winList.contains(unitID))
{
champion.set("winning team");
write.set(champion);
context.write(write, gold);
}
else
{
champion.set("losing team");
write.set(champion);
context.write(write, gold);
}
}
}
}
}
static class st8Reducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable numGames = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
Integer gold = 0;
for (IntWritable val : values) {
gold += val.get();
}
numGames.set(gold);
context.write(key, numGames);
}
}
}