Search in sources :

Example 6 with CrunchRuntimeException

use of org.apache.crunch.impl.mr.run.CrunchRuntimeException in project crunch by cloudera.

the class Writables method deepCopy.

/**
   * Perform a deep copy of a writable value.
   * 
   * @param value
   *          The value to be copied
   * @param writableClass
   *          The Writable class of the value to be copied
   * @return A fully detached deep copy of the input value
   */
public static <T extends Writable> T deepCopy(T value, Class<T> writableClass) {
    ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
    DataOutputStream dataOut = new DataOutputStream(byteOutStream);
    T copiedValue = null;
    try {
        value.write(dataOut);
        dataOut.flush();
        ByteArrayInputStream byteInStream = new ByteArrayInputStream(byteOutStream.toByteArray());
        DataInput dataInput = new DataInputStream(byteInStream);
        copiedValue = writableClass.newInstance();
        copiedValue.readFields(dataInput);
    } catch (Exception e) {
        throw new CrunchRuntimeException("Error while deep copying " + value, e);
    }
    return copiedValue;
}
Also used : DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) CrunchRuntimeException(org.apache.crunch.impl.mr.run.CrunchRuntimeException) CrunchRuntimeException(org.apache.crunch.impl.mr.run.CrunchRuntimeException)

Aggregations

CrunchRuntimeException (org.apache.crunch.impl.mr.run.CrunchRuntimeException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 Path (org.apache.hadoop.fs.Path)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInput (java.io.DataInput)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 URI (java.net.URI)1 Pair (org.apache.crunch.Pair)1 MRPipeline (org.apache.crunch.impl.mr.MRPipeline)1 SourcePathTargetImpl (org.apache.crunch.io.impl.SourcePathTargetImpl)1 PTypeFamily (org.apache.crunch.types.PTypeFamily)1 Configuration (org.apache.hadoop.conf.Configuration)1 HBaseConfiguration (org.apache.hadoop.hbase.HBaseConfiguration)1