Java Code Examples for org.apache.hadoop.io.Writable#readFields()

The following examples show how to use org.apache.hadoop.io.Writable#readFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinaryProtocol.java    From hadoop-gpu with Apache License 2.0 6 votes vote down vote up
private void readObject(Writable obj) throws IOException {
  int numBytes = WritableUtils.readVInt(inStream);
  byte[] buffer;
  // For BytesWritable and Text, use the specified length to set the length
  // this causes the "obvious" translations to work. So that if you emit
  // a string "abc" from C++, it shows up as "abc".
  if (obj instanceof BytesWritable) {
    buffer = new byte[numBytes];
    inStream.readFully(buffer);
    ((BytesWritable) obj).set(buffer, 0, numBytes);
  } else if (obj instanceof Text) {
    buffer = new byte[numBytes];
    inStream.readFully(buffer);
    ((Text) obj).set(buffer);
  } else {
    obj.readFields(inStream);
  }
}
 
Example 2
Source File: Writables.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Set bytes into the passed Writable by calling its
 * {@link Writable#readFields(java.io.DataInput)}.
 * @param bytes serialized bytes
 * @param offset offset into array
 * @param length length of data
 * @param w An empty Writable (usually made by calling the null-arg
 * constructor).
 * @return The passed Writable after its readFields has been called fed
 * by the passed <code>bytes</code> array or IllegalArgumentException
 * if passed null or an empty <code>bytes</code> array.
 * @throws IOException e
 * @throws IllegalArgumentException
 */
public static Writable getWritable(final byte [] bytes, final int offset,
  final int length, final Writable w)
throws IOException {
  if (bytes == null || length <=0) {
    throw new IllegalArgumentException("Can't build a writable with empty " +
      "bytes array");
  }
  if (w == null) {
    throw new IllegalArgumentException("Writable cannot be null");
  }
  DataInputBuffer in = new DataInputBuffer();
  try {
    in.reset(bytes, offset, length);
    w.readFields(in);
    return w;
  } finally {
    in.close();
  }
}
 
Example 3
Source File: ReadRCFileBuilder.java    From kite with Apache License 2.0 6 votes vote down vote up
private Writable updateColumnValue(RCFileColumn column, BytesRefWritable bytesRef) throws IOException {
  if(bytesRef.getLength() == 0) {
    // This is a null field.
    return NullWritable.get();
  }
  Writable newColumnValue = column.newWritable();
  // Small optimization to bypass DataInput read if the column writable is
  // BytesRefWritable
  if (newColumnValue.getClass() == BytesRefWritable.class) {
    newColumnValue = bytesRef;
  } else {
    byte[] currentRowBytes = Arrays.copyOfRange(bytesRef.getData(),
        bytesRef.getStart(), bytesRef.getStart() + bytesRef.getLength());
    DataInput dataInput = ByteStreams.newDataInput(currentRowBytes);
    newColumnValue.readFields(dataInput);
  }
  return newColumnValue;
}
 
Example 4
Source File: VertexValueWritable.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void readFields(final DataInput input) throws IOException {
    try {
        Kryo kryo = new Kryo();
        kryo.register(HBaseVertex.class, new HBaseVertexSerializer());
        final ByteArrayInputStream bais = new ByteArrayInputStream(WritableUtils.readCompressedByteArray(input));
        this.vertex = kryo.readObject(new Input(bais), HBaseVertex.class);
        Class<? extends Writable> cls = Class.forName(Text.readString(input)).asSubclass(Writable.class);
        Writable writable;
        if (cls.equals(NullWritable.class)) {
            writable = NullWritable.get();
        } else {
            writable = cls.newInstance();
        }
        writable.readFields(input);
        this.value = writable != NullWritable.get() ? (V) writable : null;
    } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
        throw new IOException("Failed writable init", e);
    }
}
 
Example 5
Source File: WALFile.java    From streamx with Apache License 2.0 6 votes vote down vote up
/**
 * Read the next key in the file into <code>key</code>, skipping its value.  True if another
 * entry exists, and false at end of file.
 */
