-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapReduceHadoop.java
219 lines (180 loc) · 8.35 KB
/
MapReduceHadoop.java
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* CS236: MapReduce Job to find hotel revenue
*
* author: christopher moussa
*/
import java.lang.reflect.Method;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
public class MapReduceHadoop {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "hotel-revenue");
// set mapper, reducer, and combiner classes
job.setJarByClass(MapReduceHadoop.class);
job.setMapperClass(HotelBookingMapper.class);
job.setCombinerClass(HotelBookingReducer.class);
job.setReducerClass(HotelBookingReducer.class);
// set output key/value classes for mapper and reducer
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(DoubleWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DoubleWritable.class);
// set input paths
FileInputFormat.addInputPath(job, new Path(args[0]));
FileInputFormat.addInputPath(job, new Path(args[1]));
FileOutputFormat.setOutputPath(job, new Path(args[2])); // output path
// wait for job to complete; will output 0 on SUCCESS, 1 on FAILURE
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
// mapper class
public static class HotelBookingMapper extends Mapper<LongWritable, Text, Text, DoubleWritable> {
private Text out_key = new Text();
private DoubleWritable out_value = new DoubleWritable();
private static final String FILE1_NAME = "customer-reservations.csv";
private static final String FILE2_NAME = "hotel-booking.csv";
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
FileSplit fileSplit = (FileSplit) context.getInputSplit();
String fileName = fileSplit.getPath().getName();
// check which input file the current record is from;
// this will determine how we parse the .csv file since
// the headers are different for each file
String inputFileTag;
if (fileName.contains("customer-reservations")) {
inputFileTag = FILE1_NAME;
} else {
inputFileTag = FILE2_NAME;
}
String[] csv_input_fields = value.toString().split(",");
// skip header row of customer-reservations.csv
if (csv_input_fields[0].equals("Booking_ID")) {
return;
}
// skip header row of hotel-booking.csv
if (csv_input_fields[0].equals("hotel")) {
return;
}
double avg_price_per_room = 0.0;
int arrival_yr = 0;
int arrival_mo = 0;
int stays_in_weekend_nights = 0;
int stays_in_week_nights = 0;
int total_nights = 0;
double total_cost = 0.0;
if (inputFileTag.contains("customer-reservations")) {
// extract relevant fields for the map phase, consisting of:
//
// - avg_price_per_room
// - arrival_year
// - arrival_month
// - booking_status
// we need to check if the reservation was canceled or not;
// if it is canceled, we should just set the price_per_room
// to 0 because the customer never actually stayed there
boolean canceled = csv_input_fields[9].equals("Canceled");
if (canceled == true) {
avg_price_per_room = 0.0;
} else {
avg_price_per_room = Double.parseDouble(csv_input_fields[8]);
}
arrival_yr = Integer.parseInt(csv_input_fields[4]);
arrival_mo = Integer.parseInt(csv_input_fields[5]);
stays_in_weekend_nights = Integer.parseInt(csv_input_fields[1]);
stays_in_week_nights = Integer.parseInt(csv_input_fields[2]);
total_nights = stays_in_week_nights + stays_in_week_nights;
total_cost = total_nights * avg_price_per_room;
} else {
// we know that we are parsing hotel-booking, so we need to extract
// the relevant fields for this specific .csv file, which consists
// of:
//
// - avg_price_per_room
// - arrival_year
// - arrival_month
// - booking_status
// same check as for customer-reservations.csv; if the customer
// canceled their reservation, we shouldn't add their revenue
// to the total revenue for a given month/year
boolean canceled = Integer.parseInt(csv_input_fields[1]) == 1;
if (canceled == true) {
avg_price_per_room = 0.0;
} else {
avg_price_per_room = Double.parseDouble(csv_input_fields[11]);
}
arrival_yr = Integer.parseInt(csv_input_fields[3]);
// the arrival_month field in hotel-booking.csv is in String format,
// so we need to map the month to an integer value
switch(csv_input_fields[4]) {
case "January":
arrival_mo = 1;
break;
case "February":
arrival_mo = 2;
break;
case "March":
arrival_mo = 3;
break;
case "April":
arrival_mo = 4;
break;
case "May":
arrival_mo = 5;
break;
case "June":
arrival_mo = 6;
break;
case "July":
arrival_mo = 7;
break;
case "August":
arrival_mo = 8;
break;
case "September":
arrival_mo = 9;
break;
case "October":
arrival_mo = 10;
break;
case "November":
arrival_mo = 11;
break;
case "December":
arrival_mo = 12;
break;
}
stays_in_weekend_nights = Integer.parseInt(csv_input_fields[7]);
stays_in_week_nights = Integer.parseInt(csv_input_fields[8]);
total_nights = stays_in_week_nights + stays_in_week_nights;
total_cost = total_nights * avg_price_per_room;
}
// create month-year composite key to map revenue to
String comp_key = arrival_mo + "-" + arrival_yr;
// output key
out_key.set(comp_key);
out_value.set(total_cost);
context.write(out_key, out_value);
}
}
// reducer class
public static class HotelBookingReducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
private DoubleWritable out_value = new DoubleWritable();
public void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
double total_revenue = 0.0;
// aggregate revenues for the same month/year
for (DoubleWritable value : values) {
total_revenue += value.get();
}
// output the composite key and total revenue
out_value.set(total_revenue);
context.write(key, out_value);
}
}
}