Repository navigation
Expand file tree
/
Copy pathHistogram.java
More file actions
executable file
·120 lines (107 loc) · 3.73 KB
/
Copy pathHistogram.java
File metadata and controls
executable file
·120 lines (107 loc) · 3.73 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
//Name:Chinmaya Venkatesh
//UTA ID:1001778014
import java.io.*;
import java.util.Scanner;
import java.util.Vector;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import java.io.*;
import java.util.Scanner;
import java.util.Vector;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
/* single color intensity */
class Color implements WritableComparable {
public short type; /* red=1, green=2, blue=3 */
public short intensity; /* between 0 and 255 */
/* need class constructors, toString, write, readFields, and compareTo methods */
Color(){}
Color(short ty,short intens){
type = ty;
intensity = intens;
}
public void write ( DataOutput out ) throws IOException {
out.writeShort(type);
out.writeShort(intensity);
}
public void readFields ( DataInput in ) throws IOException {
type = in.readShort();
intensity = in.readShort();
}
@Override
public String toString() { return type+" "+intensity; }
public int compareTo(Object o) {
Color co = (Color)o;
short thisType = this.type;
short thatType = co.type;
short thisIntense = this.intensity;
short thatIntense = co.intensity;
if(thisType==thatType){
if(thisIntense == thatIntense){
return 0;
}
else if(thisIntense>thatIntense){
return 1;
}
else
return -1;
}
else if(thisType<thatType){
return -1;
}
return 1;
}
}
public class Histogram {
public static class HistogramMapper extends Mapper<Object,Text,Color,IntWritable> {
@Override
public void map ( Object key, Text value, Context context )
throws IOException, InterruptedException {
/* write your mapper code */
Scanner sc = new Scanner(value.toString()).useDelimiter(",");
short red = sc.nextShort();
short green = sc.nextShort();
short blue = sc.nextShort();
context.write(new Color((short)1,red),new IntWritable(1));
context.write(new Color((short)2,green),new IntWritable(1));
context.write(new Color((short)3,blue),new IntWritable(1));
sc.close();
}
}
public static class HistogramReducer extends Reducer<Color,IntWritable,Color,IntWritable> {
@Override
public void reduce ( Color key, Iterable<IntWritable> values, Context context )
throws IOException, InterruptedException {
/* write your reducer code */
int sum = 0;
for(IntWritable i : values){
sum += i.get();
};
context.write(key,new IntWritable(sum));
}
}
public static void main ( String[] args ) throws Exception {
/* write your main program code */
Job job = Job.getInstance();
job.setJobName("HistogramJob");
job.setJarByClass(Histogram.class);
job.setOutputKeyClass(Color.class);
job.setOutputValueClass(IntWritable.class);
job.setMapOutputKeyClass(Color.class);
job.setMapOutputValueClass(IntWritable.class);
job.setMapperClass(HistogramMapper.class);
job.setReducerClass(HistogramReducer.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
job.waitForCompletion(true);
}
}