hadoop - 自定义hadoop键和值: How to write CompareTo() Method

标签 hadoop mapreduce

我需要从映射器发出一个 2D double 组作为键和值。 Stack Overflow 上发布了一些问题,但没有得到解答。

我正在给定数据集中进行一些矩阵乘法,之后我需要发出 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.

我写了我的类(class):

更新

    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();



  }

}

我能够从映射器发出一个值作为矩阵

context.write(...,new MatrixWritable(AAtrans));

如何从映射器发出矩阵 AtransD 作为键?

为此我需要编写compareto()方法,对吧?

该方法中应该包含什么?

最佳答案

首先,要实现自定义键,您必须实现WritableComparable。要实现自定义值,您必须实现Writable。在许多情况下,由于能够方便地交换键和值,因此大多数人将所有自定义类型编写为 WritableComparable

这里是Hadoop:权威指南部分的链接,其中介绍了WritableComparable的编写。 Writing A Custom Writable

写出数组的技巧是在读取端您需要知道要读取多少个元素。所以基本的模式是......

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]);
 }

关于hadoop - 自定义hadoop键和值: How to write CompareTo() Method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655071/

相关文章:

sql - 在 Hive 中查询

hadoop - 亚马逊弹性 MapReduce : the number of launched map task

hadoop - 级联函数在单个线程中作为hadoop映射器函数执行吗?

hadoop - 映射器输出直接打印到输出文件

hadoop - 如何将键值数据加载到hbase表中?

hadoop - 绕过hadoop中Mapreduce作业的洗牌阶段?

hadoop - Mapreduce 将值链接到每个键的列表中

hadoop - 在 hive/hbase 中更新查询

hadoop - Apache Tika 无法解析 HDFS 文件

hadoop - Hadoop Oozie shell 操作中的 kerberos 票证和委托(delegate) token 使用