I have a custom class MW. MW gets 2 matrices-(ke matrix and val matrix).
And I am trying to sumup all the matrices coming into reducer.
So I need to first parse my string and I stored them into 2 double array. I am geting all the ke matrix and val matrix in reducer.
But I am not able to sumup.
Any suggestion.
Inorder to get the sum outside the forloop,i declared them as static.
public class Reducer extends Reducer<IntWritable, MW, Text, Text> { static double[][] key; static double[][] value; public void reduce(IntWritable keys, Iterable<MW> values, Context context) throws IOException, InterruptedException { for (MW c : values) { String data = c.toString(); data = data.trim(); String[] parts = data.split("#"); String part1 = parts[0]; String part2 = parts[1]; /* * Parse key */ String[] keyrows = part1.split(","); String[][] keymatrix = new String[keyrows.length][]; int keyr = 0; for (String keyrow : keyrows) { keymatrix[keyr++] = keyrow.split("\|"); } double[][] ke = new double[keymatrix.length][keymatrix[0].length]; for (int i = 0; i<keymatrix.length; i++) { for (int j = 0; j<keymatrix[0].length; j++) { ke[i][j] = Double.valueOf(keymatrix[i][j]); } } key = new double[ke.length][ke[0].length]; for(int sumi = 0;sumi<ke.length;sumi++){ for(int sumj=0;sumj<ke[0].length;sumj++){ key[sumi][sumj] += ke[sumi][sumj]; } } /*Parsing value */ String[] valuerows = part2.split(","); String[][] valuematrix = new String[valuerows.length][]; int valr = 0; for (String valuerow : valuerows) { valuematrix[valr++] = valuerow.split("\|"); } double[][] val = new double[valuematrix.length][valuematrix[0].length]; for (int i = 0; i<valuematrix.length; i++) { for (int j = 0; j<valuematrix[0].length; j++) { val[i][j] = Double.valueOf(valuematrix[i][j]); } } //calculating sum for value value = new double[val.length][val[0].length]; for(int sumi = 0;sumi<val.length;sumi++){ for(int sumj=0;sumj<val[0].length;sumj++){ value[sumi][sumj] += val[sumi][sumj]; } } } System.out.println("sum 1"); for(int diai=0;diai<key.length;diai++){ for(int diaj=0;diaj<key[0].length;diaj++){ System.out.print(key[diai][diaj]+" "); } System.out.println(""); } System.out.println("sum 2"); for(int diai=0;diai<value.length;diai++){ for(int diaj=0;diaj<value[0].length;diaj++){ System.out.print(value[diai][diaj]+" "); } System.out.println(""); }
UPDATE I think the problem is with in line
key = new double[ke.length][ke[0].length];
and
value = new double[val.length][val[0].length];
before summing I am rebuilding the matrix key and value inside the loop.
It should build it once before the loop and then add to it. But to do that I should do
double[][] key = new double[ke.length][ke[0].length]; double[][] value = new double[val.length][val[0].length];
before
for (MW c : values) {
but How will I get the dimensions
outside the for loop?