복붙노트

[HADOOP] 사용자 정의 키 및 값 : CompareTo () 메서드 작성 방법

HADOOP

사용자 정의 키 및 값 : CompareTo () 메서드 작성 방법

mapper에서 키와 값으로 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.

나는 수업을 썼다.

최신 정보

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



  }

}

매퍼 (mapper)에서 행렬로서 값을 낼 수있다.

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

mapper에서 행렬 AtransD를 키로 내보내는 방법은 무엇입니까?

그 때문에 compareto () 메서드를 작성해야합니다. 맞습니까?

그 방법에 무엇을 포함시켜야합니까?

해결법

  1. ==============================

    1.첫째, 사용자 지정 키를 구현하려면 WritableComparable을 구현해야합니다. 사용자 정의 값을 구현하려면 Writable을 구현해야합니다. 많은 경우 키와 값을 바꿀 수 있기 때문에 대부분의 사용자는 모든 사용자 정의 유형을 WritableComparable로 작성합니다.

    첫째, 사용자 지정 키를 구현하려면 WritableComparable을 구현해야합니다. 사용자 정의 값을 구현하려면 Writable을 구현해야합니다. 많은 경우 키와 값을 바꿀 수 있기 때문에 대부분의 사용자는 모든 사용자 정의 유형을 WritableComparable로 작성합니다.

    다음은 Hadoop 섹션에 대한 링크입니다 : WritableComparable 작성에 관한 확실한 안내서. 사용자 정의 쓰기 가능 쓰기

    배열을 작성하는 트릭은 읽기 측면에서 몇 개의 요소를 읽어야하는지 알 필요가 있다는 것입니다. 그래서 기본적인 후판은 ...

    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]);
     }
    
  2. from https://stackoverflow.com/questions/19655071/custom-hadoop-key-and-value-how-to-write-compareto-method by cc-by-sa and MIT license