Example usage for org.apache.hadoop.io WritableUtils writeVInt

List of usage examples for org.apache.hadoop.io WritableUtils writeVInt

Introduction

In this page you can find the example usage for org.apache.hadoop.io WritableUtils writeVInt.

Prototype

public static void writeVInt(DataOutput stream, int i) throws IOException 

Source Link

Document

Serializes an integer to a binary stream with zero-compressed encoding.

Usage

From source file:org.apache.hama.pipes.BinaryProtocol.java

License:Apache License

public void endOfInput() throws IOException {
    WritableUtils.writeVInt(stream, MessageType.CLOSE.code);
    flush();/*from   w  w  w  . ja va  2s .co  m*/
    LOG.debug("Sent close command");
    LOG.debug("Sent MessageType.CLOSE");
}

From source file:org.apache.hama.pipes.BinaryProtocol.java

License:Apache License

@Override
public void abort() throws IOException {
    WritableUtils.writeVInt(stream, MessageType.ABORT.code);
    flush();// ww w .  ja va2 s  .c o  m
    LOG.debug("Sent MessageType.ABORT");
}

From source file:org.apache.hama.pipes.BinaryProtocol.java

License:Apache License

/**
 * Write the given object to the stream. If it is a Text or BytesWritable,
 * write it directly. Otherwise, write it to a buffer and then write the
 * length and data to the stream./*from  w  ww .  j ava 2s.c om*/
 * 
 * @param obj the object to write
 * @throws IOException
 */
protected void writeObject(Writable obj) throws IOException {
    // For Text and BytesWritable, encode them directly, so that they end up
    // in C++ as the natural translations.
    if (obj instanceof Text) {
        Text t = (Text) obj;
        int len = t.getLength();
        WritableUtils.writeVInt(stream, len);
        stream.write(t.getBytes(), 0, len);
    } else if (obj instanceof BytesWritable) {
        BytesWritable b = (BytesWritable) obj;
        int len = b.getLength();
        WritableUtils.writeVInt(stream, len);
        stream.write(b.getBytes(), 0, len);
    } else {
        buffer.reset();
        obj.write(buffer);
        int length = buffer.getLength();
        WritableUtils.writeVInt(stream, length);
        stream.write(buffer.getData(), 0, length);
    }
}

From source file:org.apache.hama.pipes.protocol.BinaryProtocol.java

License:Apache License

@Override
public void start() throws IOException {
    LOG.debug("starting downlink");
    WritableUtils.writeVInt(this.outStream, MessageType.START.code);
    WritableUtils.writeVInt(this.outStream, CURRENT_PROTOCOL_VERSION);
    flush();/*from  w w w  .  j av  a 2s  .  c om*/
    LOG.debug("Sent MessageType.START");
    setBSPJobConf(conf);
}

From source file:org.apache.hama.pipes.protocol.BinaryProtocol.java

License:Apache License

@Override
public void setBSPJobConf(Configuration conf) throws IOException {
    WritableUtils.writeVInt(this.outStream, MessageType.SET_BSPJOB_CONF.code);
    List<Entry<String, String>> list = new ArrayList<Entry<String, String>>();
    for (Entry<String, String> entry : conf) {
        list.add(entry);//w w  w .j a  v  a  2 s.c  o m
    }
    WritableUtils.writeVInt(this.outStream, list.size());
    for (Entry<String, String> entry : list) {
        Text.writeString(this.outStream, entry.getKey());
        Text.writeString(this.outStream, entry.getValue());
    }
    flush();
    LOG.debug("Sent MessageType.SET_BSPJOB_CONF including " + list.size() + " entries.");
}

From source file:org.apache.hama.pipes.protocol.BinaryProtocol.java

License:Apache License

@Override
public void setInputTypes(String keyType, String valueType) throws IOException {
    WritableUtils.writeVInt(this.outStream, MessageType.SET_INPUT_TYPES.code);
    Text.writeString(this.outStream, keyType);
    Text.writeString(this.outStream, valueType);
    flush();//from w  w w . ja va  2  s  .c om
    LOG.debug("Sent MessageType.SET_INPUT_TYPES");
}

From source file:org.apache.hama.pipes.protocol.BinaryProtocol.java

License:Apache License

@Override
public void runSetup() throws IOException {
    WritableUtils.writeVInt(this.outStream, MessageType.RUN_SETUP.code);
    flush();/* w w  w .j  a  v  a2  s.  c  om*/
    setHasTask(true);
    LOG.debug("Sent MessageType.RUN_SETUP");
}

From source file:org.apache.hama.pipes.protocol.BinaryProtocol.java

License:Apache License

@Override
public void runBsp() throws IOException {
    WritableUtils.writeVInt(this.outStream, MessageType.RUN_BSP.code);
    flush();/*from  w ww . j  a  v a  2  s  . com*/
    setHasTask(true);
    LOG.debug("Sent MessageType.RUN_BSP");
}

From source file:org.apache.hama.pipes.protocol.BinaryProtocol.java

License:Apache License

@Override
public void runCleanup() throws IOException {
    WritableUtils.writeVInt(this.outStream, MessageType.RUN_CLEANUP.code);
    flush();//from   w w  w .ja  v a2s. com
    setHasTask(true);
    LOG.debug("Sent MessageType.RUN_CLEANUP");
}

From source file:org.apache.hama.pipes.protocol.BinaryProtocol.java

License:Apache License

@Override
public int getPartition(K1 key, V1 value, int numTasks) throws IOException {

    WritableUtils.writeVInt(this.outStream, MessageType.PARTITION_REQUEST.code);
    writeObject((Writable) key);/*  w w  w . j av a2  s.com*/
    writeObject((Writable) value);
    WritableUtils.writeVInt(this.outStream, numTasks);
    flush();

    LOG.debug("Sent MessageType.PARTITION_REQUEST - key: "
            + ((key.toString().length() < 10) ? key.toString() : key.toString().substring(0, 9) + "...")
            + " value: "
            + ((value.toString().length() < 10) ? value.toString() : value.toString().substring(0, 9) + "...")
            + " numTasks: " + numTasks);

    int resultVal = 0;

    synchronized (this.resultLock) {
        try {
            while (resultInt == null) {
                this.resultLock.wait(); // this call blocks
            }

            resultVal = resultInt;
            resultInt = null;

        } catch (InterruptedException e) {
            LOG.error(e);
        }
    }
    return resultVal;
}