-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAverage.java
111 lines (86 loc) · 3.84 KB
/
Average.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
/**
*finding the cheapest and most expensive products of the given brand
*/
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
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.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class Average {
private static String brand_option;
private enum COUNTERS{
INVALID_RECORD_COUNT
}
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
conf.set("mapred.textoutputformat.separator", ","); //csv format
if(args.length != 3){
System.err.println("Usage:<in> <out> <brandOpt> ");
System.exit(2);
}
brand_option = args[2];
Job job = Job.getInstance(conf,"Min-Max");
job.setJarByClass(Average.class);
job.setMapperClass(AverageMapper.class);
job.setCombinerClass(AverageReducer.class);
job.setReducerClass(AverageReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DoubleWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
org.apache.hadoop.mapreduce.Counters counters = job.getCounters();
System.out.println("Invalid record count :"+ counters.findCounter(COUNTERS.INVALID_RECORD_COUNT).getValue());
}
public static class AverageMapper extends Mapper<Object, Text, Text, DoubleWritable> {
IntWritable productId = new IntWritable();
String brandName;
DoubleWritable price = new DoubleWritable();
String eventType;
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] parts = value.toString().split("[,]");
productId.set(Integer.parseInt(parts[TableHeaders.PRODUCT_ID]));
price.set(Double.parseDouble(parts[TableHeaders.PRICE]));
brandName = parts[TableHeaders.BRAND];
eventType = parts[TableHeaders.EVENT_TYPE];
if (parts.length != 9) {
// add counter for invalid records
context.getCounter(COUNTERS.INVALID_RECORD_COUNT).increment(1L);
}else{
if(eventType.equals("purchase")){
if(brand_option.equals("all"))
context.write(new Text(brandName), price);
else if( brandName.equals(brand_option))
context.write(new Text(brandName), price);
}
}
}
}
public static class AverageReducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
@Override
public void reduce( Text brandName,Iterable<DoubleWritable> values, Context context)
throws IOException, InterruptedException {
Double sum = 0.0;
Integer size = 0;
Double average;
for (DoubleWritable val : values) {
sum += val.get();
size += 1;
}
average = sum/size;
context.write(brandName, new DoubleWritable(average) );
}
}
}