Example usage for org.apache.hadoop.io Writable write

List of usage examples for org.apache.hadoop.io Writable write

Introduction

In this page you can find the example usage for org.apache.hadoop.io Writable write.

Prototype

void write(DataOutput out) throws IOException;

Source Link

Document

Serialize the fields of this object to out.

Usage

From source file:net.sf.katta.zk.ZKClient.java

License:Apache License

private byte[] writableToByteArray(final Writable writable) throws KattaException {
    byte[] data = new byte[0];
    if (writable != null) {
        final DataOutputBuffer out = new DataOutputBuffer();
        try {//from  w w  w  .j  ava2s  . c o  m
            writable.write(out);
        } catch (final IOException e) {
            throw new KattaException("unable to serialize writable", e);
        }
        data = out.getData();
    }
    return data;
}

From source file:nl.basjes.utils.TestWritableInterface.java

License:Apache License

/**
 * Converts an instance of Writable into a byte[].
 * @param writable/*w  w  w .jav  a 2s  .c  o  m*/
 * @return
 * @throws IOException
 */
public static byte[] serialize(Writable writable) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DataOutputStream dataOut = null;
    try {
        dataOut = new DataOutputStream(out);
        writable.write(dataOut);
        return out.toByteArray();
    } finally {
        IOUtils.closeStream(dataOut);
    }
}

From source file:org.apache.accumulo.core.client.mapreduce.lib.impl.InputConfigurator.java

License:Apache License

private static String toBase64(Writable writable) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    try {/*from w w  w  .  j  a v  a  2  s .com*/
        writable.write(dos);
        dos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return Base64.getEncoder().encodeToString(baos.toByteArray());
}

From source file:org.apache.accumulo.server.tabletserver.log.MultiReader.java

License:Apache License

private static void copy(Writable src, Writable dest) throws IOException {
    // not exactly efficient...
    DataOutputBuffer output = new DataOutputBuffer();
    src.write(output);
    DataInputBuffer input = new DataInputBuffer();
    input.reset(output.getData(), output.getLength());
    dest.readFields(input);/*from  www .  j av  a 2  s .c  om*/
}

From source file:org.apache.accumulo.test.util.SerializationUtil.java

License:Apache License

public static void serializeWritable(Writable obj, OutputStream outputStream) {
    Objects.requireNonNull(obj);/*from  ww w. j av a2s  .  co  m*/
    Objects.requireNonNull(outputStream);
    DataOutputStream out = null;
    try {
        out = new DataOutputStream(outputStream);
        obj.write(out);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (IOException e) {
                log.error("cannot close", e);
            }
    }
}

From source file:org.apache.crunch.impl.spark.serde.WritableSerDe.java

License:Apache License

@Override
public byte[] toBytes(Writable obj) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    obj.write(dos);
    dos.close();// w ww  .  j a  v  a  2  s .c o  m
    return baos.toByteArray();
}

From source file:org.apache.crunch.test.Tests.java

License:Apache License

/**
 * Serialize the given Writable into a byte array.
 *
 * @param value The instance to serialize
 * @return The serialized data//from w w w . j  a va  2 s  .co  m
 */
public static byte[] serialize(Writable value) {
    checkNotNull(value);
    try {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        value.write(out);
        return out.toByteArray();
    } catch (IOException e) {
        throw new IllegalStateException("cannot serialize", e);
    }
}

From source file:org.apache.eagle.storage.hbase.query.coprocessor.ProtoBufConverter.java

License:Apache License

public static ByteString writableToByteString(Writable writable) throws IOException {
    ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput();
    writable.write(dataOutput);
    return ByteString.copyFrom(dataOutput.toByteArray());
}

From source file:org.apache.flink.hadoopcompatibility.mapreduce.wrapper.HadoopInputSplit.java

License:Apache License

@Override
public void write(DataOutputView out) throws IOException {
    out.writeInt(this.splitNumber);
    out.writeUTF(this.mapreduceInputSplit.getClass().getName());
    Writable w = (Writable) this.mapreduceInputSplit;
    w.write(out);
}

From source file:org.apache.flume.channel.file.TestUtils.java

License:Apache License

public static DataInput toDataInput(Writable writable) throws IOException {
    ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
    DataOutputStream dataOutput = new DataOutputStream(byteOutput);
    writable.write(dataOutput);
    ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray());
    DataInputStream dataInput = new DataInputStream(byteInput);
    return dataInput;
}