Return-Path: Delivered-To: apmail-incubator-hama-commits-archive@locus.apache.org Received: (qmail 85889 invoked from network); 25 Aug 2008 13:27:28 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 25 Aug 2008 13:27:28 -0000 Received: (qmail 5656 invoked by uid 500); 25 Aug 2008 13:27:27 -0000 Delivered-To: apmail-incubator-hama-commits-archive@incubator.apache.org Received: (qmail 5646 invoked by uid 500); 25 Aug 2008 13:27:27 -0000 Mailing-List: contact hama-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: hama-dev@ Delivered-To: mailing list hama-commits@incubator.apache.org Received: (qmail 5635 invoked by uid 99); 25 Aug 2008 13:27:27 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Aug 2008 06:27:27 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Aug 2008 13:26:36 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 7466A2388970; Mon, 25 Aug 2008 06:27:06 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r688720 [1/2] - in /incubator/hama/trunk: ./ src/examples/org/apache/hama/examples/ src/java/org/apache/hama/ src/java/org/apache/hama/algebra/ src/java/org/apache/hama/io/ src/java/org/apache/hama/mapred/ src/test/org/apache/hama/ src/test... Date: Mon, 25 Aug 2008 13:27:05 -0000 To: hama-commits@incubator.apache.org From: edwardyoon@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080825132706.7466A2388970@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: edwardyoon Date: Mon Aug 25 06:27:04 2008 New Revision: 688720 URL: http://svn.apache.org/viewvc?rev=688720&view=rev Log: Rename MatrixInterface to Matrix. Added: incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java incubator/hama/trunk/src/java/org/apache/hama/mapred/DenseMap.java Modified: incubator/hama/trunk/CHANGES.txt incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java incubator/hama/trunk/src/java/org/apache/hama/Matrix.java incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java incubator/hama/trunk/src/java/org/apache/hama/Vector.java incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionMap.java incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionReduce.java incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormatBase.java incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java incubator/hama/trunk/src/test/org/apache/hama/TestMatrix.java incubator/hama/trunk/src/test/org/apache/hama/TestVector.java incubator/hama/trunk/src/test/org/apache/hama/mapred/TestMatrixMapReduce.java Modified: incubator/hama/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/CHANGES.txt (original) +++ incubator/hama/trunk/CHANGES.txt Mon Aug 25 06:27:04 2008 @@ -19,6 +19,7 @@ IMPROVEMENTS + HAMA-40: Rename MatrixInterface to Matrix (edwardyoon) HAMA-14: Using Java 6 (edwardyoon) HAMA-22: Add IRC channel information to website (edwardyoon) HAMA-41: Add some tests (edwardyoon) Modified: incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java (original) +++ incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java Mon Aug 25 06:27:04 2008 @@ -1,67 +1,68 @@ -/** - * 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.examples; - -import org.apache.hama.HamaConfiguration; -import org.apache.hama.Matrix; - -public class MatrixAddition { - - public static void main(String[] args) { - if (args.length < 2) { - System.out.println("addition "); - System.exit(-1); - } - - int row = Integer.parseInt(args[0]); - int column = Integer.parseInt(args[1]); - - HamaConfiguration conf = new HamaConfiguration(); - - Matrix a = Matrix.random(conf, row, column); - Matrix b = Matrix.random(conf, row, column); - - Matrix c = a.add(b); - - System.out.println("\nMatrix A"); - System.out.println("----------------------"); - for(int i = 0; i < row; i++) { - for(int j = 0; j < row; j++) { - System.out.println(a.get(i, j)); - } - } - - System.out.println("\nMatrix B"); - System.out.println("----------------------"); - for(int i = 0; i < row; i++) { - for(int j = 0; j < row; j++) { - System.out.println(b.get(i, j)); - } - } - - System.out.println("\nC = A + B"); - System.out.println("----------------------"); - for(int i = 0; i < row; i++) { - for(int j = 0; j < row; j++) { - System.out.println(c.get(i, j)); - } - } - } -} +/** + * 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.examples; + +import org.apache.hama.DenseMatrix; +import org.apache.hama.HamaConfiguration; +import org.apache.hama.Matrix; + +public class MatrixAddition { + + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("addition "); + System.exit(-1); + } + + int row = Integer.parseInt(args[0]); + int column = Integer.parseInt(args[1]); + + HamaConfiguration conf = new HamaConfiguration(); + + Matrix a = DenseMatrix.random(conf, row, column); + Matrix b = DenseMatrix.random(conf, row, column); + + Matrix c = a.add(b); + + System.out.println("\nMatrix A"); + System.out.println("----------------------"); + for(int i = 0; i < row; i++) { + for(int j = 0; j < row; j++) { + System.out.println(a.get(i, j)); + } + } + + System.out.println("\nMatrix B"); + System.out.println("----------------------"); + for(int i = 0; i < row; i++) { + for(int j = 0; j < row; j++) { + System.out.println(b.get(i, j)); + } + } + + System.out.println("\nC = A + B"); + System.out.println("----------------------"); + for(int i = 0; i < row; i++) { + for(int j = 0; j < row; j++) { + System.out.println(c.get(i, j)); + } + } + } +} Modified: incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java (original) +++ incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java Mon Aug 25 06:27:04 2008 @@ -1,172 +1,153 @@ -/** - * 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 org.apache.hadoop.hbase.HColumnDescriptor; -import org.apache.hadoop.hbase.HTableDescriptor; -import org.apache.hadoop.hbase.MasterNotRunningException; -import org.apache.hadoop.hbase.client.HBaseAdmin; -import org.apache.hadoop.hbase.client.HTable; -import org.apache.hadoop.hbase.io.BatchUpdate; -import org.apache.hadoop.hbase.io.Cell; -import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hadoop.io.Text; -import org.apache.hama.util.Numeric; -import org.apache.log4j.Logger; - -/** - * Methods of the matrix classes - */ -public abstract class AbstractMatrix implements MatrixInterface { - static final Logger LOG = Logger.getLogger(AbstractMatrix.class); - - /** Hama Configuration */ - protected HamaConfiguration config; - /** Hbase admin object */ - protected HBaseAdmin admin; - /** The name of Matrix */ - protected String matrixName; - /** Hbase table object */ - protected HTable table; - /** Matrix attribute description */ - protected HTableDescriptor tableDesc; - - /** - * Sets the job configuration - * - * @param conf configuration object - */ - public void setConfiguration(HamaConfiguration conf) { - config = (HamaConfiguration) conf; - try { - admin = new HBaseAdmin(config); - } catch (MasterNotRunningException e) { - LOG.error(e, e); - } - } - - /** - * Create matrix space - */ - protected void create() { - try { - tableDesc.addFamily(new HColumnDescriptor(Constants.METADATA)); - LOG.info("Initializing the matrix storage."); - admin.createTable(tableDesc); - } catch (IOException e) { - LOG.error(e, e); - } - } - - /** {@inheritDoc} */ - public double get(int i, int j) { - Cell c; - double result = -1; - try { - c = table - .get(Bytes.toBytes(String.valueOf(i)), Numeric.getColumnIndex(j)); - if (c != null) { - result = Numeric.bytesToDouble(c.getValue()); - } - } catch (IOException e) { - LOG.error(e, e); - } - return result; - } - - /** {@inheritDoc} */ - public Vector getRow(int row) { - try { - return new Vector(row, table.getRow(String.valueOf(row))); - } catch (IOException e) { - LOG.error(e, e); - } - return null; - } - - public Vector getRow(byte[] row) { - try { - return new Vector(Numeric.bytesToInt(row), table.getRow(row)); - } catch (IOException e) { - LOG.error(e, e); - } - return null; - } - - /** {@inheritDoc} */ - public int getRows() { - Cell rows = null; - try { - rows = table.get(Constants.METADATA, Constants.METADATA_ROWS); - } catch (IOException e) { - LOG.error(e, e); - } - - return Bytes.toInt(rows.getValue()); - } - - /** {@inheritDoc} */ - public int getColumns() { - Cell columns = null; - try { - columns = table.get(Constants.METADATA, Constants.METADATA_COLUMNS); - } catch (IOException e) { - LOG.error(e, e); - } - return Bytes.toInt(columns.getValue()); - } - - /** {@inheritDoc} */ - public void set(int i, int j, double value) { - BatchUpdate b = new BatchUpdate(new Text(String.valueOf(i))); - b.put(new Text(Constants.COLUMN + String.valueOf(j)), Numeric - .doubleToBytes(value)); - try { - table.commit(b); - } catch (IOException e) { - LOG.error(e, e); - } - } - - /** {@inheritDoc} */ - public void add(int i, int j, double value) { - // TODO Auto-generated method stub - } - - /** {@inheritDoc} */ - public void setDimension(int rows, int columns) { - BatchUpdate b = new BatchUpdate(Constants.METADATA); - b.put(Constants.METADATA_ROWS, Bytes.toBytes(rows)); - b.put(Constants.METADATA_COLUMNS, Bytes.toBytes(columns)); - - try { - table.commit(b); - } catch (IOException e) { - LOG.error(e, e); - } - } - - /** {@inheritDoc} */ - public String getName() { - return (matrixName != null) ? matrixName.toString() : null; - } -} +/** + * 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 org.apache.hadoop.hbase.HColumnDescriptor; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.MasterNotRunningException; +import org.apache.hadoop.hbase.client.HBaseAdmin; +import org.apache.hadoop.hbase.client.HTable; +import org.apache.hadoop.hbase.io.BatchUpdate; +import org.apache.hadoop.hbase.io.Cell; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.io.Text; +import org.apache.hama.util.Numeric; +import org.apache.log4j.Logger; + +/** + * Methods of the matrix classes + */ +public abstract class AbstractMatrix implements Matrix { + static final Logger LOG = Logger.getLogger(AbstractMatrix.class); + + /** Hama Configuration */ + protected HamaConfiguration config; + /** Hbase admin object */ + protected HBaseAdmin admin; + /** The name of Matrix */ + protected String matrixName; + /** Hbase table object */ + protected HTable table; + /** Matrix attribute description */ + protected HTableDescriptor tableDesc; + + /** + * Sets the job configuration + * + * @param conf configuration object + */ + public void setConfiguration(HamaConfiguration conf) { + config = (HamaConfiguration) conf; + try { + admin = new HBaseAdmin(config); + } catch (MasterNotRunningException e) { + LOG.error(e, e); + } + } + + /** + * Create matrix space + */ + protected void create() { + try { + tableDesc.addFamily(new HColumnDescriptor(Constants.METADATA)); + LOG.info("Initializing the matrix storage."); + admin.createTable(tableDesc); + } catch (IOException e) { + LOG.error(e, e); + } + } + + /** {@inheritDoc} */ + public double get(int i, int j) { + Cell c; + double result = -1; + try { + c = table + .get(Bytes.toBytes(String.valueOf(i)), Numeric.getColumnIndex(j)); + if (c != null) { + result = Numeric.bytesToDouble(c.getValue()); + } + } catch (IOException e) { + LOG.error(e, e); + } + return result; + } + + /** {@inheritDoc} */ + public int getRows() { + Cell rows = null; + try { + rows = table.get(Constants.METADATA, Constants.METADATA_ROWS); + } catch (IOException e) { + LOG.error(e, e); + } + + return Bytes.toInt(rows.getValue()); + } + + /** {@inheritDoc} */ + public int getColumns() { + Cell columns = null; + try { + columns = table.get(Constants.METADATA, Constants.METADATA_COLUMNS); + } catch (IOException e) { + LOG.error(e, e); + } + return Bytes.toInt(columns.getValue()); + } + + /** {@inheritDoc} */ + public void set(int i, int j, double value) { + BatchUpdate b = new BatchUpdate(new Text(String.valueOf(i))); + b.put(new Text(Constants.COLUMN + String.valueOf(j)), Numeric + .doubleToBytes(value)); + try { + table.commit(b); + } catch (IOException e) { + LOG.error(e, e); + } + } + + /** {@inheritDoc} */ + public void add(int i, int j, double value) { + // TODO Auto-generated method stub + } + + /** {@inheritDoc} */ + public void setDimension(int rows, int columns) { + BatchUpdate b = new BatchUpdate(Constants.METADATA); + b.put(Constants.METADATA_ROWS, Bytes.toBytes(rows)); + b.put(Constants.METADATA_COLUMNS, Bytes.toBytes(columns)); + + try { + table.commit(b); + } catch (IOException e) { + LOG.error(e, e); + } + } + + /** {@inheritDoc} */ + public String getName() { + return (matrixName != null) ? matrixName : null; + } +} Added: incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java?rev=688720&view=auto ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java (added) +++ incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java Mon Aug 25 06:27:04 2008 @@ -0,0 +1,185 @@ +/** + * 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 org.apache.hadoop.hbase.HColumnDescriptor; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.client.HTable; +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.mapred.JobClient; +import org.apache.hadoop.mapred.JobConf; +import org.apache.hama.algebra.AdditionMap; +import org.apache.hama.algebra.AdditionReduce; +import org.apache.hama.mapred.DenseMap; +import org.apache.hama.mapred.MatrixReduce; +import org.apache.hama.util.RandomVariable; + +public class DenseMatrix extends AbstractMatrix implements Matrix { + + /** + * Construct + * + * @param conf configuration object + */ + public DenseMatrix(HamaConfiguration conf) { + setConfiguration(conf); + } + + /** + * Construct an matrix + * + * @param conf configuration object + * @param matrixName the name of the matrix + */ + public DenseMatrix(HamaConfiguration conf, String matrixName) { + try { + setConfiguration(conf); + this.matrixName = matrixName; + + if (!admin.tableExists(matrixName)) { + tableDesc = new HTableDescriptor(matrixName); + tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN)); + create(); + } + + table = new HTable(config, matrixName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Construct an m-by-n constant matrix. + * + * @param conf configuration object + * @param m the number of rows. + * @param n the number of columns. + * @param s fill the matrix with this scalar value. + */ + public DenseMatrix(HamaConfiguration conf, int m, int n, double s) { + try { + setConfiguration(conf); + matrixName = RandomVariable.randMatrixName(); + + if (!admin.tableExists(matrixName)) { + tableDesc = new HTableDescriptor(matrixName); + tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN)); + create(); + } + + table = new HTable(config, matrixName); + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + set(i, j, s); + } + } + + setDimension(m, n); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Generate matrix with random elements + * + * @param conf configuration object + * @param m the number of rows. + * @param n the number of columns. + * @return an m-by-n matrix with uniformly distributed random elements. + */ + public static Matrix random(HamaConfiguration conf, int m, int n) { + String name = RandomVariable.randMatrixName(); + Matrix rand = new DenseMatrix(conf, name); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + rand.set(i, j, RandomVariable.rand()); + } + } + + rand.setDimension(m, n); + LOG.info("Create the " + m + " * " + n + " random matrix : " + name); + return rand; + } + + public Matrix add(Matrix B) { + String output = RandomVariable.randMatrixName(); + Matrix C = new DenseMatrix(config, output); + + JobConf jobConf = new JobConf(config); + jobConf.setJobName("addition MR job"); + + DenseMap.initJob(this.getName(), B.getName(), AdditionMap.class, + ImmutableBytesWritable.class, DenseVector.class, jobConf); + MatrixReduce.initJob(C.getName(), AdditionReduce.class, jobConf); + + try { + JobClient.runJob(jobConf); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return C; + } + + public Matrix add(double alpha, Matrix B) { + // TODO Auto-generated method stub + return null; + } + + public DenseVector getRow(int row) { + try { + return new DenseVector(row, table.getRow(String.valueOf(row))); + } catch (IOException e) { + LOG.error(e, e); + } + return null; + } + + public Matrix mult(Matrix B) { + // TODO Auto-generated method stub + return null; + } + + public Matrix multAdd(double alpha, Matrix B, Matrix C) { + // TODO Auto-generated method stub + return null; + } + + public double norm(Norm type) { + // TODO Auto-generated method stub + return 0; + } + + public Matrix set(double alpha, Matrix B) { + // TODO Auto-generated method stub + return null; + } + + public Matrix set(Matrix B) { + // TODO Auto-generated method stub + return null; + } + +} Added: incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java?rev=688720&view=auto ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java (added) +++ incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java Mon Aug 25 06:27:04 2008 @@ -0,0 +1,154 @@ +package org.apache.hama; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.apache.hadoop.hbase.io.Cell; +import org.apache.hadoop.hbase.io.HbaseMapWritable; +import org.apache.hadoop.hbase.io.RowResult; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hama.io.VectorWritable; +import org.apache.hama.util.Numeric; +import org.apache.log4j.Logger; + +public class DenseVector extends VectorWritable implements Vector { + static final Logger LOG = Logger.getLogger(Vector.class); + + public DenseVector() { + this(null, new HbaseMapWritable()); + } + + public DenseVector(final byte[] rowKey, final HbaseMapWritable m) { + this.row = row; + this.cells = m; + } + + public DenseVector(int row, RowResult rowResult) { + this.cells = new HbaseMapWritable(); + this.row = Numeric.intToBytes(row); + for (Map.Entry f : rowResult.entrySet()) { + this.cells.put(f.getKey(), f.getValue()); + } + } + + /** + * Get the row for this Vector + */ + public byte[] getRow() { + return row; + } + + public HbaseMapWritable getCells() { + return cells; + } + + public void add(int index, double value) { + // TODO Auto-generated method stub + + } + + public Vector add(double alpha, Vector v) { + // TODO Auto-generated method stub + return null; + } + + public Vector add(Vector v2) { + HbaseMapWritable trunk = new HbaseMapWritable(); + for (int i = 0; i < this.size(); i++) { + double value = (this.get(i) + v2.get(i)); + Cell cValue = new Cell(String.valueOf(value), System.currentTimeMillis()); + trunk.put(Bytes.toBytes("column:" + i), cValue); + } + + return new DenseVector(row, trunk); + } + + public double dot(Vector v) { + double cosine = 0.0; + double q_i, d_i; + for (int i = 0; i < Math.min(this.size(), v.size()); i++) { + q_i = v.get(i); + d_i = this.get(i); + cosine += q_i * d_i; + } + return cosine / (this.getNorm2() * ((DenseVector) v).getNorm2()); + } + + public Vector scale(double alpha) { + Set keySet = cells.keySet(); + Iterator it = keySet.iterator(); + + while (it.hasNext()) { + byte[] key = it.next(); + double oValue = Numeric.bytesToDouble(get(key).getValue()); + double nValue = oValue * alpha; + Cell cValue = new Cell(String.valueOf(nValue), System.currentTimeMillis()); + cells.put(key, cValue); + } + + return this; + } + + public double get(int index) { + return Numeric.bytesToDouble(cells.get(Numeric.getColumnIndex(index)) + .getValue()); + } + + public double norm(Norm type) { + if (type == Norm.One) + return getNorm1(); + else if (type == Norm.Two) + return getNorm2(); + else if (type == Norm.TwoRobust) + return getNorm2Robust(); + else + return getNormInf(); + } + + public void set(int index, double value) { + Cell cValue = new Cell(String.valueOf(value), System.currentTimeMillis()); + cells.put(Numeric.getColumnIndex(index), cValue); + } + + public DenseVector set(Vector v) { + return new DenseVector(((DenseVector) v).getRow(), ((DenseVector) v).getCells()); + } + + public double getNorm1() { + double sum = 0.0; + + Set keySet = cells.keySet(); + Iterator it = keySet.iterator(); + + while (it.hasNext()) { + sum += Numeric.bytesToDouble(get(it.next()).getValue()); + } + + return sum; + } + + public double getNorm2() { + double square_sum = 0.0; + + Set keySet = cells.keySet(); + Iterator it = keySet.iterator(); + + while (it.hasNext()) { + double value = Numeric.bytesToDouble(get(it.next()).getValue()); + square_sum += value * value; + } + + return Math.sqrt(square_sum); + } + + public double getNorm2Robust() { + // TODO Auto-generated method stub + return 0; + } + + public double getNormInf() { + // TODO Auto-generated method stub + return 0; + } +} \ No newline at end of file Modified: incubator/hama/trunk/src/java/org/apache/hama/Matrix.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/Matrix.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/Matrix.java (original) +++ incubator/hama/trunk/src/java/org/apache/hama/Matrix.java Mon Aug 25 06:27:04 2008 @@ -1,178 +1,157 @@ -/** - * 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 org.apache.hadoop.hbase.HColumnDescriptor; -import org.apache.hadoop.hbase.HTableDescriptor; -import org.apache.hadoop.hbase.client.HTable; -import org.apache.hadoop.hbase.io.ImmutableBytesWritable; -import org.apache.hadoop.mapred.JobClient; -import org.apache.hadoop.mapred.JobConf; -import org.apache.hama.algebra.AdditionMap; -import org.apache.hama.algebra.AdditionReduce; -import org.apache.hama.mapred.MatrixMap; -import org.apache.hama.mapred.MatrixReduce; -import org.apache.hama.util.RandomVariable; - -/** - * A library for mathematical operations on matrices of double. - */ -public class Matrix extends AbstractMatrix { - - /** - * Construct - * - * @param conf configuration object - */ - public Matrix(HamaConfiguration conf) { - setConfiguration(conf); - } - - /** - * Construct an matrix - * - * @param conf configuration object - * @param matrixName the name of the matrix - */ - public Matrix(HamaConfiguration conf, String matrixName) { - try { - setConfiguration(conf); - this.matrixName = matrixName; - - if (!admin.tableExists(matrixName)) { - tableDesc = new HTableDescriptor(matrixName.toString()); - tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN.toString())); - create(); - } - - table = new HTable(config, matrixName); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Construct an m-by-n constant matrix. - * - * @param conf configuration object - * @param m the number of rows. - * @param n the number of columns. - * @param s fill the matrix with this scalar value. - */ - public Matrix(HamaConfiguration conf, int m, int n, double s) { - try { - setConfiguration(conf); - matrixName = RandomVariable.randMatrixName(); - - if (!admin.tableExists(matrixName)) { - tableDesc = new HTableDescriptor(matrixName.toString()); - tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN.toString())); - create(); - } - - table = new HTable(config, matrixName); - - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - set(i, j, s); - } - } - - setDimension(m, n); - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * Generate matrix with random elements - * - * @param conf configuration object - * @param m the number of rows. - * @param n the number of columns. - * @return an m-by-n matrix with uniformly distributed random elements. - */ - public static Matrix random(HamaConfiguration conf, int m, int n) { - String name = RandomVariable.randMatrixName(); - Matrix rand = new Matrix(conf, name); - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - rand.set(i, j, RandomVariable.rand()); - } - } - - rand.setDimension(m, n); - LOG.info("Create the " + m + " * " + n + " random matrix : " + name); - return rand; - } - - public Matrix add(Matrix B) { - String output = RandomVariable.randMatrixName(); - Matrix C = new Matrix(config, output); - - JobConf jobConf = new JobConf(config); - jobConf.setJobName("addition MR job"); - - MatrixMap.initJob(this.getName(), B.getName(), AdditionMap.class, - ImmutableBytesWritable.class, Vector.class, jobConf); - MatrixReduce.initJob(C.getName(), AdditionReduce.class, jobConf); - - try { - JobClient.runJob(jobConf); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return C; - } - - public Matrix add(double alpha, Matrix B) { - // TODO Auto-generated method stub - return null; - } - - public Matrix mult(Matrix B) { - // TODO Auto-generated method stub - return null; - } - - public Matrix multAdd(double alpha, Matrix B, Matrix C) { - // TODO Auto-generated method stub - return null; - } - - public double norm(Norm type) { - // TODO Auto-generated method stub - return 0; - } - - public Matrix set(double alpha, Matrix B) { - // TODO Auto-generated method stub - return null; - } - - public Matrix set(Matrix B) { - // TODO Auto-generated method stub - return null; - } -} +/** + * 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; + +/** + * Basic matrix interface. + */ +public interface Matrix { + /** + * Gets the double value of (i, j) + * + * @param i ith row of the matrix + * @param j jth column of the matrix + * @return the value of entry + */ + public double get(int i, int j); + + /** + * Gets the vector of row + * + * @param row the row index of the matrix + * @return the feature vector of row + */ + public Vector getRow(int row); + + /** + * Get a number of row of the matrix from the meta-data column + * + * @return a number of rows of the matrix + */ + public int getRows(); + + /** + * Get a number of column of the matrix from the meta-data column + * + * @return a number of columns of the matrix + */ + public int getColumns(); + + /** + * Sets the double value of (i, j) + * + * @param i ith row of the matrix + * @param j jth column of the matrix + * @param value the value of entry + */ + public void set(int i, int j, double value); + + /** + * A=alpha*B + * + * @param alpha + * @param B + * @return A + */ + public Matrix set(double alpha, Matrix B); + + /** + * A=B + * + * @param B + * @return A + */ + public Matrix set(Matrix B); + + /** + * Sets the dimension of matrix + * + * @param rows the number of rows + * @param columns the number of columns + */ + public void setDimension(int rows, int columns); + + /** + * A(i, j) += value + * + * @param i + * @param j + * @param value + */ + public void add(int i, int j, double value); + + /** + * A = B + A + * + * @param B + * @return A + */ + public Matrix add(Matrix B); + + /** + * A = alpha*B + A + * + * @param alpha + * @param B + * @return A + */ + public Matrix add(double alpha, Matrix B); + + /** + * C = A*B + * + * @param B + * @return C + */ + public Matrix mult(Matrix B); + + /** + * C = alpha*A*B + C + * + * @param alpha + * @param B + * @param C + * @return C + */ + public Matrix multAdd(double alpha, Matrix B, Matrix C); + + /** + * Computes the given norm of the matrix + * + * @param type + * @return norm of the matrix + */ + public double norm(Norm type); + + /** + * Supported matrix-norms. + */ + enum Norm { + /** Largest entry in absolute value */ + Infinity + } + + /** + * Return the matrix name + * + * @return the name of the matrix + */ + public String getName(); +} Modified: incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java (original) +++ incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java Mon Aug 25 06:27:04 2008 @@ -19,9 +19,7 @@ */ package org.apache.hama; -/** - * Basic matrix interface. - */ +@Deprecated public interface MatrixInterface { /** Modified: incubator/hama/trunk/src/java/org/apache/hama/Vector.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/Vector.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/Vector.java (original) +++ incubator/hama/trunk/src/java/org/apache/hama/Vector.java Mon Aug 25 06:27:04 2008 @@ -1,173 +1,128 @@ -/** - * 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 java.util.Map; -import java.util.Set; - -import org.apache.hadoop.hbase.io.Cell; -import org.apache.hadoop.hbase.io.HbaseMapWritable; -import org.apache.hadoop.hbase.io.RowResult; -import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hama.io.VectorWritable; -import org.apache.hama.util.Numeric; -import org.apache.log4j.Logger; - -public class Vector extends VectorWritable implements VectorInterface { - static final Logger LOG = Logger.getLogger(Vector.class); - - public Vector() { - this(null, new HbaseMapWritable()); - } - - public Vector(final byte[] row, final HbaseMapWritable m) { - this.row = row; - this.cells = m; - } - - public Vector(int row, RowResult rowResult) { - this.cells = new HbaseMapWritable(); - this.row = Numeric.intToBytes(row); - for (Map.Entry f : rowResult.entrySet()) { - this.cells.put(f.getKey(), f.getValue()); - } - } - - /** - * Get the row for this Vector - */ - public byte[] getRow() { - return row; - } - - public HbaseMapWritable getCells() { - return cells; - } - - public void add(int index, double value) { - // TODO Auto-generated method stub - - } - - public Vector add(double alpha, Vector v) { - // TODO Auto-generated method stub - return null; - } - - public Vector add(Vector v2) { - HbaseMapWritable trunk = new HbaseMapWritable(); - for (int i = 0; i < this.size(); i++) { - double value = (this.get(i) + v2.get(i)); - Cell cValue = new Cell(String.valueOf(value), System.currentTimeMillis()); - trunk.put(Bytes.toBytes("column:" + i), cValue); - } - - return new Vector(row, trunk); - } - - public double dot(Vector v) { - double cosine = 0.0; - double q_i, d_i; - for (int i = 0; i < Math.min(this.size(), v.size()); i++) { - q_i = v.get(i); - d_i = this.get(i); - cosine += q_i * d_i; - } - return cosine / (this.getNorm2() * v.getNorm2()); - } - - public Vector scale(double alpha) { - Set keySet = cells.keySet(); - Iterator it = keySet.iterator(); - - while (it.hasNext()) { - byte[] key = it.next(); - double oValue = Numeric.bytesToDouble(get(key).getValue()); - double nValue = oValue * alpha; - Cell cValue = new Cell(String.valueOf(nValue), System.currentTimeMillis()); - cells.put(key, cValue); - } - - return this; - } - - public double get(int index) { - return Numeric.bytesToDouble(cells.get(Numeric.getColumnIndex(index)) - .getValue()); - } - - public double norm(Norm type) { - if (type == Norm.One) - return getNorm1(); - else if (type == Norm.Two) - return getNorm2(); - else if (type == Norm.TwoRobust) - return getNorm2Robust(); - else - return getNormInf(); - } - - public void set(int index, double value) { - Cell cValue = new Cell(String.valueOf(value), System.currentTimeMillis()); - cells.put(Numeric.getColumnIndex(index), cValue); - } - - public Vector set(Vector v) { - return new Vector(v.getRow(), v.getCells()); - } - - public double getNorm1() { - double sum = 0.0; - - Set keySet = cells.keySet(); - Iterator it = keySet.iterator(); - - while (it.hasNext()) { - sum += Numeric.bytesToDouble(get(it.next()).getValue()); - } - - return sum; - } - - public double getNorm2() { - double square_sum = 0.0; - - Set keySet = cells.keySet(); - Iterator it = keySet.iterator(); - - while (it.hasNext()) { - double value = Numeric.bytesToDouble(get(it.next()).getValue()); - square_sum += value * value; - } - - return Math.sqrt(square_sum); - } - - public double getNorm2Robust() { - // TODO Auto-generated method stub - return 0; - } - - public double getNormInf() { - // TODO Auto-generated method stub - return 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; + +/** + * Basic vector interface. + */ +public interface Vector { + + /** + * Size of the vector + * + * @return size of the vector + */ + public int size(); + + /** + * Gets the value of index + * + * @param index + * @return v(index) + */ + public double get(int index); + + /** + * Sets the value of index + * + * @param index + * @param value + */ + public void set(int index, double value); + + /** + * Sets the vector + * + * @param v + * @return x = v + */ + public Vector set(Vector v); + + /** + * Adds the value to v(index) + * + * @param index + * @param value + */ + public void add(int index, double value); + + /** + * x = alpha*v + x + * + * @param alpha + * @param v + * @return x = alpha*v + x + */ + public Vector add(double alpha, Vector v); + + /** + * x = v + x + * + * @param v + * @return x = v + x + */ + public Vector add(Vector v); + + /** + * x dot v + * + * @param v + * @return x dot v + */ + public double dot(Vector v); + + /** + * v = alpha*v + * + * @param alpha + * @return v = alpha*v + */ + public Vector scale(double alpha); + + + /** + * Computes the given norm of the vector + * + * @param type + * @return norm of the vector + */ + public double norm(Norm type); + + /** + * Supported vector-norms. + */ + enum Norm { + + /** Sum of the absolute values of the entries */ + One, + + /** The root of sum of squares */ + Two, + + /** + * As the 2 norm may overflow, an overflow resistant version is also + * available. Note that it may be slower. + */ + TwoRobust, + + /** Largest entry in absolute value */ + Infinity + } +} Modified: incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java (original) +++ incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java Mon Aug 25 06:27:04 2008 @@ -1,128 +1,126 @@ -/** - * 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; - -/** - * Basic vector interface. - */ -public interface VectorInterface { - - /** - * Size of the vector - * - * @return size of the vector - */ - public int size(); - - /** - * Gets the value of index - * - * @param index - * @return v(index) - */ - public double get(int index); - - /** - * Sets the value of index - * - * @param index - * @param value - */ - public void set(int index, double value); - - /** - * Sets the vector - * - * @param v - * @return x = v - */ - public Vector set(Vector v); - - /** - * Adds the value to v(index) - * - * @param index - * @param value - */ - public void add(int index, double value); - - /** - * x = alpha*v + x - * - * @param alpha - * @param v - * @return x = alpha*v + x - */ - public Vector add(double alpha, Vector v); - - /** - * x = v + x - * - * @param v - * @return x = v + x - */ - public Vector add(Vector v); - - /** - * x dot v - * - * @param v - * @return x dot v - */ - public double dot(Vector v); - - /** - * v = alpha*v - * - * @param alpha - * @return v = alpha*v - */ - public Vector scale(double alpha); - - - /** - * Computes the given norm of the vector - * - * @param type - * @return norm of the vector - */ - public double norm(Norm type); - - /** - * Supported vector-norms. - */ - enum Norm { - - /** Sum of the absolute values of the entries */ - One, - - /** The root of sum of squares */ - Two, - - /** - * As the 2 norm may overflow, an overflow resistant version is also - * available. Note that it may be slower. - */ - TwoRobust, - - /** Largest entry in absolute value */ - Infinity - } -} +/** + * 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; + +@Deprecated +public interface VectorInterface { + + /** + * Size of the vector + * + * @return size of the vector + */ + public int size(); + + /** + * Gets the value of index + * + * @param index + * @return v(index) + */ + public double get(int index); + + /** + * Sets the value of index + * + * @param index + * @param value + */ + public void set(int index, double value); + + /** + * Sets the vector + * + * @param v + * @return x = v + */ + public Vector set(Vector v); + + /** + * Adds the value to v(index) + * + * @param index + * @param value + */ + public void add(int index, double value); + + /** + * x = alpha*v + x + * + * @param alpha + * @param v + * @return x = alpha*v + x + */ + public Vector add(double alpha, Vector v); + + /** + * x = v + x + * + * @param v + * @return x = v + x + */ + public Vector add(Vector v); + + /** + * x dot v + * + * @param v + * @return x dot v + */ + public double dot(Vector v); + + /** + * v = alpha*v + * + * @param alpha + * @return v = alpha*v + */ + public Vector scale(double alpha); + + + /** + * Computes the given norm of the vector + * + * @param type + * @return norm of the vector + */ + public double norm(Norm type); + + /** + * Supported vector-norms. + */ + enum Norm { + + /** Sum of the absolute values of the entries */ + One, + + /** The root of sum of squares */ + Two, + + /** + * As the 2 norm may overflow, an overflow resistant version is also + * available. Note that it may be slower. + */ + TwoRobust, + + /** Largest entry in absolute value */ + Infinity + } +} Modified: incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionMap.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionMap.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionMap.java (original) +++ incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionMap.java Mon Aug 25 06:27:04 2008 @@ -1,41 +1,43 @@ -/** - * 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.algebra; - -import java.io.IOException; - -import org.apache.hadoop.hbase.io.ImmutableBytesWritable; -import org.apache.hadoop.mapred.OutputCollector; -import org.apache.hadoop.mapred.Reporter; -import org.apache.hama.Vector; -import org.apache.hama.mapred.MatrixMap; - -public class AdditionMap extends MatrixMap { - - public void map(ImmutableBytesWritable key, Vector value, - OutputCollector output, - Reporter reporter) throws IOException { - - Vector v1 = B.getRow(key.get()); - output.collect(key, v1.add(value)); - - } - -} +/** + * 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.algebra; + +import java.io.IOException; + +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.mapred.OutputCollector; +import org.apache.hadoop.mapred.Reporter; +import org.apache.hama.DenseVector; +import org.apache.hama.Vector; +import org.apache.hama.mapred.DenseMap; +import org.apache.hama.util.Numeric; + +public class AdditionMap extends DenseMap { + + @Override + public void map(ImmutableBytesWritable key, Vector value, + OutputCollector output, + Reporter reporter) throws IOException { + + Vector v1 = MATRIX_B.getRow(Numeric.bytesToInt(key.get())); + output.collect(key, (DenseVector) v1.add(value)); + + } +} Modified: incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionReduce.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionReduce.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionReduce.java (original) +++ incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionReduce.java Mon Aug 25 06:27:04 2008 @@ -1,51 +1,51 @@ -/** - * 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.algebra; - -import java.io.IOException; -import java.util.Iterator; -import java.util.Map; - -import org.apache.hadoop.hbase.io.BatchUpdate; -import org.apache.hadoop.hbase.io.Cell; -import org.apache.hadoop.hbase.io.ImmutableBytesWritable; -import org.apache.hadoop.mapred.OutputCollector; -import org.apache.hadoop.mapred.Reporter; -import org.apache.hama.Vector; -import org.apache.hama.mapred.MatrixReduce; - -public class AdditionReduce extends - MatrixReduce { - - @Override - public void reduce(ImmutableBytesWritable key, Iterator values, - OutputCollector output, - Reporter reporter) throws IOException { - - BatchUpdate b = new BatchUpdate(key.get()); - Vector vector = values.next(); - for (Map.Entry f : vector.entrySet()) { - b.put(f.getKey(), f.getValue().getValue()); - } - - output.collect(key, b); - } - -} +/** + * 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.algebra; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; + +import org.apache.hadoop.hbase.io.BatchUpdate; +import org.apache.hadoop.hbase.io.Cell; +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.mapred.OutputCollector; +import org.apache.hadoop.mapred.Reporter; +import org.apache.hama.DenseVector; +import org.apache.hama.mapred.MatrixReduce; + +public class AdditionReduce extends + MatrixReduce { + + @Override + public void reduce(ImmutableBytesWritable key, Iterator values, + OutputCollector output, + Reporter reporter) throws IOException { + + BatchUpdate b = new BatchUpdate(key.get()); + DenseVector vector = values.next(); + for (Map.Entry f : vector.entrySet()) { + b.put(f.getKey(), f.getValue().getValue()); + } + + output.collect(key, b); + } + +} Modified: incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java (original) +++ incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java Mon Aug 25 06:27:04 2008 @@ -152,15 +152,15 @@ sb.append(", timestamp="); sb.append(Long.toString(e.getValue().getTimestamp())); sb.append(", value="); - byte[] v = e.getValue().getValue(); + byte[] value = e.getValue().getValue(); if (Bytes.equals(e.getKey(), HConstants.COL_REGIONINFO)) { try { - sb.append(Writables.getHRegionInfo(v).toString()); + sb.append(Writables.getHRegionInfo(value).toString()); } catch (IOException ioe) { sb.append(ioe.toString()); } } else { - sb.append(v); + sb.append(value); } sb.append(")"); } @@ -178,8 +178,8 @@ private final byte[] column; private final Cell cell; - Entries(byte[] row, Cell cell) { - this.column = row; + Entries(byte[] column, Cell cell) { + this.column = column; this.cell = cell; } Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/DenseMap.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/DenseMap.java?rev=688720&view=auto ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/mapred/DenseMap.java (added) +++ incubator/hama/trunk/src/java/org/apache/hama/mapred/DenseMap.java Mon Aug 25 06:27:04 2008 @@ -0,0 +1,43 @@ +package org.apache.hama.mapred; + +import java.io.IOException; + +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.io.Writable; +import org.apache.hadoop.io.WritableComparable; +import org.apache.hadoop.mapred.FileInputFormat; +import org.apache.hadoop.mapred.JobConf; +import org.apache.hadoop.mapred.MapReduceBase; +import org.apache.hadoop.mapred.Mapper; +import org.apache.hadoop.mapred.OutputCollector; +import org.apache.hadoop.mapred.Reporter; +import org.apache.hama.Constants; +import org.apache.hama.DenseMatrix; +import org.apache.hama.HamaConfiguration; +import org.apache.hama.Matrix; +import org.apache.hama.Vector; + +@SuppressWarnings("unchecked") +public abstract class DenseMap + extends MapReduceBase implements + Mapper { + public static Matrix MATRIX_B; + + public static void initJob(String matrixA, String matrixB, + Class mapper, + Class outputKeyClass, + Class outputValueClass, JobConf job) { + + job.setInputFormat(MatrixInputFormat.class); + job.setMapOutputValueClass(outputValueClass); + job.setMapOutputKeyClass(outputKeyClass); + job.setMapperClass(mapper); + FileInputFormat.addInputPaths(job, matrixA); + + MATRIX_B = new DenseMatrix(new HamaConfiguration(), matrixB); + job.set(MatrixInputFormat.COLUMN_LIST, Constants.COLUMN); + } + + public abstract void map(ImmutableBytesWritable key, Vector value, + OutputCollector output, Reporter reporter) throws IOException; +} Modified: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormatBase.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormatBase.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormatBase.java (original) +++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormatBase.java Mon Aug 25 06:27:04 2008 @@ -1,274 +1,292 @@ -package org.apache.hama.mapred; - -import java.io.IOException; -import java.util.HashSet; -import java.util.Set; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.hbase.HConstants; -import org.apache.hadoop.hbase.client.HTable; -import org.apache.hadoop.hbase.client.Scanner; -import org.apache.hadoop.hbase.filter.RowFilterInterface; -import org.apache.hadoop.hbase.filter.RowFilterSet; -import org.apache.hadoop.hbase.filter.StopRowFilter; -import org.apache.hadoop.hbase.io.ImmutableBytesWritable; -import org.apache.hadoop.hbase.io.RowResult; -import org.apache.hadoop.hbase.mapred.TableSplit; -import org.apache.hadoop.hbase.regionserver.HRegion; -import org.apache.hadoop.hbase.util.Writables; -import org.apache.hadoop.mapred.InputFormat; -import org.apache.hadoop.mapred.InputSplit; -import org.apache.hadoop.mapred.JobConf; -import org.apache.hadoop.mapred.RecordReader; -import org.apache.hadoop.mapred.Reporter; -import org.apache.hama.Vector; -import org.apache.hama.io.VectorWritable; - -public abstract class MatrixInputFormatBase implements - InputFormat { - private final Log LOG = LogFactory.getLog(MatrixInputFormatBase.class); - private byte[][] inputColumns; - private HTable table; - private TableRecordReader tableRecordReader; - private RowFilterInterface rowFilter; - - /** - * Iterate over an HBase table data, return (Text, VectorResult) pairs - */ - protected class TableRecordReader implements - RecordReader { - private byte[] startRow; - private byte[] endRow; - private RowFilterInterface trrRowFilter; - private Scanner scanner; - private HTable htable; - private byte[][] trrInputColumns; - - /** - * Build the scanner. Not done in constructor to allow for extension. - * - * @throws IOException - */ - public void init() throws IOException { - if ((endRow != null) && (endRow.length > 0)) { - if (trrRowFilter != null) { - final Set rowFiltersSet = new HashSet(); - rowFiltersSet.add(new StopRowFilter(endRow)); - rowFiltersSet.add(trrRowFilter); - this.scanner = this.htable.getScanner(trrInputColumns, startRow, - new RowFilterSet(RowFilterSet.Operator.MUST_PASS_ALL, - rowFiltersSet)); - } else { - this.scanner = this.htable.getScanner(trrInputColumns, startRow, - endRow); - } - } else { - this.scanner = this.htable.getScanner(trrInputColumns, startRow, - trrRowFilter); - } - } - - /** - * @param htable the {@link HTable} to scan. - */ - public void setHTable(HTable htable) { - this.htable = htable; - } - - /** - * @param inputColumns the columns to be placed in {@link VectorWritable}. - */ - public void setInputColumns(final byte[][] inputColumns) { - this.trrInputColumns = inputColumns; - } - - /** - * @param startRow the first row in the split - */ - public void setStartRow(final byte[] startRow) { - this.startRow = startRow; - } - - /** - * - * @param endRow the last row in the split - */ - public void setEndRow(final byte[] endRow) { - this.endRow = endRow; - } - - /** - * @param rowFilter the {@link RowFilterInterface} to be used. - */ - public void setRowFilter(RowFilterInterface rowFilter) { - this.trrRowFilter = rowFilter; - } - - /** {@inheritDoc} */ - public void close() throws IOException { - this.scanner.close(); - } - - /** - * @return ImmutableBytesWritable - * - * @see org.apache.hadoop.mapred.RecordReader#createKey() - */ - public ImmutableBytesWritable createKey() { - return new ImmutableBytesWritable(); - } - - /** - * @return VectorResult - * - * @see org.apache.hadoop.mapred.RecordReader#createValue() - */ - public Vector createValue() { - return new Vector(); - } - - /** {@inheritDoc} */ - public long getPos() { - // This should be the ordinal tuple in the range; - // not clear how to calculate... - return 0; - } - - /** {@inheritDoc} */ - public float getProgress() { - // Depends on the total number of tuples and getPos - return 0; - } - - /** - * @param key HStoreKey as input key. - * @param value MapWritable as input value - * - * Converts Scanner.next() to Text, VectorResult - * - * @return true if there was more data - * @throws IOException - */ - @SuppressWarnings("unchecked") - public boolean next(ImmutableBytesWritable key, Vector value) - throws IOException { - RowResult result = this.scanner.next(); - boolean hasMore = result != null && result.size() > 0; - if (hasMore) { - key.set(result.getRow()); - Writables.copyWritable(result, value); - } - return hasMore; - } - } - - /** - * Builds a TableRecordReader. If no TableRecordReader was provided, uses the - * default. - * - * @see org.apache.hadoop.mapred.InputFormat#getRecordReader(InputSplit, - * JobConf, Reporter) - */ - public RecordReader getRecordReader( - InputSplit split, @SuppressWarnings("unused") JobConf job, - @SuppressWarnings("unused") Reporter reporter) throws IOException { - TableSplit tSplit = (TableSplit) split; - TableRecordReader trr = this.tableRecordReader; - // if no table record reader was provided use default - if (trr == null) { - trr = new TableRecordReader(); - } - trr.setStartRow(tSplit.getStartRow()); - trr.setEndRow(tSplit.getEndRow()); - trr.setHTable(this.table); - trr.setInputColumns(this.inputColumns); - trr.setRowFilter(this.rowFilter); - trr.init(); - return trr; - } - - /** - * Calculates the splits that will serve as input for the map tasks. - *
    - * Splits are created in number equal to the smallest between numSplits and - * the number of {@link HRegion}s in the table. If the number of splits is - * smaller than the number of {@link HRegion}s then splits are spanned across - * multiple {@link HRegion}s and are grouped the most evenly possible. In the - * case splits are uneven the bigger splits are placed first in the - * {@link InputSplit} array. - * - * @param job the map task {@link JobConf} - * @param numSplits a hint to calculate the number of splits - * - * @return the input splits - * - * @see org.apache.hadoop.mapred.InputFormat#getSplits(org.apache.hadoop.mapred.JobConf, - * int) - */ - public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException { - byte[][] startKeys = this.table.getStartKeys(); - if (startKeys == null || startKeys.length == 0) { - throw new IOException("Expecting at least one region"); - } - if (this.table == null) { - throw new IOException("No table was provided"); - } - if (this.inputColumns == null || this.inputColumns.length == 0) { - throw new IOException("Expecting at least one column"); - } - int realNumSplits = numSplits > startKeys.length ? startKeys.length - : numSplits; - InputSplit[] splits = new InputSplit[realNumSplits]; - int middle = startKeys.length / realNumSplits; - int startPos = 0; - for (int i = 0; i < realNumSplits; i++) { - int lastPos = startPos + middle; - lastPos = startKeys.length % realNumSplits > i ? lastPos + 1 : lastPos; - splits[i] = new TableSplit(this.table.getTableName(), - startKeys[startPos], ((i + 1) < realNumSplits) ? startKeys[lastPos] - : HConstants.EMPTY_START_ROW); - if (LOG.isDebugEnabled()) { - LOG.debug("split: " + i + "->" + splits[i]); - } - startPos = lastPos; - } - return splits; - - } - - /** - * @param inputColumns to be passed in {@link VectorWritable} to the map task. - */ - protected void setInputColums(byte[][] inputColumns) { - this.inputColumns = inputColumns; - } - - /** - * Allows subclasses to set the {@link HTable}. - * - * @param table to get the data from - */ - protected void setHTable(HTable table) { - this.table = table; - } - - /** - * Allows subclasses to set the {@link TableRecordReader}. - * - * @param tableRecordReader to provide other {@link TableRecordReader} - * implementations. - */ - protected void setTableRecordReader(TableRecordReader tableRecordReader) { - this.tableRecordReader = tableRecordReader; - } - - /** - * Allows subclasses to set the {@link RowFilterInterface} to be used. - * - * @param rowFilter - */ - protected void setRowFilter(RowFilterInterface rowFilter) { - this.rowFilter = rowFilter; - } -} +/** + * 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 java.util.HashSet; +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.client.HTable; +import org.apache.hadoop.hbase.client.Scanner; +import org.apache.hadoop.hbase.filter.RowFilterInterface; +import org.apache.hadoop.hbase.filter.RowFilterSet; +import org.apache.hadoop.hbase.filter.StopRowFilter; +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.hbase.io.RowResult; +import org.apache.hadoop.hbase.mapred.TableSplit; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.util.Writables; +import org.apache.hadoop.mapred.InputFormat; +import org.apache.hadoop.mapred.InputSplit; +import org.apache.hadoop.mapred.JobConf; +import org.apache.hadoop.mapred.RecordReader; +import org.apache.hadoop.mapred.Reporter; +import org.apache.hama.DenseVector; + +public abstract class MatrixInputFormatBase implements + InputFormat { + private final Log LOG = LogFactory.getLog(MatrixInputFormatBase.class); + private byte[][] inputColumns; + private HTable table; + private TableRecordReader tableRecordReader; + private RowFilterInterface rowFilter; + + /** + * Iterate over an HBase table data, return (Text, DenseVector) pairs + */ + protected class TableRecordReader implements + RecordReader { + private byte[] startRow; + private byte[] endRow; + private RowFilterInterface trrRowFilter; + private Scanner scanner; + private HTable htable; + private byte[][] trrInputColumns; + + /** + * Build the scanner. Not done in constructor to allow for extension. + * + * @throws IOException + */ + public void init() throws IOException { + if ((endRow != null) && (endRow.length > 0)) { + if (trrRowFilter != null) { + final Set rowFiltersSet = new HashSet(); + rowFiltersSet.add(new StopRowFilter(endRow)); + rowFiltersSet.add(trrRowFilter); + this.scanner = this.htable.getScanner(trrInputColumns, startRow, + new RowFilterSet(RowFilterSet.Operator.MUST_PASS_ALL, + rowFiltersSet)); + } else { + this.scanner = this.htable.getScanner(trrInputColumns, startRow, + endRow); + } + } else { + this.scanner = this.htable.getScanner(trrInputColumns, startRow, + trrRowFilter); + } + } + + /** + * @param htable the {@link HTable} to scan. + */ + public void setHTable(HTable htable) { + this.htable = htable; + } + + /** + * @param inputColumns the columns to be placed in {@link DenseVector}. + */ + public void setInputColumns(final byte[][] inputColumns) { + this.trrInputColumns = inputColumns; + } + + /** + * @param startRow the first row in the split + */ + public void setStartRow(final byte[] startRow) { + this.startRow = startRow; + } + + /** + * + * @param endRow the last row in the split + */ + public void setEndRow(final byte[] endRow) { + this.endRow = endRow; + } + + /** + * @param rowFilter the {@link RowFilterInterface} to be used. + */ + public void setRowFilter(RowFilterInterface rowFilter) { + this.trrRowFilter = rowFilter; + } + + /** {@inheritDoc} */ + public void close() throws IOException { + this.scanner.close(); + } + + /** + * @return ImmutableBytesWritable + * + * @see org.apache.hadoop.mapred.RecordReader#createKey() + */ + public ImmutableBytesWritable createKey() { + return new ImmutableBytesWritable(); + } + + /** + * @return DenseVector + * + * @see org.apache.hadoop.mapred.RecordReader#createValue() + */ + public DenseVector createValue() { + return new DenseVector(); + } + + /** {@inheritDoc} */ + public long getPos() { + // This should be the ordinal tuple in the range; + // not clear how to calculate... + return 0; + } + + /** {@inheritDoc} */ + public float getProgress() { + // Depends on the total number of tuples and getPos + return 0; + } + + /** + * @param key HStoreKey as input key. + * @param value MapWritable as input value + * + * Converts Scanner.next() to Text, DenseVector + * + * @return true if there was more data + * @throws IOException + */ + @SuppressWarnings("unchecked") + public boolean next(ImmutableBytesWritable key, DenseVector value) + throws IOException { + RowResult result = this.scanner.next(); + boolean hasMore = result != null && result.size() > 0; + if (hasMore) { + key.set(result.getRow()); + Writables.copyWritable(result, value); + } + return hasMore; + } + } + + /** + * Builds a TableRecordReader. If no TableRecordReader was provided, uses the + * default. + * + * @see org.apache.hadoop.mapred.InputFormat#getRecordReader(InputSplit, + * JobConf, Reporter) + */ + public RecordReader getRecordReader( + InputSplit split, @SuppressWarnings("unused") JobConf job, + @SuppressWarnings("unused") Reporter reporter) throws IOException { + TableSplit tSplit = (TableSplit) split; + TableRecordReader trr = this.tableRecordReader; + // if no table record reader was provided use default + if (trr == null) { + trr = new TableRecordReader(); + } + trr.setStartRow(tSplit.getStartRow()); + trr.setEndRow(tSplit.getEndRow()); + trr.setHTable(this.table); + trr.setInputColumns(this.inputColumns); + trr.setRowFilter(this.rowFilter); + trr.init(); + return trr; + } + + /** + * Calculates the splits that will serve as input for the map tasks. + *
      + * Splits are created in number equal to the smallest between numSplits and + * the number of {@link HRegion}s in the table. If the number of splits is + * smaller than the number of {@link HRegion}s then splits are spanned across + * multiple {@link HRegion}s and are grouped the most evenly possible. In the + * case splits are uneven the bigger splits are placed first in the + * {@link InputSplit} array. + * + * @param job the map task {@link JobConf} + * @param numSplits a hint to calculate the number of splits + * + * @return the input splits + * + * @see org.apache.hadoop.mapred.InputFormat#getSplits(org.apache.hadoop.mapred.JobConf, + * int) + */ + public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException { + byte[][] startKeys = this.table.getStartKeys(); + if (startKeys == null || startKeys.length == 0) { + throw new IOException("Expecting at least one region"); + } + if (this.table == null) { + throw new IOException("No table was provided"); + } + if (this.inputColumns == null || this.inputColumns.length == 0) { + throw new IOException("Expecting at least one column"); + } + int realNumSplits = numSplits > startKeys.length ? startKeys.length + : numSplits; + InputSplit[] splits = new InputSplit[realNumSplits]; + int middle = startKeys.length / realNumSplits; + int startPos = 0; + for (int i = 0; i < realNumSplits; i++) { + int lastPos = startPos + middle; + lastPos = startKeys.length % realNumSplits > i ? lastPos + 1 : lastPos; + splits[i] = new TableSplit(this.table.getTableName(), + startKeys[startPos], ((i + 1) < realNumSplits) ? startKeys[lastPos] + : HConstants.EMPTY_START_ROW); + if (LOG.isDebugEnabled()) { + LOG.debug("split: " + i + "->" + splits[i]); + } + startPos = lastPos; + } + return splits; + + } + + /** + * @param inputColumns to be passed in {@link DenseVector} to the map task. + */ + protected void setInputColums(byte[][] inputColumns) { + this.inputColumns = inputColumns; + } + + /** + * Allows subclasses to set the {@link HTable}. + * + * @param table to get the data from + */ + protected void setHTable(HTable table) { + this.table = table; + } + + /** + * Allows subclasses to set the {@link TableRecordReader}. + * + * @param tableRecordReader to provide other {@link TableRecordReader} + * implementations. + */ + protected void setTableRecordReader(TableRecordReader tableRecordReader) { + this.tableRecordReader = tableRecordReader; + } + + /** + * Allows subclasses to set the {@link RowFilterInterface} to be used. + * + * @param rowFilter + */ + protected void setRowFilter(RowFilterInterface rowFilter) { + this.rowFilter = rowFilter; + } +} Modified: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java (original) +++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java Mon Aug 25 06:27:04 2008 @@ -1,61 +1,62 @@ -/** - * 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.hbase.io.ImmutableBytesWritable; -import org.apache.hadoop.io.Writable; -import org.apache.hadoop.io.WritableComparable; -import org.apache.hadoop.mapred.FileInputFormat; -import org.apache.hadoop.mapred.JobConf; -import org.apache.hadoop.mapred.MapReduceBase; -import org.apache.hadoop.mapred.Mapper; -import org.apache.hadoop.mapred.OutputCollector; -import org.apache.hadoop.mapred.Reporter; -import org.apache.hama.Constants; -import org.apache.hama.HamaConfiguration; -import org.apache.hama.Matrix; -import org.apache.hama.Vector; - -@SuppressWarnings("unchecked") -public abstract class MatrixMap - extends MapReduceBase implements - Mapper { - protected static Matrix B; - - public static void initJob(String matrixA, String matrixB, - Class mapper, - Class outputKeyClass, - Class outputValueClass, JobConf job) { - - job.setInputFormat(MatrixInputFormat.class); - job.setMapOutputValueClass(outputValueClass); - job.setMapOutputKeyClass(outputKeyClass); - job.setMapperClass(mapper); - FileInputFormat.addInputPaths(job, matrixA); - - B = new Matrix(new HamaConfiguration(), matrixB); - job.set(MatrixInputFormat.COLUMN_LIST, Constants.COLUMN); - } - - public abstract void map(ImmutableBytesWritable key, Vector value, - OutputCollector output, Reporter reporter) throws IOException; -} +/** + * 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.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.io.Writable; +import org.apache.hadoop.io.WritableComparable; +import org.apache.hadoop.mapred.FileInputFormat; +import org.apache.hadoop.mapred.JobConf; +import org.apache.hadoop.mapred.MapReduceBase; +import org.apache.hadoop.mapred.Mapper; +import org.apache.hadoop.mapred.OutputCollector; +import org.apache.hadoop.mapred.Reporter; +import org.apache.hama.Constants; +import org.apache.hama.DenseMatrix; +import org.apache.hama.HamaConfiguration; +import org.apache.hama.Matrix; +import org.apache.hama.Vector; + +@Deprecated +public abstract class MatrixMap + extends MapReduceBase implements + Mapper { + protected static Matrix MATRIX_B; + + public static void initJob(String matrixA, String matrixB, + Class mapper, + Class outputKeyClass, + Class outputValueClass, JobConf job) { + + job.setInputFormat(MatrixInputFormat.class); + job.setMapOutputValueClass(outputValueClass); + job.setMapOutputKeyClass(outputKeyClass); + job.setMapperClass(mapper); + FileInputFormat.addInputPaths(job, matrixA); + + MATRIX_B = new DenseMatrix(new HamaConfiguration(), matrixB); + job.set(MatrixInputFormat.COLUMN_LIST, Constants.COLUMN); + } + + public abstract void map(ImmutableBytesWritable key, Vector value, + OutputCollector output, Reporter reporter) throws IOException; +} Modified: incubator/hama/trunk/src/test/org/apache/hama/TestMatrix.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestMatrix.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/test/org/apache/hama/TestMatrix.java (original) +++ incubator/hama/trunk/src/test/org/apache/hama/TestMatrix.java Mon Aug 25 06:27:04 2008 @@ -28,7 +28,7 @@ * Random matrix creation test */ public void testRandomMatrix() { - Matrix rand = Matrix.random(conf, SIZE, SIZE); + Matrix rand = DenseMatrix.random(conf, SIZE, SIZE); assertTrue(rand.getRows() == SIZE); } }