public synchronized boolean next(Writable key) throws IOException {
  if (key.getClass() != WALEntry.class) {
    throw new IOException("wrong key class: " + key.getClass().getName()
                          + " is not " + WALEntry.class);
  }

  outBuf.reset();

  keyLength = next(outBuf);
  if (keyLength < 0) {
    return false;
  }

  valBuffer.reset(outBuf.getData(), outBuf.getLength());

  key.readFields(valBuffer);
  valBuffer.mark(0);
  if (valBuffer.getPosition() != keyLength) {
    throw new IOException(key + " read " + valBuffer.getPosition()
                          + " bytes, should read " + keyLength);
  }


  return true;
}
 
Example 6
Source File: LocalFileUtils.java    From systemds with Apache License 2.0 6 votes vote down vote up
/**
 * Reads an arbitrary writable from local file system, using a fused buffered reader
 * with special support for matrix blocks.
 * 
 * @param fname file name to read
 * @param ret hadoop writable
 * @return hadoop writable
 * @throws IOException if IOException occurs
 */
public static Writable readWritableFromLocal(String fname, Writable ret)
	throws IOException
{
	FileInputStream fis = new FileInputStream(fname);
	DataInput in = !(ret instanceof MatrixBlock) ? 
		new DataInputStream(new BufferedInputStream(fis, BUFFER_SIZE)) :
		new FastBufferedDataInputStream(fis, BUFFER_SIZE);		
	try {
		ret.readFields(in);
	}
	finally {
		IOUtilFunctions.closeSilently((InputStream)in);
		IOUtilFunctions.closeSilently(fis);
	}
		
	return ret;
}
 
Example 7
Source File: CommonStub.java    From big-c with Apache License 2.0 6 votes vote down vote up
protected void readObject(Writable obj, DataInputStream inStream) throws IOException {
  int numBytes = WritableUtils.readVInt(inStream);
  byte[] buffer;
  // For BytesWritable and Text, use the specified length to set the length
  // this causes the "obvious" translations to work. So that if you emit
  // a string "abc" from C++, it shows up as "abc".
  if (obj instanceof BytesWritable) {
    buffer = new byte[numBytes];
    inStream.readFully(buffer);
    ((BytesWritable) obj).set(buffer, 0, numBytes);
  } else if (obj instanceof Text) {
    buffer = new byte[numBytes];
    inStream.readFully(buffer);
    ((Text) obj).set(buffer);
  } else {
    obj.readFields(inStream);
  }
}
 
Example 8
Source File: ReflectionUtils.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static void cloneWritableInto(Writable dst, 
                                     Writable src) throws IOException {
  CopyInCopyOutBuffer buffer = cloneBuffers.get();
  buffer.outBuffer.reset();
  src.write(buffer.outBuffer);
  buffer.moveData();
  dst.readFields(buffer.inBuffer);
}
 
Example 9
Source File: GenericWritableConfigurable.java    From anthelion with Apache License 2.0 5 votes vote down vote up
@Override
public void readFields(DataInput in) throws IOException {
  byte type = in.readByte();
  Class clazz = getTypes()[type];
  try {
    set((Writable) clazz.newInstance());
  } catch (Exception e) {
    e.printStackTrace();
    throw new IOException("Cannot initialize the class: " + clazz);
  }
  Writable w = get();
  if (w instanceof Configurable)
    ((Configurable)w).setConf(conf);
  w.readFields(in);
}
 
Example 10
Source File: Server.java    From RDFS with Apache License 2.0 5 votes vote down vote up
private void processData() throws  IOException, InterruptedException {
  DataInputStream dis =
    new DataInputStream(new ByteArrayInputStream(data.array()));
  int id = dis.readInt();                    // try to read an id

  if (LOG.isDebugEnabled())
    LOG.debug(" got #" + id);

  Writable param = ReflectionUtils.newInstance(paramClass, conf);           // read param
  param.readFields(dis);        
    
  Call call = new Call(id, param, this, responder);
  callQueue.put(call);              // queue the call; maybe blocked here
}
 
