-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixMul.java
More file actions
135 lines (114 loc) · 4.62 KB
/
matrixMul.java
File metadata and controls
135 lines (114 loc) · 4.62 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package org.apache.hadoop.examples;
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.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.util.GenericOptionsParser;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapred.JobConf;
import java.util.*;
public class matrixMul {
public static class Map1 extends Mapper<Object, Text, Text, Text>{
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] line = value.toString().split(",");
String mapval = "";
String matrixID = line[0];
String posval = line[3];
String jpos = "";
if(matrixID.equals("M"))
{
jpos = line[2];
String ipos = line[1];
mapval = matrixID+","+ipos+","+posval;
context.write(new Text(jpos), new Text(mapval));
}
else
{
jpos = line[1];
String kpos = line[2];
mapval = matrixID+","+kpos+","+posval;
context.write(new Text(jpos), new Text(mapval));
}
}
}
public static class Red1 extends Reducer<Text,Text,Text,Text> {
public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {
ArrayList<Integer> MList = new ArrayList<>();
ArrayList<Integer> NList = new ArrayList<>();
for(Text val : values)
{
String[] line = val.toString().split(",");
String matrixID = line[0];
int ipos = Integer.valueOf(line[1]);
int eleval = Integer.valueOf(line[2]);
if(matrixID.equals("M"))
{
MList.add(ipos);
MList.add(eleval);
}
else
{
NList.add(ipos);
NList.add(eleval);
}
}
int Mlen = MList.size() , Nlen = NList.size();
for(int i = 0 ; i < Mlen ; i+=2)
{
for(int j = 0 ; j < Nlen ; j+=2)
{
String i_k_pos = "";
i_k_pos = String.valueOf(MList.get(i))+","+String.valueOf(NList.get(j));
String product = String.valueOf(MList.get(i+1) * NList.get(j+1));
context.write(new Text(i_k_pos) , new Text(product));
}
}
}
}
public static class Map2 extends Mapper<Object, Text, Text, Text>{
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] line = value.toString().split("\t");
context.write(new Text(line[0]) , new Text(line[1]));
}
}
public static class Red2 extends Reducer<Text,Text,Text,Text> {
public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {
int sum = 0;
for(Text val : values)
{
String product = val.toString();
sum += Integer.valueOf(product);
}
context.write(key , new Text(String.valueOf(sum)));
}
}
public static void main(String[] args) throws Exception {
Configuration conf1 = new Configuration();
Job job1 = new Job(conf1, "matrixMul");
job1.setJarByClass(matrixMul.class);
job1.setMapperClass(Map1.class);
job1.setReducerClass(Red1.class);
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job1, new Path("data/test.txt"));
FileOutputFormat.setOutputPath(job1, new Path("output/matrixMul_1"));
job1.waitForCompletion(true);
Configuration conf2 = new Configuration();
Job job2 = new Job(conf2, "matrixMul");
job2.setJarByClass(matrixMul.class);
job2.setMapperClass(Map2.class);
job2.setReducerClass(Red2.class);
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job2, new Path("output/matrixMul_1"));
FileOutputFormat.setOutputPath(job2, new Path("output/matrixMul_2"));
job2.waitForCompletion(true);
}
}