自定义hadoop键和值:如何编写CompareTo()方法
我需要从mapper发出一个二维双数组作为键和值。堆栈溢出中发布了一些问题,但没有得到回答 我在给定的数据集中执行一些矩阵乘法,然后我需要发出自定义hadoop键和值:如何编写CompareTo()方法,hadoop,mapreduce,Hadoop,Mapreduce,我需要从mapper发出一个二维双数组作为键和值。堆栈溢出中发布了一些问题,但没有得到回答 我在给定的数据集中执行一些矩阵乘法,然后我需要发出a*Atrns的值,它将是一个矩阵作为键,Atrans*D也将是一个矩阵作为值。那么如何从映射器中发出这些矩阵。值应该与键本身相对应 ie key -----> A*Atrans--------->after multiplication the result will be a 2D array which is declared as d
a*Atrns
的值,它将是一个矩阵作为键,Atrans*D
也将是一个矩阵作为值。那么如何从映射器中发出这些矩阵。值应该与键本身相对应
ie key -----> A*Atrans--------->after multiplication the result will be a 2D array which is declared as double (matrix) lets say the result be Matrix "Ekey"(double[][] Ekey)
value ------> Atrans*D ---------> after multiplication the result will be Matrix "Eval" (double[][] Eval).
After that I need to emit these matrix to reducer for further calculations.
So in mapper:
context.write(Ekey,Eval);
Reducer:
I need to do further calculations with these Ekey and Eval.
我写了我的班级:
更新
public class MatrixWritable implements WritableComparable<MatrixWritable>{
/**
* @param args
*/
private double[][] value;
private double[][] values;
public MatrixWritable() {
// TODO Auto-generated constructor stub
setValue(new double[0][0]);
}
public MatrixWritable(double[][] value) {
// TODO Auto-generated constructor stub
this.value = value;
}
public void setValue(double[][] value) {
this.value = value;
}
public double[][] getValue() {
return values;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(value.length); // write values
for (int i = 0; i < value.length; i++) {
out.writeInt(value[i].length);
}
for (int i = 0; i < value.length; i++) {
for (int j = 0; j < value[i].length; j++) {
out.writeDouble(value[i][j]);
}
}
}
@Override
public void readFields(DataInput in) throws IOException {
value = new double[in.readInt()][];
for (int i = 0; i < value.length; i++) {
value[i] = new double[in.readInt()];
}
values = new double[value.length][value[0].length];
for(int i=0;i<value.length ; i++){
for(int j= 0 ; j< value[0].length;j++){
values[i][j] = in.readDouble();
}
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(value);
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof MatrixWritable)) {
return false;
}
MatrixWritable other = (MatrixWritable) obj;
if (!Arrays.deepEquals(value, other.value)) {
return false;
}
return true;
}
@Override
public int compareTo(MatrixWritable o) {
// TODO Auto-generated method stub
return 0;
}
public String toString() { String separator = "|";
StringBuffer result = new StringBuffer();
// iterate over the first dimension
for (int i = 0; i < values.length; i++) {
// iterate over the second dimension
for(int j = 0; j < values[i].length; j++){
result.append(values[i][j]);
result.append(separator);
}
// remove the last separator
result.setLength(result.length() - separator.length());
// add a line break.
result.append(",");
}
return result.toString();
}
}
如何将矩阵AtransD作为键从映射器发出
为此,我需要编写compareto()方法,对吗
该方法应包括哪些内容 首先,要实现自定义密钥,您必须实现
WritableComparable
。要实现自定义值,必须实现可写
。在许多情况下,由于可以方便地交换键和值,大多数人将所有自定义类型编写为writeablecomparable
这里有一个链接,指向Hadoop:the definal Guide
一节,该节介绍了编写writeablecomparable
写出数组的诀窍在于,在读取端,您需要知道要读取多少个元素。所以基本模式是
On write:
write the number of elements
write each element
On read:
read the number of elements (n)
create an array of the appropriate size
read 0 - (n-1) elements and populate array
更新
您应该在默认构造函数中将数组实例化为空,以防止以后出现NullPointerException
实现的问题在于,它假定每个内部数组的长度相同。如果这是真的,则不需要多次计算列长度。如果为false,则需要在写入行的值之前写入每行的长度
我建议这样做:
context.write(row); // as calculated above
for (int i=0; i<row; i++){
double[] rowVals = array[row];
context.write(rowVals.length);
for (int j=0; j<rowVals.length; j++)
context.write(rowVals[j]);
}
context.write(行);//如上计算
对于(In i=0;ISO i SLD覆盖所有的方法)?这个可写的东西是什么意思?该方法被声明为我们可以读取一个双数组或矩阵。1。根据定义,因为<代码>可写可写的< /代码>是<代码>接口<代码>。您必须实现在接口中定义的所有方法。提供了一个自定义的toString
、equals
和hashcode
实现。在过去的一周里,有很多问题被否决了,我能想到的唯一原因是它已经被问了很多次。我将对它进行投票。@JohnB:很抱歉,我没有得到你的答案。为什么行和列被表示为数组。为什么是上下文。请写进去writable class.it is out.writeInt.na?很快被写入,应该是out.writeInt()
Duplicate:
context.write(row); // as calculated above
for (int i=0; i<row; i++){
double[] rowVals = array[row];
context.write(rowVals.length);
for (int j=0; j<rowVals.length; j++)
context.write(rowVals[j]);
}