-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransposer.java
82 lines (57 loc) · 2.49 KB
/
Transposer.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
public class Transposer {
private static class TMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] vals = value.toString().split("\\t");
String[] ai = vals[1].split(",");
for (int i = 0; i < ai.length; i++) {
context.write(
new LongWritable(i + 1),
new Text(vals[0] + "\t" + ai[i])
);
}
}
}
private static class TReducer extends Reducer<LongWritable, Text, LongWritable, Text> {
@Override
protected void reduce(LongWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
int n = context.getConfiguration().getInt("mh", -1);
double[] ai = new double[n];
for (Text value : values) {
String[] keyVal = value.toString().split("\\t");
int j = Integer.parseInt(keyVal[0]);
double aij = Double.parseDouble(keyVal[1]);
ai[j - 1] = aij;
}
StringBuilder result = new StringBuilder();
for (int j = 0; j < n; j++) {
result.append(ai[j]);
if (j < n - 1) {
result.append(",");
}
}
context.write(key, new Text(result.toString()));
}
}
private Configuration configuration;
private String inputPath;
private String outputPath;
public Transposer(Configuration configuration, String inputPath, String outputPath) {
this.configuration = configuration;
this.inputPath = inputPath;
this.outputPath = outputPath;
}
public void run() throws IOException, ClassNotFoundException, InterruptedException {
Job job = Job.getInstance(configuration, "com.lsdp.util.Transposer");
job.setJarByClass(MRNMF.class);
FileInputFormat.addInputPath(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath));
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(Text.class);
job.setMapperClass(TMapper.class);
job.setReducerClass(TReducer.class);
job.waitForCompletion(true);
}
}