Modified: incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java?rev=708884&r1=708883&r2=708884&view=diff ============================================================================== --- incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java (original) +++ incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java Wed Oct 29 04:59:40 2008 @@ -1,214 +1,288 @@ -/** - * Copyright 2007 The Apache Software Foundation - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hama; - -import java.io.IOException; -import java.util.Iterator; - -import junit.extensions.TestSetup; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import org.apache.hama.io.VectorEntry; -import org.apache.log4j.Logger; - -/** - * Matrix test - */ -public class TestDenseMatrix extends TestCase { - static final Logger LOG = Logger.getLogger(TestDenseMatrix.class); - private static int SIZE = 10; - private static Matrix m1; - private static Matrix m2; - private final static String SAVE = "save"; - - public static Test suite() { - TestSetup setup = new TestSetup(new TestSuite(TestDenseMatrix.class)) { - protected void setUp() throws Exception { - HCluster hCluster = new HCluster(); - hCluster.setUp(); - - m1 = DenseMatrix.random(hCluster.getConf(), SIZE, SIZE); - m2 = DenseMatrix.random(hCluster.getConf(), SIZE, SIZE); - } - - protected void tearDown() { - try { - closeTest(); - } catch (IOException e) { - e.printStackTrace(); - } - } - }; - return setup; - } - - public static void closeTest() throws IOException { - m1.close(); - m2.close(); - } - - /** - * Column vector test. - * - * @param rand - * @throws IOException - */ - public void testGetColumn() throws IOException { - Vector v = m1.getColumn(0); - Iterator<VectorEntry> it = v.iterator(); - int x = 0; - while (it.hasNext()) { - assertEquals(m1.get(x, 0), it.next().getValue()); - x++; - } - } - - public void testGetSetAttribute() throws IOException { - m1.setRowAttribute(0, "row1"); - assertEquals(m1.getRowAttribute(0), "row1"); - assertEquals(m1.getRowAttribute(1), null); - - m1.setColumnAttribute(0, "column1"); - assertEquals(m1.getColumnAttribute(0), "column1"); - assertEquals(m1.getColumnAttribute(1), null); - } - - public void testSubMatrix() throws IOException { - SubMatrix a = m1.subMatrix(2, 4, 2, 4); - for (int i = 0; i < a.size(); i++) { - for (int j = 0; j < a.size(); j++) { - assertEquals(a.get(i, j), m1.get(i + 2, j + 2)); - } - } - - SubMatrix b = m2.subMatrix(0, 2, 0, 2); - SubMatrix c = a.mult(b); - - double[][] C = new double[3][3]; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - for (int k = 0; k < 3; k++) { - C[i][k] += m1.get(i + 2, j + 2) * m2.get(j, k); - } - } - } - - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - assertEquals(C[i][j], c.get(i, j)); - } - } - } - - /** - * Test matrices addition - * - * @throws IOException - */ - public void testMatrixAdd() throws IOException { - Matrix result = m1.add(m2); - - assertEquals(result.getRows(), SIZE); - assertEquals(result.getColumns(), SIZE); - - for (int i = 0; i < SIZE; i++) { - for (int j = 0; j < SIZE; j++) { - assertEquals(result.get(i, j), m1.get(i, j) + m2.get(i, j)); - } - } - } - - /** - * Test matrices multiplication - * - * @throws IOException - */ - public void testMatrixMult() throws IOException { - Matrix result = m1.mult(m2); - - assertEquals(result.getRows(), SIZE); - assertEquals(result.getColumns(), SIZE); - - verifyMultResult(m1, m2, result); - } - - public void testSetRow() throws IOException { - Vector v = new DenseVector(); - double[] entries = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; - - for (int i = 0; i < SIZE; i++) { - v.set(i, entries[i]); - } - - m1.setRow(SIZE + 1, v); - Iterator<VectorEntry> it = m1.getRow(SIZE + 1).iterator(); - - // We should remove the timestamp and row attribute from the vector - int i = 0; - while (it.hasNext()) { - assertEquals(entries[i], it.next().getValue()); - i++; - } - } - - public void testLoadSave() throws IOException { - m1.save(SAVE); - HCluster hCluster = new HCluster(); - DenseMatrix loadTest = new DenseMatrix(hCluster.conf, SAVE); - - for (int i = 0; i < loadTest.getRows(); i++) { - for (int j = 0; j < loadTest.getColumns(); j++) { - assertEquals(m1.get(i, j), loadTest.get(i, j)); - } - } - - assertEquals(loadTest.getName(), SAVE); - } - - /** - * Verifying multiplication result - * - * @param m1 - * @param m2 - * @param result - * @throws IOException - */ - private void verifyMultResult(Matrix m1, Matrix m2, Matrix result) - throws IOException { - double[][] C = new double[SIZE][SIZE]; - - for (int i = 0; i < SIZE; i++) { - for (int j = 0; j < SIZE; j++) { - for (int k = 0; k < SIZE; k++) { - C[i][k] += m1.get(i, j) * m2.get(j, k); - } - } - } - - for (int i = 0; i < SIZE; i++) { - for (int j = 0; j < SIZE; j++) { - assertEquals(String.valueOf(result.get(i, j)).substring(0, 14), String - .valueOf(C[i][j]).substring(0, 14)); - } - } - } -} +/** + * Copyright 2007 The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hama; + +import java.io.IOException; +import java.util.Iterator; + +import junit.extensions.TestSetup; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.hadoop.hbase.client.HBaseAdmin; +import org.apache.hama.io.VectorEntry; +import org.apache.log4j.Logger; + +/** + * Matrix test + */ +public class TestDenseMatrix extends TestCase { + static final Logger LOG = Logger.getLogger(TestDenseMatrix.class); + private static int SIZE = 10; + private static Matrix m1; + private static Matrix m2; + private final static String aliase1 = "matrix_aliase_A"; + private final static String aliase2 = "matrix_aliase_B"; + private static HamaConfiguration conf; + private static HBaseAdmin admin; + private static HamaAdmin hamaAdmin; + + public static Test suite() { + TestSetup setup = new TestSetup(new TestSuite(TestDenseMatrix.class)) { + protected void setUp() throws Exception { + HCluster hCluster = new HCluster(); + hCluster.setUp(); + + conf = hCluster.getConf(); + admin = new HBaseAdmin(conf); + hamaAdmin = new HamaAdminImpl(conf, admin); + + m1 = DenseMatrix.random(hCluster.getConf(), SIZE, SIZE); + m2 = DenseMatrix.random(hCluster.getConf(), SIZE, SIZE); + } + + protected void tearDown() { + try { + closeTest(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }; + return setup; + } + + public static void closeTest() throws IOException { + m1.close(); + m2.close(); + } + + /** + * Column vector test. + * + * @param rand + * @throws IOException + */ + public void testGetColumn() throws IOException { + Vector v = m1.getColumn(0); + Iterator<VectorEntry> it = v.iterator(); + int x = 0; + while (it.hasNext()) { + assertEquals(m1.get(x, 0), it.next().getValue()); + x++; + } + } + + public void testGetSetAttribute() throws IOException { + m1.setRowAttribute(0, "row1"); + assertEquals(m1.getRowAttribute(0), "row1"); + assertEquals(m1.getRowAttribute(1), null); + + m1.setColumnAttribute(0, "column1"); + assertEquals(m1.getColumnAttribute(0), "column1"); + assertEquals(m1.getColumnAttribute(1), null); + } + + public void testSubMatrix() throws IOException { + SubMatrix a = m1.subMatrix(2, 4, 2, 4); + for (int i = 0; i < a.size(); i++) { + for (int j = 0; j < a.size(); j++) { + assertEquals(a.get(i, j), m1.get(i + 2, j + 2)); + } + } + + SubMatrix b = m2.subMatrix(0, 2, 0, 2); + SubMatrix c = a.mult(b); + + double[][] C = new double[3][3]; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + for (int k = 0; k < 3; k++) { + C[i][k] += m1.get(i + 2, j + 2) * m2.get(j, k); + } + } + } + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + assertEquals(C[i][j], c.get(i, j)); + } + } + } + + /** + * Test matrices addition + * + * @throws IOException + */ + public void testMatrixAdd() throws IOException { + Matrix result = m1.add(m2); + + assertEquals(result.getRows(), SIZE); + assertEquals(result.getColumns(), SIZE); + + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + assertEquals(result.get(i, j), m1.get(i, j) + m2.get(i, j)); + } + } + } + + /** + * Test matrices multiplication + * + * @throws IOException + */ + public void testMatrixMult() throws IOException { + Matrix result = m1.mult(m2); + + assertEquals(result.getRows(), SIZE); + assertEquals(result.getColumns(), SIZE); + + verifyMultResult(m1, m2, result); + } + + public void testSetRow() throws IOException { + Vector v = new DenseVector(); + double[] entries = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; + + for (int i = 0; i < SIZE; i++) { + v.set(i, entries[i]); + } + + m1.setRow(SIZE + 1, v); + Iterator<VectorEntry> it = m1.getRow(SIZE + 1).iterator(); + + // We should remove the timestamp and row attribute from the vector + int i = 0; + while (it.hasNext()) { + assertEquals(entries[i], it.next().getValue()); + i++; + } + } + + public void testLoadSave() throws IOException { + String path1 = m1.getPath(); + // save m1 to aliase1 + m1.save(aliase1); + // load matrix m1 using aliase1 + DenseMatrix loadTest = new DenseMatrix(conf, aliase1, false); + + for (int i = 0; i < loadTest.getRows(); i++) { + for (int j = 0; j < loadTest.getColumns(); j++) { + assertEquals(m1.get(i, j), loadTest.get(i, j)); + } + } + + assertEquals(path1, loadTest.getPath()); + // close loadTest, it just disconnect to the table but didn't delete it. + loadTest.close(); + + // try to close m1 & load matrix m1 using aliase1 again. + m1.close(); + DenseMatrix loadTest2 = new DenseMatrix(conf, aliase1, false); + assertEquals(path1, loadTest2.getPath()); + // remove aliase1 + // because loadTest2 connect the aliase1, so we just remove aliase entry + // but didn't delete the table. + hamaAdmin.delete(aliase1); + assertEquals(true, admin.tableExists(path1)); + // close loadTest2, because it is the last one who reference table 'path1' + // it will do the gc! + loadTest2.close(); + assertEquals(false, admin.tableExists(path1)); + + // if we try to load non-existed matrix using aliase name, it should fail. + DenseMatrix loadTest3 = null; + try { + loadTest3 = new DenseMatrix(conf, aliase1, false); + fail("Try to load a non-existed matrix should fail!"); + } catch (IOException e) { + + } finally { + if(loadTest3!=null) + loadTest3.close(); + } + } + + public void testForceCreate() throws IOException { + String path2 = m2.getPath(); + // save m2 to aliase2 + m2.save(aliase2); + // load matrix m2 using aliase2 + DenseMatrix loadTest = new DenseMatrix(conf, aliase2, false); + + for (int i = 0; i < loadTest.getRows(); i++) { + for (int j = 0; j < loadTest.getColumns(); j++) { + assertEquals(m2.get(i, j), loadTest.get(i, j)); + } + } + + assertEquals(path2, loadTest.getPath()); + + // force to create matrix loadTest2 using aliasename 'aliase2' + DenseMatrix loadTest2 = new DenseMatrix(conf, aliase2, true); + String loadPath2 = loadTest2.getPath(); + assertFalse(path2.equals(loadPath2)); + assertEquals(loadPath2, hamaAdmin.getPath(aliase2)); + assertFalse(path2.equals(hamaAdmin.getPath(aliase2))); + + // try to close m2 & loadTest, it table will be deleted finally + m2.close(); + assertEquals(true, admin.tableExists(path2)); + loadTest.close(); + assertEquals(false, admin.tableExists(path2)); + + // remove 'aliase2' & close loadTest2 + loadTest2.close(); + assertEquals(true, admin.tableExists(loadPath2)); + hamaAdmin.delete(aliase2); + assertEquals(false, admin.tableExists(loadPath2)); + } + + /** + * Verifying multiplication result + * + * @param m1 + * @param m2 + * @param result + * @throws IOException + */ + private void verifyMultResult(Matrix m1, Matrix m2, Matrix result) + throws IOException { + double[][] C = new double[SIZE][SIZE]; + + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + for (int k = 0; k < SIZE; k++) { + C[i][k] += m1.get(i, j) * m2.get(j, k); + } + } + } + + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + assertEquals(String.valueOf(result.get(i, j)).substring(0, 14), String + .valueOf(C[i][j]).substring(0, 14)); + } + } + } +}
Modified: incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java?rev=708884&r1=708883&r2=708884&view=diff ============================================================================== --- incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java (original) +++ incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java Wed Oct 29 04:59:40 2008 @@ -1,147 +1,147 @@ -/** - * Copyright 2007 The Apache Software Foundation - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hama; - -import java.util.Iterator; - -import junit.extensions.TestSetup; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.hama.io.VectorEntry; - -public class TestDenseVector extends TestCase { - final static Log LOG = LogFactory.getLog(TestDenseVector.class.getName()); - - private static final double cosine = 0.6978227007909176; - private static final double norm1 = 12.0; - private static final double norm2 = 6.782329983125268; - private static double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } }; - private static Matrix m1; - private static Vector v1; - private static Vector v2; - - public static Test suite() { - TestSetup setup = new TestSetup(new TestSuite(TestDenseVector.class)) { - protected void setUp() throws Exception { - HCluster hCluster = new HCluster(); - hCluster.setUp(); - - m1 = new DenseMatrix(hCluster.getConf(), "vectorTest"); - - for (int i = 0; i < 2; i++) - for (int j = 0; j < 4; j++) - m1.set(i, j, values[i][j]); - - v1 = m1.getRow(0); - v2 = m1.getRow(1); - } - - protected void tearDown() { - LOG.info("tearDown()"); - } - }; - return setup; - } - - /** - * Test |a| dot |b| - */ - public void testDot() { - double cos = v1.dot(v2); - assertEquals(cos, cosine); - } - - public void testSubVector() { - int start = 2; - Vector subVector = v1.subVector(start, v1.size() - 1); - Iterator<VectorEntry> it = subVector.iterator(); - - int i = start; - while (it.hasNext()) { - assertEquals(v1.get(i), it.next().getValue()); - i++; - } - } - - /** - * Test norm one - */ - public void testNom1() { - double result = ((DenseVector) v1).getNorm1(); - assertEquals(norm1, result); - } - - /** - * Test norm two - */ - public void testNom2() { - double result = ((DenseVector) v1).getNorm2(); - assertEquals(norm2, result); - } - - /** - * Test scaling - */ - public void scalingTest() { - v2.scale(0.5); - - for (int i = 0; i < v2.size(); i++) { - assertEquals(values[1][i] * 0.5, v2.get(i)); - } - } - - /** - * Test get/set methods - */ - public void testGetSet() { - assertEquals(v1.get(0), values[0][0]); - } - - /** - * Test add() - */ - public void testAdd() { - v1.add(v2); - int i = 0; - Iterator<VectorEntry> it = v1.iterator(); - while (it.hasNext()) { - VectorEntry c = it.next(); - assertEquals(c.getValue(), values[0][i] + values[1][i]); - i++; - } - - v1.add(0.5, v2); - int j = 0; - Iterator<VectorEntry> itt = v1.iterator(); - while (itt.hasNext()) { - VectorEntry c = itt.next(); - assertEquals(c.getValue(), (values[0][j] + values[1][j]) + (0.5 * values[1][j])); - j++; - } - - double old = v1.get(0); - v1.add(0, norm1); - assertEquals(v1.get(0), old + norm1); - } -} +/** + * Copyright 2007 The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hama; + +import java.util.Iterator; + +import junit.extensions.TestSetup; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hama.io.VectorEntry; + +public class TestDenseVector extends TestCase { + final static Log LOG = LogFactory.getLog(TestDenseVector.class.getName()); + + private static final double cosine = 0.6978227007909176; + private static final double norm1 = 12.0; + private static final double norm2 = 6.782329983125268; + private static double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } }; + private static Matrix m1; + private static Vector v1; + private static Vector v2; + + public static Test suite() { + TestSetup setup = new TestSetup(new TestSuite(TestDenseVector.class)) { + protected void setUp() throws Exception { + HCluster hCluster = new HCluster(); + hCluster.setUp(); + + m1 = new DenseMatrix(hCluster.getConf()); + + for (int i = 0; i < 2; i++) + for (int j = 0; j < 4; j++) + m1.set(i, j, values[i][j]); + + v1 = m1.getRow(0); + v2 = m1.getRow(1); + } + + protected void tearDown() { + LOG.info("tearDown()"); + } + }; + return setup; + } + + /** + * Test |a| dot |b| + */ + public void testDot() { + double cos = v1.dot(v2); + assertEquals(cos, cosine); + } + + public void testSubVector() { + int start = 2; + Vector subVector = v1.subVector(start, v1.size() - 1); + Iterator<VectorEntry> it = subVector.iterator(); + + int i = start; + while (it.hasNext()) { + assertEquals(v1.get(i), it.next().getValue()); + i++; + } + } + + /** + * Test norm one + */ + public void testNom1() { + double result = ((DenseVector) v1).getNorm1(); + assertEquals(norm1, result); + } + + /** + * Test norm two + */ + public void testNom2() { + double result = ((DenseVector) v1).getNorm2(); + assertEquals(norm2, result); + } + + /** + * Test scaling + */ + public void scalingTest() { + v2.scale(0.5); + + for (int i = 0; i < v2.size(); i++) { + assertEquals(values[1][i] * 0.5, v2.get(i)); + } + } + + /** + * Test get/set methods + */ + public void testGetSet() { + assertEquals(v1.get(0), values[0][0]); + } + + /** + * Test add() + */ + public void testAdd() { + v1.add(v2); + int i = 0; + Iterator<VectorEntry> it = v1.iterator(); + while (it.hasNext()) { + VectorEntry c = it.next(); + assertEquals(c.getValue(), values[0][i] + values[1][i]); + i++; + } + + v1.add(0.5, v2); + int j = 0; + Iterator<VectorEntry> itt = v1.iterator(); + while (itt.hasNext()) { + VectorEntry c = itt.next(); + assertEquals(c.getValue(), (values[0][j] + values[1][j]) + (0.5 * values[1][j])); + j++; + } + + double old = v1.get(0); + v1.add(0, norm1); + assertEquals(v1.get(0), old + norm1); + } +} Modified: incubator/hama/trunk/src/test/org/apache/hama/mapred/TestMatrixMapReduce.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/mapred/TestMatrixMapReduce.java?rev=708884&r1=708883&r2=708884&view=diff ============================================================================== --- incubator/hama/trunk/src/test/org/apache/hama/mapred/TestMatrixMapReduce.java (original) +++ incubator/hama/trunk/src/test/org/apache/hama/mapred/TestMatrixMapReduce.java Wed Oct 29 04:59:40 2008 @@ -1,81 +1,84 @@ -/** - * Copyright 2007 The Apache Software Foundation - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hama.mapred; - -import java.io.IOException; - -import org.apache.hadoop.io.IntWritable; -import org.apache.hadoop.mapred.JobClient; -import org.apache.hadoop.mapred.JobConf; -import org.apache.hama.DenseMatrix; -import org.apache.hama.HCluster; -import org.apache.hama.Matrix; -import org.apache.hama.algebra.Add1DLayoutMap; -import org.apache.hama.algebra.Add1DLayoutReduce; -import org.apache.hama.io.VectorWritable; -import org.apache.log4j.Logger; - -/** - * Test Matrix Map/Reduce job - */ -public class TestMatrixMapReduce extends HCluster { - static final Logger LOG = Logger.getLogger(TestMatrixMapReduce.class); - private String A = "matrixA"; - private String B = "matrixB"; - private String output = "output"; - - /** constructor */ - public TestMatrixMapReduce() { - super(); - } - - public void testMatrixMapReduce() throws IOException { - Matrix matrixA = new DenseMatrix(conf, A); - matrixA.set(0, 0, 1); - matrixA.set(0, 1, 0); - matrixA.setDimension(1, 2); - - Matrix matrixB = new DenseMatrix(conf, B); - matrixB.set(0, 0, 1); - matrixB.set(0, 1, 1); - matrixB.setDimension(1, 2); - - miniMRJob(); - } - - public void miniMRJob() throws IOException { - Matrix c = new DenseMatrix(conf, output); - - JobConf jobConf = new JobConf(conf, TestMatrixMapReduce.class); - jobConf.setJobName("test MR job"); - - Add1DLayoutMap.initJob(A, B, Add1DLayoutMap.class, IntWritable.class, - VectorWritable.class, jobConf); - MatrixReduce.initJob(output, Add1DLayoutReduce.class, jobConf); - - jobConf.setNumMapTasks(2); - jobConf.setNumReduceTasks(2); - - JobClient.runJob(jobConf); - - assertEquals(c.get(0, 0), 2.0); - assertEquals(c.get(0, 1), 1.0); - } -} +/** + * Copyright 2007 The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hama.mapred; + +import java.io.IOException; + +import org.apache.hadoop.io.IntWritable; +import org.apache.hadoop.mapred.JobClient; +import org.apache.hadoop.mapred.JobConf; +import org.apache.hama.DenseMatrix; +import org.apache.hama.HCluster; +import org.apache.hama.Matrix; +import org.apache.hama.algebra.Add1DLayoutMap; +import org.apache.hama.algebra.Add1DLayoutReduce; +import org.apache.hama.io.VectorWritable; +import org.apache.log4j.Logger; + +/** + * Test Matrix Map/Reduce job + */ +public class TestMatrixMapReduce extends HCluster { + static final Logger LOG = Logger.getLogger(TestMatrixMapReduce.class); + String pathA; + String pathB; + String output; + + /** constructor */ + public TestMatrixMapReduce() { + super(); + } + + public void testMatrixMapReduce() throws IOException { + Matrix matrixA = new DenseMatrix(conf); + pathA = matrixA.getPath(); + matrixA.set(0, 0, 1); + matrixA.set(0, 1, 0); + matrixA.setDimension(1, 2); + + Matrix matrixB = new DenseMatrix(conf); + pathB = matrixB.getPath(); + matrixB.set(0, 0, 1); + matrixB.set(0, 1, 1); + matrixB.setDimension(1, 2); + + miniMRJob(); + } + + private void miniMRJob() throws IOException { + Matrix c = new DenseMatrix(conf); + output = c.getPath(); + + JobConf jobConf = new JobConf(conf, TestMatrixMapReduce.class); + jobConf.setJobName("test MR job"); + + Add1DLayoutMap.initJob(pathA, pathB, Add1DLayoutMap.class, IntWritable.class, + VectorWritable.class, jobConf); + MatrixReduce.initJob(output, Add1DLayoutReduce.class, jobConf); + + jobConf.setNumMapTasks(2); + jobConf.setNumReduceTasks(2); + + JobClient.runJob(jobConf); + + assertEquals(c.get(0, 0), 2.0); + assertEquals(c.get(0, 1), 1.0); + } +}
