我正在运行在http://www.norstad.org/matrix-multiply/index.html找到的MapReduce矩阵乘法程序。
我发现当输入矩阵中有0时,此实现无法正常工作。但是我不明白为什么,以及如何修改程序使其正常工作?
MapReduce操作完成,但输出始终是所有元素为0的矩阵。
我的输入矩阵A和B是:
以下内容摘自该作业的日志文件:
矩阵B的映射输出:
注意,矩阵A的Map不会从输入文件中读取零值。 注意:两个输入文件都以序列文件的形式存储在HDFS中,输出也是序列文件。有人能解释一下问题出在哪里吗?谢谢! Mapper类的代码如下:
如果出现语法错误,那只是因为我复制粘贴了代码并在这里进行了修改,所以我可能会忽略一些东西。
我需要帮助理解Map函数的逻辑,它不能从输入文件中读取0,有人能告诉我为什么吗?
我的输入矩阵A和B是:
Matrix A Matrix B
0 0 0 6 7 4
0 1 6 9 1 3
7 8 9 7 6 2
输出矩阵:
0 0 0
0 0 0
0 0 0
以下内容摘自该作业的日志文件:
矩阵B的映射输出:
##### Map setup: matrixA = false for hdfs://localhost/user/hadoop-user/B
strategy = 4
R1 = 4
I = 3
K = 3
J = 3
IB = 3
KB = 3
JB = 3
##### Map input: (0,0) 6
##### Map output: (0,0,0,1) (0,0,6)
##### Map input: (0,1) 7
##### Map output: (0,0,0,1) (0,1,7)
##### Map input: (0,2) 4
##### Map output: (0,0,0,1) (0,2,4)
##### Map input: (1,0) 9
##### Map output: (0,0,0,1) (1,0,9)
##### Map input: (1,1) 1
##### Map output: (0,0,0,1) (1,1,1)
##### Map input: (1,2) 3
##### Map output: (0,0,0,1) (1,2,3)
##### Map input: (2,0) 7
##### Map output: (0,0,0,1) (2,0,7)
##### Map input: (2,1) 6
##### Map output: (0,0,0,1) (2,1,6)
##### Map input: (2,2) 2
##### Map output: (0,0,0,1) (2,2,2)
矩阵A的映射输出:
##### Map setup: matrixA = true for hdfs://localhost/user/hadoop-user/A
strategy = 4
R1 = 4
I = 3
K = 3
J = 3
IB = 3
KB = 3
JB = 3
##### Map input: (1,1) 1
##### Map output: (0,0,0,0) (1,1,1)
##### Map input: (1,2) 6
##### Map output: (0,0,0,0) (1,2,6)
##### Map input: (2,0) 7
##### Map output: (0,0,0,0) (2,0,7)
##### Map input: (2,1) 8
##### Map output: (0,0,0,0) (2,1,8)
##### Map input: (2,2) 9
##### Map output: (0,0,0,0) (2,2,9)
注意,矩阵A的Map不会从输入文件中读取零值。 注意:两个输入文件都以序列文件的形式存储在HDFS中,输出也是序列文件。有人能解释一下问题出在哪里吗?谢谢! Mapper类的代码如下:
/** The job 1 mapper class. */
private static class Job1Mapper
extends Mapper<IndexPair, IntWritable, Key, Value>
{
private Path path;
private boolean matrixA;
private Key key = new Key();
private Value value = new Value();
public void setup (Context context) {
init(context);
FileSplit split = (FileSplit)context.getInputSplit();
path = split.getPath();
matrixA = path.toString().startsWith(inputPathA);
if (DEBUG) {
System.out.println("##### Map setup: matrixA = " + matrixA + " for " + path);
System.out.println(" strategy = " + strategy);
System.out.println(" R1 = " + R1);
System.out.println(" I = " + I);
System.out.println(" K = " + K);
System.out.println(" J = " + J);
System.out.println(" IB = " + IB);
System.out.println(" KB = " + KB);
System.out.println(" JB = " + JB);
}
}
private void printMapInput (IndexPair indexPair, IntWritable el) {
System.out.println("##### Map input: (" + indexPair.index1 + "," +
indexPair.index2 + ") " + el.get());
}
private void printMapOutput (Key key, Value value) {
System.out.println("##### Map output: (" + key.index1 + "," +
key.index2 + "," + key.index3 + "," + key.m + ") (" +
value.index1 + "," + value.index2 + "," + value.v + ") ");
}
private void badIndex (int index, int dim, String msg) {
System.err.println("Invalid " + msg + " in " + path + ": " + index + " " + dim);
System.exit(1);
}
public void map (IndexPair indexPair, IntWritable el, Context context)
throws IOException, InterruptedException
{
if (DEBUG) printMapInput(indexPair, el);
int i = 0;
int k = 0;
int j = 0;
if (matrixA) {
i = indexPair.index1;
if (i < 0 || i >= I) badIndex(i, I, "A row index");
k = indexPair.index2;
if (k < 0 || k >= K) badIndex(k, K, "A column index");
} else {
k = indexPair.index1;
if (k < 0 || k >= K) badIndex(k, K, "B row index");
j = indexPair.index2;
if (j < 0 || j >= J) badIndex(j, J, "B column index");
}
value.v = el.get();
if (matrixA) {
key.index1 = i/IB;
key.index3 = k/KB;
key.m = 0;
value.index1 = i % IB;
value.index2 = k % KB;
for (int jb = 0; jb < NJB; jb++) {
key.index2 = jb;
context.write(key, value);
if (DEBUG) printMapOutput(key, value);
}
} else {
key.index2 = j/JB;
key.index3 = k/KB;
key.m = 1;
value.index1 = k % KB;
value.index2 = j % JB;
for (int ib = 0; ib < NIB; ib++) {
key.index1 = ib;
context.write(key, value);
if (DEBUG) printMapOutput(key, value);
}
}
}
}
如果出现语法错误,那只是因为我复制粘贴了代码并在这里进行了修改,所以我可能会忽略一些东西。
我需要帮助理解Map函数的逻辑,它不能从输入文件中读取0,有人能告诉我为什么吗?