-
Notifications
You must be signed in to change notification settings - Fork 0
/
Step5.java
93 lines (75 loc) · 3.58 KB
/
Step5.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
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.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class Step5 {
public static class MapperClass extends Mapper<LongWritable, Text, Text, Text> {
@Override
public void map(LongWritable key, Text Value, Context context) throws IOException, InterruptedException {
String[] value = Value.toString().split("\t");
String[] str = value[1].split("\\s+");
context.write(new Text(value[0]), new Text(str[0] + " " + str[1] + " " + str[2]
+ " " + str[3] + " " + str[4] + " " + str[5]));// tr: r0 r1 tr01 nr0 tr10 nr1)
}
}
public static class PartitionerClass extends Partitioner<Text, Text> {
public int getPartition(Text key, Text value, int numPartitions) {
return key.hashCode();
}
}
public static class ReducerClass extends Reducer<Text, Text, Text, DoubleWritable> {
@Override
public void reduce(Text key, Iterable<Text> Values, Context context) throws IOException, InterruptedException {
Iterator<Text> it = Values.iterator();
double result = 0;
long n = context.getConfiguration().getLong("n",0);
while(it.hasNext()){
String[] str = it.next().toString().split(" ");
double tr01 = Double.parseDouble(str[2]);
double nr0 = Double.parseDouble(str[3]);
double tr10 = Double.parseDouble(str[4]);
double nr1 = Double.parseDouble(str[5]);
result = (tr01+tr10)/(n);
result = result * (1/(nr0+nr1));
}
context.write(key,new DoubleWritable(result));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "final");
FileSystem fs = FileSystem.get(new URI("s3://output-hadoop-test/n"), conf);
Path filePath = new Path("s3://output-hadoop-test/n");
FSDataInputStream fsDataInputStream = fs.open(filePath);
BufferedReader br = new BufferedReader(new InputStreamReader(fsDataInputStream));
long n = (long)Double.parseDouble(br.readLine());
job.getConfiguration().setLong("n",n);
FileSystem.setDefaultUri(conf, new URI(args[1]));
//set format for input and output
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
//path to input and output files
FileInputFormat.addInputPath(job, new Path(args[0]));
FileSystem.get(conf).delete(new Path(args[1]), true);
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setJarByClass(Step5.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DoubleWritable.class);
job.setMapperClass(MapperClass.class);
job.setReducerClass(ReducerClass.class);
job.setPartitionerClass(PartitionerClass.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}