You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by ed...@apache.org on 2008/06/24 12:36:25 UTC
svn commit: r671120 [3/4] - in /incubator/hama/trunk: ./ lib/ lib/findbugs/
lib/findbugs/plugin/ lib/jetty-ext/ src/ src/docs/ src/docs/src/
src/docs/src/documentation/ src/docs/src/documentation/classes/
src/docs/src/documentation/content/ src/docs/sr...
Added: 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=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Matrix.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/Matrix.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,303 @@
+/**
+ * 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.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.JobClient;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hama.algebra.AdditionSubtractionMap;
+import org.apache.hama.algebra.AdditionSubtractionReduce;
+import org.apache.hama.algebra.CholeskyDecompositionMap;
+import org.apache.hama.algebra.CroutDecompositionMap;
+import org.apache.hama.algebra.DeterminantMap;
+import org.apache.hama.algebra.DeterminantReduce;
+import org.apache.hama.algebra.MultiplicationMap;
+import org.apache.hama.algebra.MultiplicationReduce;
+import org.apache.hama.mapred.MatrixInputFormat;
+import org.apache.hama.mapred.MatrixOutputFormat;
+
+/**
+ * A library for mathematical operations on matrices of double.
+ */
+public class Matrix extends AbstractMatrix {
+
+ /**
+ * Construct
+ *
+ * @param conf configuration object
+ */
+ public Matrix(Configuration conf) {
+ setConfiguration(conf);
+ }
+
+ /**
+ * Construct an matrix
+ *
+ * @param conf configuration object
+ * @param matrixName the name of the matrix
+ */
+ public Matrix(Configuration conf, Text 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(HBaseConfiguration conf, int m, int n, double s) {
+ try {
+ setConfiguration(conf);
+ matrixName = new Text(Constants.RANDOM + System.currentTimeMillis());
+
+ 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(Configuration conf, int m, int n) {
+ Text name = new Text(Constants.RANDOM + System.currentTimeMillis());
+ 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);
+
+ return rand;
+ }
+
+ /**
+ * Generate matrix with identity elements
+ *
+ * @param conf configuration object
+ * @param m the number of rows.
+ * @param n the number of columns.
+ * @return an m-by-n matrix with ones on the diagonal and zeros elsewhere.
+ */
+ public static Matrix identity(HBaseConfiguration conf, int m, int n) {
+ // TODO
+ return null;
+ }
+
+ /** {@inheritDoc} */
+ public Matrix multiply(Matrix b) {
+ String output = Constants.RESULT + System.currentTimeMillis();
+ Matrix c = new Matrix(config, new Text(output));
+
+ JobConf jobConf = new JobConf(config);
+ jobConf.setJobName("parallel matrix multiplication of " + getName() + " and " + b.getName());
+ jobConf.setInputFormat(MatrixInputFormat.class);
+ jobConf.setOutputFormat(MatrixOutputFormat.class);
+ MultiplicationMap.initJob(getName(), b.getName(), MultiplicationMap.class, jobConf);
+ MultiplicationReduce.initJob(output, MultiplicationReduce.class, jobConf);
+
+ jobConf.setNumMapTasks(mapper);
+ jobConf.setNumReduceTasks(reducer);
+
+ try {
+ JobClient.runJob(jobConf);
+ } catch (IOException e) {
+ LOG.info(e);
+ }
+
+ return c;
+ }
+
+ /** {@inheritDoc} */
+ public Matrix addition(Matrix b) {
+ return additionSubtraction(b, Constants.PLUS);
+ }
+
+ /** {@inheritDoc} */
+ public Matrix subtraction(Matrix b) {
+ return additionSubtraction(b, Constants.PLUS);
+ }
+
+ /**
+ * Method for add or subtract operation
+ *
+ * @param target
+ * @param operator
+ * @return matrix
+ */
+ public Matrix additionSubtraction(Matrix target, String operator) {
+ String b = target.getName();
+ String output = Constants.RESULT + System.currentTimeMillis();
+ Matrix c = new Matrix(config, new Text(output));
+ String jobName = "parallel matrix " + operator + " of " + getName() + " and " + b;
+ LOG.info(jobName);
+
+ JobConf jobConf = new JobConf(config);
+ jobConf.setJobName(jobName);
+ jobConf.setInputFormat(MatrixInputFormat.class);
+ jobConf.setOutputFormat(MatrixOutputFormat.class);
+ AdditionSubtractionMap.initJob(getName(), b, operator,
+ AdditionSubtractionMap.class, jobConf);
+ AdditionSubtractionReduce.initJob(output,
+ AdditionSubtractionReduce.class, jobConf);
+
+ jobConf.setNumMapTasks(mapper);
+ jobConf.setNumReduceTasks(reducer);
+
+ try {
+ JobClient.runJob(jobConf);
+ } catch (IOException e) {
+ LOG.info(e);
+ }
+
+ return c;
+ }
+
+ /** {@inheritDoc} */
+ public double determinant() {
+ JobConf jobConf = new JobConf(config);
+ jobConf.setJobName("matrix determinant");
+
+ String check = Constants.RESULT + System.currentTimeMillis();
+ Matrix c = new Matrix(config, new Text(check));
+ for (int i = 0; i < getRowDimension(); i++) {
+ c.set(i, 0, 1.0);
+ }
+ c.setDimension(getRowDimension(), 0);
+ jobConf.setInputFormat(MatrixInputFormat.class);
+ jobConf.setOutputFormat(MatrixOutputFormat.class);
+ DeterminantMap.initJob(getName(), check, DeterminantMap.class, jobConf);
+ DeterminantReduce.initJob(getName(), DeterminantReduce.class, jobConf);
+
+ jobConf.setNumMapTasks(mapper);
+ jobConf.setNumReduceTasks(reducer);
+
+ try {
+ JobClient.runJob(jobConf);
+ } catch (IOException e) {
+ LOG.info(e);
+ }
+
+ c.clear();
+ return getDeterminant();
+ }
+
+ /** {@inheritDoc} */
+ public TriangularMatrix decompose(Decomposition technique) {
+ if (technique.equals(Decomposition.Cholesky))
+ return choleskyDecompose();
+ else if (technique.equals(Decomposition.Crout))
+ return croutDecompose();
+ else
+ return null;
+ }
+
+ private TriangularMatrix croutDecompose() {
+ JobConf jobConf = new JobConf(config);
+ jobConf.setJobName("Crout Decomposition");
+
+ String output = Constants.RESULT + System.currentTimeMillis();
+ TriangularMatrix b = new TriangularMatrix(config, new Text(output));
+ jobConf.setInputFormat(MatrixInputFormat.class);
+ jobConf.setOutputFormat(MatrixOutputFormat.class);
+ CroutDecompositionMap.initJob(getName(), output,
+ CroutDecompositionMap.class, jobConf);
+
+ jobConf.setNumMapTasks(mapper);
+ jobConf.setNumReduceTasks(reducer);
+
+ try {
+ JobClient.runJob(jobConf);
+ } catch (IOException e) {
+ LOG.info(e);
+ }
+
+ return b;
+ }
+
+ private TriangularMatrix choleskyDecompose() {
+ JobConf jobConf = new JobConf(config);
+ jobConf.setJobName("Cholesky Decomposition");
+
+ String output = Constants.RESULT + System.currentTimeMillis();
+ TriangularMatrix b = new TriangularMatrix(config, new Text(output));
+ jobConf.setInputFormat(MatrixInputFormat.class);
+ jobConf.setOutputFormat(MatrixOutputFormat.class);
+ CholeskyDecompositionMap.initJob(getName(), output,
+ CholeskyDecompositionMap.class, jobConf);
+
+ jobConf.setNumMapTasks(mapper);
+ jobConf.setNumReduceTasks(reducer);
+
+ try {
+ JobClient.runJob(jobConf);
+ } catch (IOException e) {
+ LOG.info(e);
+ }
+
+ return b;
+ }
+
+}
Added: 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=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,178 @@
+/**
+ * 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. It holds <code>double</code>s in a rectangular 2D
+ * array, and it is used alongside <code>Vector</code> in numerical
+ * computations. Implementing classes decides on the actual storage.
+ */
+public interface MatrixInterface {
+
+ /**
+ * 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 FeatureVector getRowVector(int row);
+
+ /**
+ * Sets the double value of (i, j)
+ *
+ * @param i ith row of the matrix
+ * @param j jth column of the matrix
+ * @param d the value of entry
+ */
+ public void set(int i, int j, double d);
+
+ /**
+ * Adds value to (i, j)
+ *
+ * @param i i th row of the matrix
+ * @param j j th column of the matrix
+ * @param d the value of entry
+ */
+ public void add(int i, int j, double d);
+
+ /**
+ * Delete a Row to Matrix.
+ *
+ * @param i row number to delete
+ */
+ public void deleteRowEquals(int i);
+
+ /**
+ * Delete a Column to Matrix.
+ *
+ * @param j column number to delete
+ */
+ public void deleteColumnEquals(int j);
+
+ /**
+ * Get a number of row of the matrix from the meta-data column
+ *
+ * @return a number of rows of the matrix
+ */
+ public int getRowDimension();
+
+ /**
+ * Get a number of column of the matrix from the meta-data column
+ *
+ * @return a number of columns of the matrix
+ */
+ public int getColumnDimension();
+
+ /**
+ * Sets the dimension of matrix
+ *
+ * @param rows the number of rows
+ * @param columns the number of columns
+ */
+ public void setDimension(int rows, int columns);
+
+ /**
+ * Modify dimensions of matrix
+ *
+ * @param m number of rows
+ * @param n number of columns
+ */
+ public void reset(int m, int n);
+
+ /**
+ * Return the matrix name
+ *
+ * @return the name of the matrix
+ */
+ public String getName();
+
+ /**
+ * Make a deep copy of a matrix
+ *
+ * @return clone matrix
+ */
+ public Matrix copy();
+
+ /**
+ * Multiply two matrices
+ *
+ * @param b matrix b
+ * @return the result of the multiplication of matrix a and matrix b
+ */
+ public Matrix multiply(Matrix b);
+
+ /**
+ * Add two matrices
+ *
+ * @param b matrix b
+ * @return the result of the addition of matrix a and matrix b
+ */
+ public Matrix addition(Matrix b);
+
+ /**
+ * Subtract two matrices
+ *
+ * @param b matrix b
+ * @return the result of the substraction of matrix a and matrix b
+ */
+ public Matrix subtraction(Matrix b);
+
+ /**
+ * Calculates determinant of a matrix
+ *
+ * @return the value of determinant
+ */
+ public double determinant();
+
+ /**
+ * Save the matrix to table
+ *
+ * @param matrixName the name of the matrix
+ */
+ public void save(String matrixName);
+
+ /**
+ * Decomposition
+ *
+ * @return the decomposed result
+ */
+ public TriangularMatrix decompose(Decomposition technique);
+
+ /**
+ * Clear object
+ */
+ public void clear();
+
+ /**
+ * Close object
+ */
+ public void close();
+
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/RandomVariable.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/RandomVariable.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/RandomVariable.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/RandomVariable.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,209 @@
+/**
+ * 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;
+
+/**
+ * The RandomVaraibale Class provides static methods for generating random
+ * numbers.
+ */
+public class RandomVariable {
+
+ /**
+ * Generate a random number between 0 and 1.
+ *
+ * @return a double between 0 and 1.
+ */
+ protected static double rand() {
+ double x = Math.random();
+ return x;
+ }
+
+ /**
+ * Generate a random integer.
+ *
+ * @param i0 min of the random variable.
+ * @param i1 max of the random variable.
+ * @return an int between i0 and i1.
+ */
+ protected static int randInt(int i0, int i1) {
+ double x = rand();
+ int i = i0 + (int) Math.floor((i1 - i0 + 1) * x);
+ return i;
+ }
+
+ /**
+ * Generate a random number from a uniform random variable.
+ *
+ * @param min min of the random variable.
+ * @param max max of the random variable.
+ * @return a double.
+ */
+ public static double uniform(double min, double max) {
+ double x = min + (max - min) * rand();
+ return x;
+ }
+
+ /**
+ * Generate a random number from a discrete random variable.
+ *
+ * @param values discrete values.
+ * @param prob probability of each value.
+ * @return a double.
+ */
+ public static double dirac(double[] values, double[] prob) {
+ double[] prob_cumul = new double[values.length];
+ prob_cumul[0] = prob[0];
+ for (int i = 1; i < values.length; i++) {
+ prob_cumul[i] = prob_cumul[i - 1] + prob[i];
+ }
+ double y = rand();
+ double x = 0;
+ for (int i = 0; i < values.length - 1; i++) {
+ if ((y > prob_cumul[i]) && (y < prob_cumul[i + 1])) {
+ x = values[i];
+ }
+ }
+ return x;
+ }
+
+ /**
+ * Generate a random number from a Gaussian (Normal) random variable.
+ *
+ * @param mu mean of the random variable.
+ * @param sigma standard deviation of the random variable.
+ * @return a double.
+ */
+ public static double normal(double mu, double sigma) {
+ double x = mu + sigma * Math.cos(2 * Math.PI * rand())
+ * Math.sqrt(-2 * Math.log(rand()));
+ return x;
+ }
+
+ /**
+ * Generate a random number from a Chi-2 random variable.
+ *
+ * @param n degrees of freedom of the chi2 random variable.
+ * @return a double.
+ */
+ public static double chi2(int n) {
+ double x = 0;
+ for (int i = 0; i < n; i++) {
+ double norm = normal(0, 1);
+ x += norm * norm;
+ }
+ return x;
+ }
+
+ /**
+ * Generate a random number from a LogNormal random variable.
+ *
+ * @param mu mean of the Normal random variable.
+ * @param sigma standard deviation of the Normal random variable.
+ * @return a double.
+ */
+ public static double logNormal(double mu, double sigma) {
+ double x = mu + sigma * Math.cos(2 * Math.PI * rand())
+ * Math.sqrt(-2 * Math.log(rand()));
+ return x;
+ }
+
+ /**
+ * Generate a random number from an exponantial random variable (Mean =
+ * 1/lambda, variance = 1/lambda^2).
+ *
+ * @param lambda parmaeter of the exponential random variable.
+ * @return a double.
+ */
+ public static double exponential(double lambda) {
+ double x = -1 / lambda * Math.log(rand());
+ return x;
+ }
+
+ /**
+ * Generate a random number from a symetric triangular random variable.
+ *
+ * @param min min of the random variable.
+ * @param max max of the random variable.
+ * @return a double.
+ */
+ public static double triangular(double min, double max) {
+ double x = min / 2 + (max - min) * rand() / 2 + min / 2 + (max - min)
+ * rand() / 2;
+ return x;
+ }
+
+ /**
+ * Generate a random number from a non-symetric triangular random variable.
+ *
+ * @param min min of the random variable.
+ * @param med value of the random variable with max density.
+ * @param max max of the random variable.
+ * @return a double.
+ */
+ public static double triangular(double min, double med, double max) {
+ double y = rand();
+ double x = (y < ((med - min) / (max - min))) ? (min + Math.sqrt(y
+ * (max - min) * (med - min))) : (max - Math.sqrt((1 - y) * (max - min)
+ * (max - med)));
+ return x;
+ }
+
+ /**
+ * Generate a random number from a beta random variable.
+ *
+ * @param a first parameter of the Beta random variable.
+ * @param b second parameter of the Beta random variable.
+ * @return a double.
+ */
+ public static double beta(double a, double b) {
+ double try_x;
+ double try_y;
+ do {
+ try_x = Math.pow(rand(), 1 / a);
+ try_y = Math.pow(rand(), 1 / b);
+ } while ((try_x + try_y) > 1);
+ return try_x / (try_x + try_y);
+ }
+
+ /**
+ * Generate a random number from a Cauchy random variable (Mean = Inf, and
+ * Variance = Inf).
+ *
+ * @param mu median of the Weibull random variable
+ * @param sigma second parameter of the Cauchy random variable.
+ * @return a double.
+ */
+ public static double cauchy(double mu, double sigma) {
+ double x = sigma * Math.tan(Math.PI * (rand() - 0.5)) + mu;
+ return x;
+ }
+
+ /**
+ * Generate a random number from a Weibull random variable.
+ *
+ * @param lambda first parameter of the Weibull random variable.
+ * @param c second parameter of the Weibull random variable.
+ * @return a double.
+ */
+ public static double weibull(double lambda, double c) {
+ double x = Math.pow(-Math.log(1 - rand()), 1 / c) / lambda;
+ return x;
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/TriangularMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/TriangularMatrix.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/TriangularMatrix.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/TriangularMatrix.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,174 @@
+/**
+ * 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.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+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.BatchUpdate;
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hadoop.io.Text;
+
+/**
+ * A library for mathematical operations on matrices of triangle forms
+ */
+public class TriangularMatrix extends AbstractMatrix {
+
+ /**
+ * Construct
+ *
+ * @param conf configuration object
+ */
+ public TriangularMatrix(Configuration conf) {
+ setConfiguration(conf);
+ }
+
+ /**
+ * Construct an triangular matrix
+ *
+ * @param conf configuration object
+ * @param matrixName the name of the triangular matrix
+ */
+ public TriangularMatrix(HBaseConfiguration conf, Text matrixName) {
+ try {
+ setConfiguration(conf);
+ this.matrixName = matrixName;
+
+ if (!admin.tableExists(matrixName)) {
+ tableDesc = new HTableDescriptor(matrixName.toString());
+ tableDesc.addFamily(new HColumnDescriptor(Constants.LOWER.toString()));
+ tableDesc.addFamily(new HColumnDescriptor(Constants.UPPER.toString()));
+ create();
+ }
+
+ table = new HTable(conf, matrixName);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /** {@inheritDoc} */
+ public Matrix multiply(Matrix b) {
+ return null;
+ }
+
+ /** {@inheritDoc} */
+ public Matrix addition(Matrix b) {
+ return null;
+ }
+
+ /** {@inheritDoc} */
+ public double determinant() {
+ return 0;
+ }
+
+ /** {@inheritDoc} */
+ public Matrix subtraction(Matrix b) {
+ return null;
+ }
+
+ /** {@inheritDoc} */
+ public TriangularMatrix decompose(Decomposition technique) {
+ return null;
+ }
+
+ /**
+ * Gets the entry value of lower matrix L(i, j)
+ *
+ * @param i ith row of lower matrix L
+ * @param j jth column of lower matrix L
+ * @return the value of entry
+ */
+ public double getFromLower(int i, int j) {
+ // If row doesn't exist, return the null pointer exception.
+ //So, we need to make a Cell object. -- Edward
+
+ Text row = new Text(String.valueOf(i));
+ Text column = new Text(Constants.LOWER + String.valueOf(j));
+
+ try {
+ Cell c = table.get(row, column);
+ if (c == null) {
+ return 0;
+ } else {
+ return toDouble(c.getValue());
+ }
+ } catch (IOException e) {
+ LOG.error(e, e);
+ return 0;
+ }
+ }
+
+ /**
+ * Gets the entry value of upper matrix U(i, j)
+ *
+ * @param i ith row of upper matrix U
+ * @param j jth column of upper matrix U
+ * @return the value of entry
+ */
+ public double getFromUpper(int i, int j) {
+ try {
+ return toDouble(table.get(new Text(String.valueOf(i)),
+ new Text(Constants.UPPER + String.valueOf(j))).getValue());
+ } catch (IOException e) {
+ LOG.error(e, e);
+ return 0;
+ }
+ }
+
+ /**
+ * Sets the double value of lower matrix L(i, j)
+ *
+ * @param i ith row of lower matrix L
+ * @param j jth column of lower matrix L
+ * @param d the value of entry
+ */
+ public void setToLower(int i, int j, double d) {
+ BatchUpdate b = new BatchUpdate(new Text(String.valueOf(i)));
+ b.put(new Text(Constants.LOWER + String.valueOf(j)), toBytes(d));
+ try {
+ table.commit(b);
+ } catch (IOException e) {
+ LOG.error(e, e);
+ }
+ }
+
+ /**
+ * Sets the double value of upper matrix U(i, j)
+ *
+ * @param i ith row of upper matrix U
+ * @param j jth column of upper matrix U
+ * @param d the value of entry
+ */
+ public void setToUpper(int i, int j, double d) {
+ BatchUpdate b = new BatchUpdate(new Text(String.valueOf(i)));
+ b.put(new Text(Constants.UPPER + String.valueOf(j)), toBytes(d));
+ try {
+ table.commit(b);
+ } catch (IOException e) {
+ LOG.error(e, e);
+ }
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionSubtractionMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionSubtractionMap.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionSubtractionMap.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionSubtractionMap.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,86 @@
+/**
+ * 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.Map;
+
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HStoreKey;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+import org.apache.hama.Matrix;
+import org.apache.hama.mapred.MatrixMap;
+import org.apache.log4j.Logger;
+
+/**
+ * Matrix addition/subtraction map
+ */
+public class AdditionSubtractionMap extends MatrixMap<Text, MapWritable> {
+ static final Logger LOG = Logger.getLogger(AdditionSubtractionMap.class);
+
+ protected Matrix matrix_b;
+ protected String operator;
+ public static final String MATRIX_B = "hama.addition.substraction.matrix.b";
+ public static final String OPERATOR = "hama.addition.substraction.operator";
+
+ public void configure(JobConf job) {
+ operator = job.get(OPERATOR, "");
+ Text b_name = new Text(job.get(MATRIX_B, ""));
+ matrix_b = new Matrix(new HBaseConfiguration(), b_name);
+ }
+
+ public static void initJob(String matrix_a, String matrix_b, String operator,
+ Class<AdditionSubtractionMap> map, JobConf jobConf) {
+ initJob(matrix_a, map, jobConf);
+ jobConf.set(MATRIX_B, matrix_b);
+ jobConf.set(OPERATOR, operator);
+ }
+
+ @Override
+ public void map(HStoreKey key, MapWritable value,
+ OutputCollector<Text, MapWritable> output, Reporter reporter)
+ throws IOException {
+ Text tKey = key.getRow();
+ MapWritable sum = new MapWritable();
+
+ for (Map.Entry<Writable, Writable> e : value.entrySet()) {
+ double a = getDouble(e.getValue());
+ double b = matrix_b.get(Integer.parseInt(tKey.toString()),
+ getIndex((Text) e.getKey()));
+ byte[] result = null;
+
+ if (operator.equals(Constants.PLUS)) {
+ result = toBytes(a + b);
+ } else if (operator.equals(Constants.MINUS)) {
+ result = toBytes(a - b);
+ }
+
+ sum.put(e.getKey(), new ImmutableBytesWritable(result));
+ }
+ output.collect(tKey, sum);
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionSubtractionReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionSubtractionReduce.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionSubtractionReduce.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionSubtractionReduce.java Tue Jun 24 03:36:21 2008
@@ -0,0 +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 java.util.Iterator;
+
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.mapred.MatrixReduce;
+
+/**
+ * Matrix addition/subtraction reduce
+ */
+public class AdditionSubtractionReduce extends MatrixReduce<Text, MapWritable> {
+
+ @Override
+ public void reduce(Text key, Iterator<MapWritable> values,
+ OutputCollector<Text, MapWritable> output, Reporter reporter)
+ throws IOException {
+ output.collect(key, values.next());
+ }
+
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/algebra/CholeskyDecompositionMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/CholeskyDecompositionMap.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/CholeskyDecompositionMap.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/CholeskyDecompositionMap.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,107 @@
+/**
+ * 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.HBaseConfiguration;
+import org.apache.hadoop.hbase.HStoreKey;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.FeatureVector;
+import org.apache.hama.Matrix;
+import org.apache.hama.TriangularMatrix;
+import org.apache.hama.mapred.MatrixMap;
+import org.apache.hama.mapred.MatrixOutputFormat;
+
+/**
+ * Cholesky Decomposition.
+ * <p>
+ * For a symmetric, positive definite matrix A, the Cholesky decomposition is an
+ * lower triangular matrix L so that A = L*L'.
+ * </p>
+ */
+public class CholeskyDecompositionMap extends MatrixMap<Text, MapWritable> {
+ protected Matrix matrix_a;
+ protected TriangularMatrix matrix_l;
+
+ public static final String MATRIX_A = "cholesky.decomposition.matrix.a";
+ public static final String MATRIX_L = "cholesky.decomposition.matrix.l";
+
+ public void configure(JobConf job) {
+ HBaseConfiguration conf = new HBaseConfiguration();
+ Text a_name = new Text(job.get(MATRIX_A, ""));
+ Text l_name = new Text(job.get(MATRIX_L, ""));
+ matrix_a = new Matrix(conf, a_name);
+ matrix_l = new TriangularMatrix(conf, l_name);
+ }
+
+ /**
+ * @param matrix_a
+ * @param matrix_l
+ * @param map
+ * @param jobConf
+ */
+ public static void initJob(String matrix_a, String matrix_l,
+ Class<CholeskyDecompositionMap> map, JobConf jobConf) {
+ initJob(matrix_a, map, jobConf);
+ jobConf.set(MatrixOutputFormat.OUTPUT, matrix_l);
+ jobConf.set(MATRIX_A, matrix_a);
+ jobConf.set(MATRIX_L, matrix_l);
+ }
+
+ @Override
+ public void map(HStoreKey key, MapWritable value,
+ OutputCollector<Text, MapWritable> output, Reporter reporter)
+ throws IOException {
+ int i = Integer.parseInt(String.valueOf(key.getRow()));
+
+ double[] ithRowOfL = new double[matrix_a.getRowDimension()];
+ double[] jthRowOfL = new double[matrix_a.getRowDimension()];
+
+ // Vector was added. I don't figure out the result. -- edward
+ FeatureVector iOfV = matrix_l.getRowVector(i);
+ for(int x = 0; x < iOfV.size(); x++) {
+ ithRowOfL[x] = iOfV.getDimAt(x);
+ }
+
+ for (int j = i; j < value.size(); j++) {
+ FeatureVector jOfV = matrix_l.getRowVector(i);
+ for(int x = 0; x < jOfV.size(); x++) {
+ jthRowOfL[x] = jOfV.getDimAt(x);
+ }
+
+ double sum = matrix_a.get(i, j);
+ for (int k = i - 1; k >= 0; k--) {
+ sum -= ithRowOfL[k] * jthRowOfL[k];
+ }
+
+ if (i == j) {
+ matrix_l.setToLower(j, i, Math.sqrt(sum));
+ } else {
+ double c = matrix_l.get(i, i);
+ matrix_l.setToLower(j, i, (sum / c));
+ }
+ }
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/algebra/CroutDecompositionMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/CroutDecompositionMap.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/CroutDecompositionMap.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/CroutDecompositionMap.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,131 @@
+/**
+ * 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.Map;
+
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HStoreKey;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.FractionMatrix;
+import org.apache.hama.TriangularMatrix;
+import org.apache.hama.mapred.MatrixMap;
+import org.apache.hama.mapred.MatrixOutputFormat;
+import org.apache.log4j.Logger;
+
+/**
+ * Crout Decomposition
+ */
+public class CroutDecompositionMap extends MatrixMap<Text, MapWritable> {
+ static final Logger LOG = Logger.getLogger(CroutDecompositionMap.class
+ .getName());
+ protected FractionMatrix matrix_a;
+ protected TriangularMatrix matrix_lu;
+
+ public static final String MATRIX_A = "crout.decompositionMap.matrix.a";
+ public static final String MATRIX_LU = "crout.decompositionMap.matrix.lu";
+
+ public void configure(JobConf job) {
+ HBaseConfiguration conf = new HBaseConfiguration();
+ Text a_name = new Text(job.get(MATRIX_A, ""));
+ Text lu_name = new Text(job.get(MATRIX_LU, ""));
+ matrix_a = new FractionMatrix(conf, a_name);
+ matrix_lu = new TriangularMatrix(conf, lu_name);
+ matrix_lu.setDimension(matrix_a.getRowDimension(), matrix_a
+ .getColumnDimension());
+ }
+
+ /**
+ * @param matrix_a
+ * @param matrix_lu
+ * @param map
+ * @param jobConf
+ */
+ public static void initJob(String matrix_a, String matrix_lu,
+ Class<CroutDecompositionMap> map, JobConf jobConf) {
+ initJob(matrix_a, map, jobConf);
+ jobConf.set(MatrixOutputFormat.OUTPUT, matrix_lu);
+ jobConf.set(MATRIX_A, matrix_a);
+ jobConf.set(MATRIX_LU, matrix_lu);
+ }
+
+ @SuppressWarnings("deprecation")
+ @Override
+ public void map(HStoreKey key, MapWritable value,
+ OutputCollector<Text, MapWritable> output, Reporter reporter)
+ throws IOException {
+ Text tKey = key.getRow();
+ int i = Integer.parseInt(String.valueOf(tKey.toString()));
+
+ for (Map.Entry<Writable, Writable> e : value.entrySet()) {
+ int j = getIndex((Text) e.getKey());
+ matrix_lu.setToUpper(i, j, upper(i, j));
+ matrix_lu.setToLower(i, j, lower(i, j));
+ }
+
+ }
+
+ public double upper(int i, int j) throws IOException {
+ double sum;
+ if (i == j)
+ return 1.0;
+ else if (i > j)
+ return 0.0;
+ else if (i == 0) {
+ return matrix_a.getFromOriginal(0, j) / matrix_a.getFromOriginal(0, 0);
+ } else {
+ sum = 0.0;
+ for (int k = 0; k < i; k++) {
+ sum += matrix_lu.getFromLower(i, k) * matrix_lu.getFromUpper(k, j);
+ }
+ double a = matrix_a.getFromOriginal(i, j) - sum;
+ double b = matrix_lu.getFromLower(i, i);
+ return a / b;
+ }
+ }
+
+ public double lower(int j, int i) throws IOException {
+ double sum, value;
+ if (i == 0)
+ value = matrix_a.getFromOriginal(j, 0);
+ else if (i > j)
+ value = 0.0;
+ else if (j == i) {
+ sum = 0.0;
+ for (int k = 0; k < i; k++) {
+ sum += matrix_lu.getFromLower(j, k) * matrix_lu.getFromUpper(k, i);
+ }
+ value = matrix_a.getFromOriginal(i, i) - sum;
+ } else {
+ sum = 0.0;
+ for (int k = 0; k < i; k++) {
+ sum += matrix_lu.getFromLower(j, k) * matrix_lu.getFromUpper(k, i);
+ }
+ value = matrix_a.getFromOriginal(j, i) - sum;
+ }
+ return value;
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/algebra/DeterminantMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/DeterminantMap.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/DeterminantMap.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/DeterminantMap.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,110 @@
+/**
+ * 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.HBaseConfiguration;
+import org.apache.hadoop.hbase.HStoreKey;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+import org.apache.hama.Matrix;
+import org.apache.hama.mapred.MatrixMap;
+import org.apache.hama.mapred.MatrixOutputFormat;
+import org.apache.log4j.Logger;
+
+/**
+ * Determinant map
+ */
+public class DeterminantMap extends MatrixMap<Text, MapWritable> {
+ static final Logger LOG = Logger.getLogger(DeterminantMap.class.getName());
+
+ protected Matrix matrix_a;
+ protected Matrix checkObj;
+
+ public static final String _CHECK = "hama.determinant.check";
+ public static final String MATRIX_A = "hama.determinant.matrix.a";
+ protected int row = 0;
+
+ public void configure(JobConf job) {
+ HBaseConfiguration conf = new HBaseConfiguration();
+ Text name_a = new Text(job.get(MATRIX_A, ""));
+ Text check = new Text(job.get(_CHECK, ""));
+
+ matrix_a = new Matrix(conf, name_a);
+ checkObj = new Matrix(conf, check);
+ row = matrix_a.getRowDimension();
+ }
+
+ /**
+ * @param matrix_a
+ * @param check
+ * @param map
+ * @param jobConf
+ */
+ public static void initJob(String matrix_a, String check,
+ Class<DeterminantMap> map, JobConf jobConf) {
+ initJob(matrix_a, map, jobConf);
+ jobConf.set(MatrixOutputFormat.OUTPUT, matrix_a);
+ jobConf.set(_CHECK, check);
+ jobConf.set(MATRIX_A, matrix_a);
+ }
+
+ @Override
+ public void map(HStoreKey key, MapWritable value,
+ OutputCollector<Text, MapWritable> output, Reporter reporter)
+ throws IOException {
+
+ int r = Integer.parseInt(String.valueOf(key.getRow()));
+
+ checkObj.set(r, 0, 0.0);
+ double d = matrix_a.get(r, 0) * Math.pow(-1.0, r) * minor(r, 1);
+ checkObj.set(r, 0, 1.0);
+
+ MapWritable val = new MapWritable();
+ val.put(Constants.COLUMN, getBytesWritable(d));
+ output.collect(Constants.DETERMINANT, val);
+ }
+
+ private double minor(int processRow, int processColumn)
+ throws IOException {
+ double result = 0.0;
+ int i = 0;
+ if ((row - processColumn) == 0) {
+ return 1.0;
+ }
+ for (int r = 0; r < row; r++) {
+ double trans = checkObj.get(r, 0);
+ if (trans != 0.0) {
+ checkObj.set(r, 0, 0.0);
+ result += matrix_a.get(r, processColumn) * Math.pow(-1.0, i)
+ * minor(r, processColumn + 1);
+ checkObj.set(r, 0, 1.0);
+
+ i++;
+ }
+ }
+ return result;
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/algebra/DeterminantReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/DeterminantReduce.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/DeterminantReduce.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/DeterminantReduce.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,53 @@
+/**
+ * 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 org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+import org.apache.hama.mapred.MatrixReduce;
+import org.apache.log4j.Logger;
+
+/**
+ * Aggregate determinant value
+ */
+public class DeterminantReduce extends MatrixReduce<Text, MapWritable> {
+ static final Logger LOG = Logger.getLogger(DeterminantReduce.class.getName());
+
+ @Override
+ public void reduce(Text key, Iterator<MapWritable> values,
+ OutputCollector<Text, MapWritable> output, Reporter reporter)
+ throws IOException {
+ double sum = 0;
+ while (values.hasNext()) {
+ sum += getDouble(values.next().get(Constants.COLUMN));
+ }
+
+ MapWritable value = new MapWritable();
+ value.put(Constants.COLUMN, getBytesWritable(sum));
+ output.collect(key, value);
+ }
+
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/algebra/MultiplicationMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/MultiplicationMap.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/MultiplicationMap.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/MultiplicationMap.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,83 @@
+/**
+ * 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.Map;
+
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HStoreKey;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.FeatureVector;
+import org.apache.hama.Matrix;
+import org.apache.hama.mapred.MatrixMap;
+import org.apache.log4j.Logger;
+
+/**
+ * Each map processor requires O(n^2).
+ */
+public class MultiplicationMap extends MatrixMap<Text, MapWritable> {
+ static final Logger LOG = Logger.getLogger(MultiplicationMap.class.getName());
+
+ protected Matrix matrix_b;
+ public static final String MATRIX_B = "hama.multiplication.matrix.b";
+
+ public void configure(JobConf job) {
+ Text name = new Text(job.get(MATRIX_B, ""));
+ matrix_b = new Matrix(new HBaseConfiguration(), name);
+ }
+
+ /**
+ * @param matrix_a
+ * @param matrix_b
+ * @param map
+ * @param jobConf
+ */
+ public static void initJob(String matrix_a, String matrix_b,
+ Class<MultiplicationMap> map, JobConf jobConf) {
+ initJob(matrix_a, map, jobConf);
+ jobConf.set(MATRIX_B, matrix_b);
+ }
+
+ @Override
+ public void map(HStoreKey key, MapWritable value,
+ OutputCollector<Text, MapWritable> output, Reporter reporter)
+ throws IOException {
+ MapWritable multipliedValue = new MapWritable();
+
+ for (Map.Entry<Writable, Writable> e : value.entrySet()) {
+ int row = getIndex((Text) e.getKey());
+ double c = getDouble(e.getValue());
+
+ FeatureVector v = matrix_b.getRowVector(row);
+ for(int i = 0; i < v.size(); i++) {
+ // GetColumnText doesn't needed -- edward
+ multipliedValue.put(getColumnText(i), getBytesWritable(v.getValueAt(i) * c));
+ }
+
+ output.collect(key.getRow(), multipliedValue);
+ }
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/algebra/MultiplicationReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/MultiplicationReduce.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/MultiplicationReduce.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/MultiplicationReduce.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,64 @@
+/**
+ * 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.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.mapred.MatrixReduce;
+
+/**
+ * Accumulate the multiplied values.
+ */
+public class MultiplicationReduce extends MatrixReduce<Text, MapWritable> {
+
+ @Override
+ public void reduce(Text key, Iterator<MapWritable> values,
+ OutputCollector<Text, MapWritable> output, Reporter reporter)
+ throws IOException {
+ Map<String, Double> sum = new HashMap<String, Double>();
+
+ while (values.hasNext()) {
+ for (Map.Entry<Writable, Writable> e : values.next().entrySet()) {
+ String sKey = e.getKey().toString();
+ if (sum.containsKey(sKey)) {
+ double before = sum.get(e.getKey().toString());
+ sum.put(sKey, before + getDouble(e.getValue()));
+ } else {
+ sum.put(sKey, getDouble(e.getValue()));
+ }
+ }
+ }
+
+ MapWritable value = new MapWritable();
+ for (Map.Entry<String, Double> f : sum.entrySet()) {
+ Writable val = getBytesWritable(f.getValue());
+ value.put(new Text(f.getKey()), val);
+ }
+ output.collect(key, value);
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/algebra/package.html
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/package.html?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/package.html (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/package.html Tue Jun 24 03:36:21 2008
@@ -0,0 +1,23 @@
+<html>
+
+<!--
+ 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.
+-->
+
+<body>
+Map/Reduce-Based Basic Linear Algebra Classes
+</body>
+</html>
Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/FractionMatrixInputFormat.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/FractionMatrixInputFormat.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/FractionMatrixInputFormat.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/FractionMatrixInputFormat.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,77 @@
+/**
+ * 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.Map;
+
+import org.apache.hadoop.hbase.HStoreKey;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.InputFormat;
+import org.apache.hadoop.mapred.InputSplit;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.JobConfigurable;
+import org.apache.hadoop.mapred.RecordReader;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+
+public class FractionMatrixInputFormat extends InputFormatBase implements
+ InputFormat<HStoreKey, MapWritable>, JobConfigurable {
+
+ Text[] m_cols = new Text[] { Constants.DENOMINATOR, Constants.NUMERATOR,
+ Constants.ORIGINAL };
+
+ public RecordReader<HStoreKey, MapWritable> getRecordReader(InputSplit split,
+ JobConf job, Reporter reporter) throws IOException {
+ MatrixSplit tSplit = (MatrixSplit) split;
+
+ Text start = tSplit.getStartRow();
+ Text end = tSplit.getEndRow();
+
+ return new FractionMatrixReader(start, end, m_table, m_cols);
+ }
+
+ class FractionMatrixReader extends MatrixRecordReader {
+
+ public FractionMatrixReader(Text startRow, Text endRow, HTable table,
+ Text[] cols) throws IOException {
+ super(startRow, endRow, table, cols);
+ }
+
+ public boolean next(HStoreKey key, MapWritable value) throws IOException {
+ m_row.clear();
+ HStoreKey tKey = key;
+
+ boolean hasMore = m_scanner.next(tKey, m_row);
+ if (hasMore) {
+ value.clear();
+ for (Map.Entry<Text, byte[]> e : m_row.entrySet()) {
+ if (e.getKey().toString().startsWith(Constants.ORIGINAL.toString())) {
+ value.put(e.getKey(), new ImmutableBytesWritable(e.getValue()));
+ }
+ }
+ }
+ return hasMore;
+ }
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/InputFormatBase.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/InputFormatBase.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/InputFormatBase.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/InputFormatBase.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,94 @@
+/**
+ * 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.fs.Path;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HStoreKey;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.InputFormat;
+import org.apache.hadoop.mapred.InputSplit;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.JobConfigurable;
+import org.apache.hadoop.mapred.RecordReader;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+import org.apache.log4j.Logger;
+
+public abstract class InputFormatBase extends MatrixMapReduce implements
+ InputFormat<HStoreKey, MapWritable>, JobConfigurable {
+ static final Logger LOG = Logger.getLogger(InputFormatBase.class.getName());
+
+ private Text m_tableName;
+ Text[] m_cols;
+ HTable m_table;
+
+ public abstract RecordReader<HStoreKey, MapWritable> getRecordReader(
+ InputSplit split, JobConf job, Reporter report) throws IOException;
+
+ /**
+ * @see org.apache.hadoop.mapred.InputFormat#getSplits(org.apache.hadoop.mapred.JobConf, int)
+ */
+ @SuppressWarnings("unused")
+ public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
+ Cell meta = m_table.get(Constants.METADATA, Constants.METADATA_ROWS);
+
+ if (bytesToInt(meta.getValue()) < numSplits) {
+ numSplits = bytesToInt(meta.getValue());
+ }
+
+ Text[] startKeys = new Text[numSplits];
+ int interval = bytesToInt(meta.getValue()) / numSplits;
+
+ for (int i = 0; i < numSplits; i++) {
+ int row = (i * interval);
+ startKeys[i] = new Text(String.valueOf(row));
+ }
+
+ InputSplit[] splits = new InputSplit[startKeys.length];
+ for (int i = 0; i < startKeys.length; i++) {
+ splits[i] = new MatrixSplit(m_tableName, startKeys[i],
+ ((i + 1) < startKeys.length) ? startKeys[i + 1] : new Text());
+ }
+ return splits;
+ }
+
+ public void configure(JobConf job) {
+ Path[] tableNames = job.getInputPaths();
+ m_tableName = new Text(tableNames[0].getName());
+ try {
+ m_table = new HTable(new HBaseConfiguration(job), m_tableName);
+ } catch (IOException e) {
+ LOG.info(e);
+ }
+ }
+
+ public void validateInput(JobConf job) throws IOException {
+ Path[] tableNames = job.getInputPaths();
+ if (tableNames == null || tableNames.length > 1) {
+ throw new IOException("expecting one table name");
+ }
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormat.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormat.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormat.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormat.java Tue Jun 24 03:36:21 2008
@@ -0,0 +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 java.util.Map;
+
+import org.apache.hadoop.hbase.HStoreKey;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.InputFormat;
+import org.apache.hadoop.mapred.InputSplit;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.JobConfigurable;
+import org.apache.hadoop.mapred.RecordReader;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+
+public class MatrixInputFormat extends InputFormatBase implements
+ InputFormat<HStoreKey, MapWritable>, JobConfigurable {
+ Text[] m_cols = new Text[] { Constants.COLUMN };
+
+ public RecordReader<HStoreKey, MapWritable> getRecordReader(InputSplit split,
+ JobConf job, Reporter reporter) throws IOException {
+ MatrixSplit tSplit = (MatrixSplit) split;
+
+ Text start = tSplit.getStartRow();
+ Text end = tSplit.getEndRow();
+
+ return new MatrixReader(start, end, m_table, m_cols);
+ }
+
+ class MatrixReader extends MatrixRecordReader {
+
+ public MatrixReader(Text startRow, Text endRow, HTable table, Text[] cols)
+ throws IOException {
+ super(startRow, endRow, table, cols);
+ }
+
+ /**
+ * @param key HStoreKey as input key.
+ * @param value MapWritable as input value
+ *
+ * Converts HScannerInterface.next(IntWritable, SortedMap<Text, byte[]>) to
+ * IntWritable, MapWritable<Text, ImmutableBytesWritable>
+ *
+ * @return true if there was more data
+ * @throws IOException
+ */
+ @SuppressWarnings("unchecked")
+ public boolean next(HStoreKey key, MapWritable value) throws IOException {
+ m_row.clear();
+ HStoreKey tKey = key;
+
+ boolean hasMore = m_scanner.next(tKey, m_row);
+ if (hasMore) {
+ value.clear();
+ for (Map.Entry<Text, byte[]> e : m_row.entrySet()) {
+ value.put(e.getKey(), new ImmutableBytesWritable(e.getValue()));
+ }
+ }
+ return hasMore;
+ }
+ }
+}
Added: 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=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,72 @@
+/**
+ * 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.fs.Path;
+import org.apache.hadoop.hbase.HStoreKey;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.Mapper;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+
+/**
+ * Scan an HBase table to sort by a specified sort column. If the column does
+ * not exist, the record is not passed to Reduce.
+ *
+ */
+@SuppressWarnings("unchecked")
+public abstract class MatrixMap<K extends WritableComparable, V extends Writable>
+ extends MatrixMapReduce implements Mapper<HStoreKey, MapWritable, K, V> {
+
+ /**
+ * Use this before submitting a MatrixMap job. It will appropriately set up
+ * the JobConf.
+ *
+ * @param matrixA table name
+ * @param mapper mapper class
+ * @param job job configuration
+ */
+ public static void initJob(String matrixA, Class<? extends MatrixMap> mapper,
+ JobConf job) {
+ job.setOutputKeyClass(Text.class);
+ job.setOutputValueClass(MapWritable.class);
+ job.setMapperClass(mapper);
+ job.setInputPath(new Path(matrixA));
+ }
+
+ /**
+ * Call a user defined function on a single HBase record, represented by a key
+ * and its associated record value.
+ *
+ * @param key
+ * @param value
+ * @param output
+ * @param reporter
+ * @throws IOException
+ */
+ public abstract void map(HStoreKey key, MapWritable value,
+ OutputCollector<K, V> output, Reporter reporter) throws IOException;
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMapReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMapReduce.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMapReduce.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMapReduce.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,137 @@
+/**
+ * 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.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.mapred.MapReduceBase;
+import org.apache.hama.Constants;
+
+public class MatrixMapReduce extends MapReduceBase {
+
+ /**
+ * Converts the double to bytes
+ *
+ * @param doubleVal
+ * @return bytes
+ */
+ public byte[] toBytes(double doubleVal) {
+ byte[] dByteArray = new byte[8];
+ long n = Double.doubleToLongBits(doubleVal);
+
+ for (int i = 0; i < dByteArray.length; i++) {
+ dByteArray[i] = (byte) (n >> (i * 8) & 0xFF);
+ }
+
+ return dByteArray;
+ }
+
+ /**
+ * Converts the bytes to double
+ *
+ * @param inBytes
+ * @return double
+ */
+ public double toDouble(byte[] inBytes) {
+ if (inBytes == null) {
+ return 0;
+ }
+
+ long n = 0;
+ for (int i = 0; i < inBytes.length; i++) {
+ n |= ((long) (inBytes[i] & 0377) << (i * 8));
+ }
+
+ double doubleValue = Double.longBitsToDouble(n);
+
+ return doubleValue;
+ }
+
+ /**
+ * Converts the int to byte array
+ *
+ * @param i
+ * @return Byte Array
+ */
+ public byte[] intToBytes(int i) {
+ ByteBuffer bb = ByteBuffer.allocate(4);
+ bb.order(ByteOrder.nativeOrder());
+ bb.putInt(i);
+ return bb.array();
+ }
+
+ /**
+ * Converts the bytes to int
+ *
+ * @param bytes
+ * @return Integer value
+ */
+ public int bytesToInt(byte[] bytes) {
+ ByteBuffer bb = ByteBuffer.wrap(bytes);
+ bb.order(ByteOrder.nativeOrder());
+ return bb.getInt();
+ }
+
+ /**
+ * Converts the double to ImmutableBytesWritable
+ *
+ * @param d
+ * @return ImmutableBytesWritable
+ */
+ public ImmutableBytesWritable getBytesWritable(double d) {
+ return new ImmutableBytesWritable(toBytes(d));
+ }
+
+ /**
+ * Converts the writable to double
+ *
+ * @param value
+ * @return double
+ */
+ public double getDouble(Writable value) {
+ return toDouble(((ImmutableBytesWritable) value).get());
+ }
+
+ /**
+ * Return the integer index
+ *
+ * @param key
+ * @return integer
+ */
+ public int getIndex(Text key) {
+ String sKey = key.toString();
+ return Integer.parseInt(sKey
+ .substring(sKey.indexOf(":") + 1, sKey.length()));
+ }
+
+ /**
+ * Return the column key
+ *
+ * @param i
+ * @return text
+ */
+ public Text getColumnText(int i) {
+ return new Text(Constants.COLUMN + String.valueOf(i));
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixOutputFormat.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixOutputFormat.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixOutputFormat.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixOutputFormat.java Tue Jun 24 03:36:21 2008
@@ -0,0 +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.mapred;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Map;
+
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.io.BatchUpdate;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.mapred.FileAlreadyExistsException;
+import org.apache.hadoop.mapred.InvalidJobConfException;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.OutputFormatBase;
+import org.apache.hadoop.mapred.RecordWriter;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hadoop.util.Progressable;
+import org.apache.hama.Constants;
+import org.apache.log4j.Logger;
+
+public class MatrixOutputFormat extends OutputFormatBase<Text, MapWritable> {
+
+ /** JobConf parameter that specifies the output */
+ public static final String OUTPUT = "hbase.mapred.output";
+ public int rowCount = 0;
+ public int columnCount = 0;
+ static final Logger LOG = Logger
+ .getLogger(MatrixOutputFormat.class.getName());
+
+ /** constructor */
+ public MatrixOutputFormat() {
+ super();
+ }
+
+ /**
+ * Convert Reduce output (key, value) to (HStoreKey, KeyedDataArrayWritable)
+ * and write to an HBase table
+ */
+ protected class TableRecordWriter implements RecordWriter<Text, MapWritable> {
+ private HTable m_table;
+
+ /**
+ * Instantiate a TableRecordWriter with the HBase HClient for writing.
+ *
+ * @param table
+ */
+ public TableRecordWriter(HTable table) {
+ m_table = table;
+ }
+
+ /** {@inheritDoc} */
+ public void close(@SuppressWarnings("unused")
+ Reporter reporter) {
+ if (rowCount > 0 && columnCount > 0) {
+ BatchUpdate b = new BatchUpdate(Constants.METADATA);
+ b.put(Constants.METADATA_ROWS, intToBytes(rowCount));
+ b.put(Constants.METADATA_COLUMNS, intToBytes(columnCount));
+ try {
+ m_table.commit(b);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * Converts the int to byte array
+ *
+ * @param i
+ * @return Byte Array
+ */
+ public byte[] intToBytes(int i) {
+ ByteBuffer bb = ByteBuffer.allocate(4);
+ bb.order(ByteOrder.nativeOrder());
+ bb.putInt(i);
+ return bb.array();
+ }
+
+ /** {@inheritDoc} */
+ public void write(Text key, MapWritable value) throws IOException {
+
+ BatchUpdate b = new BatchUpdate(key);
+ b.put(Constants.METADATA_ROWS, intToBytes(rowCount));
+ b.put(Constants.METADATA_COLUMNS, intToBytes(columnCount));
+
+ rowCount++;
+ LOG.info(columnCount);
+ if (columnCount < value.keySet().size())
+ columnCount = value.keySet().size();
+
+ for (Map.Entry<Writable, Writable> e : value.entrySet()) {
+ b.put((Text) e.getKey(), ((ImmutableBytesWritable) e.getValue()).get());
+ }
+ m_table.commit(b); // end transaction
+ }
+
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ @SuppressWarnings("unchecked")
+ public RecordWriter getRecordWriter(FileSystem ignored, JobConf job,
+ String name, Progressable progress) throws IOException {
+
+ // expecting exactly one path
+
+ Text tableName = new Text(job.get(OUTPUT));
+ HTable table = null;
+ try {
+ table = new HTable(new HBaseConfiguration(job), tableName);
+ } catch (IOException e) {
+ LOG.error(e);
+ throw e;
+ }
+ return new TableRecordWriter(table);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ @SuppressWarnings("unused")
+ public void checkOutputSpecs(FileSystem ignored, JobConf job)
+ throws FileAlreadyExistsException, InvalidJobConfException, IOException {
+
+ String tableName = job.get(OUTPUT);
+ if (tableName == null) {
+ throw new IOException("Must specify table name");
+ }
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixRecordReader.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixRecordReader.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixRecordReader.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixRecordReader.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,72 @@
+package org.apache.hama.mapred;
+
+import java.io.IOException;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+import org.apache.hadoop.hbase.HScannerInterface;
+import org.apache.hadoop.hbase.HStoreKey;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.RecordReader;
+
+/**
+ * Iterate over an HBase table data, return (IntWritable, MapWritable<Text,
+ * ImmutableBytesWritable>) pairs
+ */
+public abstract class MatrixRecordReader implements RecordReader<HStoreKey, MapWritable> {
+ protected final HScannerInterface m_scanner;
+ protected final SortedMap<Text, byte[]> m_row = new TreeMap<Text, byte[]>();
+
+ /**
+ * Constructor
+ *
+ * @param startRow start row of block
+ * @param endRow end row of block
+ * @param table HTable object
+ * @param cols column specification
+ * @throws IOException
+ */
+ public MatrixRecordReader(Text startRow, Text endRow, HTable table,
+ Text[] cols) throws IOException {
+ if (endRow != null && endRow.getLength() > 0) {
+ this.m_scanner = table.obtainScanner(cols, startRow, endRow);
+ } else {
+ this.m_scanner = table.obtainScanner(cols, startRow);
+ }
+ }
+
+ public void close() throws IOException {
+ this.m_scanner.close();
+ }
+
+ /**
+ * @return HStoreKey
+ *
+ * @see org.apache.hadoop.mapred.RecordReader#createKey()
+ */
+ public HStoreKey createKey() {
+ return new HStoreKey();
+ }
+
+ /**
+ * @return MapWritable
+ *
+ * @see org.apache.hadoop.mapred.RecordReader#createValue()
+ */
+ @SuppressWarnings("unchecked")
+ public MapWritable createValue() {
+ return new MapWritable();
+ }
+
+ /** {@inheritDoc} */
+ public long getPos() {
+ return 0;
+ }
+
+ /** {@inheritDoc} */
+ public float getProgress() {
+ return 0;
+ }
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixReduce.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixReduce.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixReduce.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,65 @@
+/**
+ * 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.Iterator;
+
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reducer;
+import org.apache.hadoop.mapred.Reporter;
+
+/**
+ * Write a table, sorting by the input key
+ */
+@SuppressWarnings("unchecked")
+public abstract class MatrixReduce<K extends WritableComparable, V extends Writable>
+ extends MatrixMapReduce implements Reducer<K, V, Text, MapWritable> {
+
+ /**
+ * Use this before submitting a TableReduce job. It will appropriately set up
+ * the JobConf.
+ *
+ * @param table
+ * @param reducer
+ * @param job
+ */
+ public static void initJob(String table,
+ Class<? extends MatrixReduce> reducer, JobConf job) {
+ job.setReducerClass(reducer);
+ job.set(MatrixOutputFormat.OUTPUT, table);
+ }
+
+ /**
+ * @param key
+ * @param values
+ * @param output
+ * @param reporter
+ * @throws IOException
+ */
+ public abstract void reduce(K key, Iterator<V> values,
+ OutputCollector<Text, MapWritable> output, Reporter reporter)
+ throws IOException;
+}
Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixSplit.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixSplit.java?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixSplit.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixSplit.java Tue Jun 24 03:36:21 2008
@@ -0,0 +1,99 @@
+/**
+ * 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.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.InputSplit;
+
+public class MatrixSplit implements InputSplit {
+ private Text m_tableName;
+ private Text m_startRow;
+ private Text m_endRow;
+
+ /** default constructor */
+ public MatrixSplit() {
+ m_tableName = new Text();
+ m_startRow = new Text();
+ m_endRow = new Text();
+ }
+
+ /**
+ * Constructor
+ *
+ * @param tableName
+ * @param startRow
+ * @param endRow
+ */
+ public MatrixSplit(Text tableName, Text startRow, Text endRow) {
+ this();
+ m_tableName.set(tableName);
+ m_startRow.set(startRow);
+ m_endRow.set(endRow);
+ }
+
+ /** @return table name */
+ public Text getTableName() {
+ return m_tableName;
+ }
+
+ /** @return starting row key */
+ public Text getStartRow() {
+ return m_startRow;
+ }
+
+ /** @return end row key */
+ public Text getEndRow() {
+ return m_endRow;
+ }
+
+ /** {@inheritDoc} */
+ public long getLength() {
+ return 0;
+ }
+
+ /** {@inheritDoc} */
+ public String[] getLocations() {
+ return new String[] {};
+ }
+
+ /** {@inheritDoc} */
+ public void readFields(DataInput in) throws IOException {
+ m_tableName.readFields(in);
+ m_startRow.readFields(in);
+ m_endRow.readFields(in);
+ }
+
+ /** {@inheritDoc} */
+ public void write(DataOutput out) throws IOException {
+ m_tableName.write(out);
+ m_startRow.write(out);
+ m_endRow.write(out);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public String toString() {
+ return m_tableName + "," + m_startRow + "," + m_endRow;
+ }
+}
\ No newline at end of file
Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/package.html
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/package.html?rev=671120&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/package.html (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/package.html Tue Jun 24 03:36:21 2008
@@ -0,0 +1,23 @@
+<html>
+
+<!--
+ 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.
+-->
+
+<body>
+Matrix Input/Output Formatter
+</body>
+</html>