-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq7.java
More file actions
171 lines (138 loc) · 6.1 KB
/
q7.java
File metadata and controls
171 lines (138 loc) · 6.1 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package tde1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
import org.apache.log4j.BasicConfigurator;
import java.io.IOException;
public class q7 {
public static void main(String[] args) throws IOException,
ClassNotFoundException,
InterruptedException {
BasicConfigurator.configure();
Configuration c = new Configuration();
String[] files = new GenericOptionsParser(c, args).getRemainingArgs();
Path input = new Path("in/transactions_amostra.csv");
Path output = new Path("output/intermediate");
Job j = new Job(c, "mr1");
j.setJarByClass(q7.class);
j.setReducerClass(Reduce1.class);
j.setMapperClass(Map1.class);
j.setCombinerClass(Combine1.class);
j.setMapOutputKeyClass(Text.class);
j.setMapOutputValueClass(DoubleWritable.class);
j.setOutputKeyClass(Text.class);
j.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(j, input);
FileOutputFormat.setOutputPath(j, output);
j.waitForCompletion(false);
Path input2 = new Path("output/intermediate");
Path output2 = new Path("output/q7");
Job j2 = new Job(c, "mr2");
j2.setJarByClass(q7.class);
j2.setReducerClass(Reduce2.class);
j2.setCombinerClass(Combine2.class);
j2.setMapperClass(Map2.class);
j2.setMapOutputKeyClass(Text.class);
j2.setMapOutputValueClass(TDE7.class);
j2.setOutputKeyClass(Text.class);
j2.setOutputValueClass(TDE7.class);
FileInputFormat.addInputPath(j2, output);
FileOutputFormat.setOutputPath(j2, output2);
j2.waitForCompletion(false);
}
public static class Map1 extends Mapper<LongWritable, Text, Text, DoubleWritable> {
// Funcao de map
public void map(LongWritable key, Text value, Context con)
throws IOException, InterruptedException {
String linha = value.toString();
//ignorando cabeçalho
if(linha.startsWith("country_or_area")) return;
String[] colunas = linha.split(";");
String commcode = colunas[2];
String amount = colunas[8];
String flowtype = colunas[4];
String ano = colunas[1];
// write apenas quando o ano for 2016
if(ano.equals("2016")){
// Caso o ano seja 2016, passamos o comcode, o flowtype e a quantidade.
con.write(new Text(commcode + "," + flowtype + ","), new DoubleWritable(Double.parseDouble(amount)));
}
}
}
public static class Combine1 extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
public void reduce(Text key, Iterable<DoubleWritable> values, Context con)
throws IOException, InterruptedException {
double soma = 0;
// combine faz uma pré soma dos valores para agilizar
for (DoubleWritable d : values) {
soma += d.get();
}
con.write(key, new DoubleWritable(soma));
}
}
public static class Reduce1 extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
public void reduce(Text key, Iterable<DoubleWritable> values, Context con)
throws IOException, InterruptedException {
// soma final das quantidades
double soma = 0;
for (DoubleWritable d : values) {
soma += d.get();
}
//write com a chave (commcode e flowtype) e a soma dos amounts de cada chave
con.write(key, new DoubleWritable(soma));
}
}
public static class Map2 extends Mapper<LongWritable, Text, Text, TDE7> { //maior amount por flowtype
// Funcao de map
public void map(LongWritable key, Text value, Context con)
throws IOException, InterruptedException {
String linha = value.toString();
linha.replace("\t", ",");
String[] colunas = linha.split(",");
String flowtype = colunas[1];
String commcode = colunas[0];
double amount = Double.parseDouble(colunas[2]);
con.write(new Text(flowtype), new TDE7(commcode, amount));
}
}
public static class Combine2 extends Reducer<Text, TDE7, Text, TDE7> {
public void reduce(Text key, Iterable<TDE7> values, Context con)
throws IOException, InterruptedException {
// O combine compara os amounts afim de sempre salvar o maior para agilizar o trabalho do reduce
String commcode = "";
double amount = Double.MIN_VALUE;
for (TDE7 t1 : values) {
if (t1.getAmount() > amount) {
commcode = t1.getCommcode();
amount = t1.getAmount();
}
}
con.write(key, new TDE7(commcode, amount));
}
}
public static class Reduce2 extends Reducer<Text, TDE7, Text, TDE7> {
public void reduce(Text key, Iterable<TDE7> values, Context con)
throws IOException, InterruptedException {
String commcode = "";
double amount = Double.MIN_VALUE;
// Reduce faz a verificação final dos amounts para garantir que o write seja feito com o maior amount por flowtype
for (TDE7 t1 : values) {
if (t1.getAmount() > amount) {
commcode = t1.getCommcode();
amount = t1.getAmount();
}
}
con.write(key, new TDE7(commcode, amount));
}
}
}