Example 11
Source File: ReflectionUtils.java    From RDFS with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static void cloneWritableInto(Writable dst, 
                                     Writable src) throws IOException {
  CopyInCopyOutBuffer buffer = cloneBuffers.get();
  buffer.outBuffer.reset();
  src.write(buffer.outBuffer);
  buffer.moveData();
  dst.readFields(buffer.inBuffer);
}
 
Example 12
Source File: ReflectionUtils.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static void cloneWritableInto(Writable dst, 
                                     Writable src) throws IOException {
  CopyInCopyOutBuffer buffer = cloneBuffers.get();
  buffer.outBuffer.reset();
  src.write(buffer.outBuffer);
  buffer.moveData();
  dst.readFields(buffer.inBuffer);
}
 
Example 13
Source File: Client.java    From RDFS with Apache License 2.0 5 votes vote down vote up
private void receiveResponse() {
  if (shouldCloseConnection.get()) {
    return;
  }
  touch();
  
  try {
    int id = in.readInt();                    // try to read an id

    if (LOG.isDebugEnabled())
      LOG.debug(getName() + " got value #" + id);

    Call call = calls.get(id);

    int state = in.readInt();     // read call status
    if (state == Status.SUCCESS.state) {
      Writable value = ReflectionUtils.newInstance(valueClass, conf);
      value.readFields(in);                 // read value
      call.setValue(value);
      calls.remove(id);
    } else if (state == Status.ERROR.state) {
      call.setException(new RemoteException(WritableUtils.readString(in),
                                            WritableUtils.readString(in)));
      calls.remove(id);
    } else if (state == Status.FATAL.state) {
      // Close the connection
      markClosed(new RemoteException(WritableUtils.readString(in), 
                                     WritableUtils.readString(in)));
    }
  } catch (IOException e) {
    markClosed(e);
  } catch (Throwable te) {
    markClosed((IOException)new IOException().initCause(te));
  }
}
 
Example 14
Source File: Server.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
private void processData() throws  IOException, InterruptedException {
  DataInputStream dis =
    new DataInputStream(new ByteArrayInputStream(data.array()));
  int id = dis.readInt();                    // try to read an id
    
  if (LOG.isDebugEnabled())
    LOG.debug(" got #" + id);

  Writable param = ReflectionUtils.newInstance(paramClass, conf);           // read param
  param.readFields(dis);        
    
  Call call = new Call(id, param, this);
  callQueue.put(call);              // queue the call; maybe blocked here
}
 
Example 15
Source File: FileReducerViolationBean.java    From jumbune with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void readFields(DataInput in) throws IOException {
	fileViolation.clear();
	int entries = in.readInt();
	for(int i=0;i<entries;i++){
		 Writable key = WritableFactories.newInstance(Text.class);
		 key.readFields(in);
		 Writable value = WritableFactories.newInstance(TotalReducerViolationBean.class);
		 value.readFields(in);
		 fileViolation.put(key, value);		
	}
	count.readFields(in);
	
}
 
Example 16
Source File: ReflectionUtils.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static void cloneWritableInto(Writable dst, 
                                     Writable src) throws IOException {
  CopyInCopyOutBuffer buffer = cloneBuffers.get();
  buffer.outBuffer.reset();
  src.write(buffer.outBuffer);
  buffer.moveData();
  dst.readFields(buffer.inBuffer);
}
 
Example 17
Source File: KeyValueHelper.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
private void copyData(Writable src, Writable tgt) throws IOException
{
  PipedInputStream in = new PipedInputStream();
  PipedOutputStream out = new PipedOutputStream(in);
  DataOutputStream dos = new DataOutputStream(out);
  DataInputStream din = new DataInputStream(in);
  src.write(dos);
  tgt.readFields(din);
}
 
Example 18
Source File: Client.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
private void receiveResponse() {
  if (shouldCloseConnection.get()) {
    return;
  }
  touch();
  
  try {
    int id = in.readInt();                    // try to read an id

    if (LOG.isDebugEnabled())
      LOG.debug(getName() + " got value #" + id);

    Call call = calls.remove(id);

    int state = in.readInt();     // read call status
    if (state == Status.SUCCESS.state) {
      Writable value = ReflectionUtils.newInstance(valueClass, conf);
      value.readFields(in);                 // read value
      call.setValue(value);
    } else if (state == Status.ERROR.state) {
      call.setException(new RemoteException(WritableUtils.readString(in),
                                            WritableUtils.readString(in)));
    } else if (state == Status.FATAL.state) {
      // Close the connection
      markClosed(new RemoteException(WritableUtils.readString(in), 
                                     WritableUtils.readString(in)));
    }
  } catch (IOException e) {
    markClosed(e);
  }
}
 
Example 19
Source File: Client.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private void receiveRpcResponse() {
  if (shouldCloseConnection.get()) {
    return;
  }
  touch();
  
  try {
    int totalLen = in.readInt();
    RpcResponseHeaderProto header = 
        RpcResponseHeaderProto.parseDelimitedFrom(in);
    checkResponse(header);

    int headerLen = header.getSerializedSize();
    headerLen += CodedOutputStream.computeRawVarint32Size(headerLen);

    int callId = header.getCallId();
    if (LOG.isDebugEnabled())
      LOG.debug(getName() + " got value #" + callId);

    Call call = calls.get(callId);
    RpcStatusProto status = header.getStatus();
    if (status == RpcStatusProto.SUCCESS) {
      Writable value = ReflectionUtils.newInstance(valueClass, conf);
      value.readFields(in);                 // read value
      calls.remove(callId);
      call.setRpcResponse(value);
      
      // verify that length was correct
      // only for ProtobufEngine where len can be verified easily
      if (call.getRpcResponse() instanceof ProtobufRpcEngine.RpcWrapper) {
        ProtobufRpcEngine.RpcWrapper resWrapper = 
            (ProtobufRpcEngine.RpcWrapper) call.getRpcResponse();
        if (totalLen != headerLen + resWrapper.getLength()) { 
          throw new RpcClientException(
              "RPC response length mismatch on rpc success");
        }
      }
    } else { // Rpc Request failed
      // Verify that length was correct
      if (totalLen != headerLen) {
        throw new RpcClientException(
            "RPC response length mismatch on rpc error");
      }
      
      final String exceptionClassName = header.hasExceptionClassName() ?
            header.getExceptionClassName() : 
              "ServerDidNotSetExceptionClassName";
      final String errorMsg = header.hasErrorMsg() ? 
            header.getErrorMsg() : "ServerDidNotSetErrorMsg" ;
      final RpcErrorCodeProto erCode = 
                (header.hasErrorDetail() ? header.getErrorDetail() : null);
      if (erCode == null) {
         LOG.warn("Detailed error code not set by server on rpc error");
      }
      RemoteException re = 
          ( (erCode == null) ? 
              new RemoteException(exceptionClassName, errorMsg) :
          new RemoteException(exceptionClassName, errorMsg, erCode));
      if (status == RpcStatusProto.ERROR) {
        calls.remove(callId);
        call.setException(re);
      } else if (status == RpcStatusProto.FATAL) {
        // Close the connection
        markClosed(re);
      }
    }
  } catch (IOException e) {
    markClosed(e);
  }
}
 
Example 20
Source File: AvroBytesRecord.java    From hiped2 with Apache License 2.0 4 votes vote down vote up
public static void fromGenericRecord(GenericRecord r, Writable w)
    throws IOException {
  ByteArrayInputStream bais = new ByteArrayInputStream(fromGenericRecord(r));
  DataInputStream dis = new DataInputStream(bais);
  w.readFields(dis);
}