FAQ
hello:
I write a class containing a treeset to implements writable , how should I
to implements the two methods?
Any help would be appreciated!
--
View this message in context: http://www.nabble.com/What-should-I-do-to-implements-writable--tp23946397p23946397.html
Sent from the Hadoop core-user mailing list archive at Nabble.com.

Search Discussions

  • Jason hadoop at Jun 10, 2009 at 3:52 am
    A writeable basically needs to implement two methods:
    /**
    * Serialize the fields of this object to <code>out</code>.
    *
    * @param out <code>DataOuput</code> to serialize this object into.
    * @throws IOException
    */
    void write(DataOutput out) throws IOException;

    /**
    * Deserialize the fields of this object from <code>in</code>.
    *
    * <p>For efficiency, implementations should attempt to re-use storage in
    the
    * existing object where possible.</p>
    *
    * @param in <code>DataInput</code> to deseriablize this object from.
    * @throws IOException
    */
    void readFields(DataInput in) throws IOException;

    These use the serialization primitives to pack and unpack the object.

    These are from the hadoop Text class in 0.19.1
    /** deserialize
    */
    public void readFields(DataInput in) throws IOException {
    int newLength = WritableUtils.readVInt(in);
    setCapacity(newLength, false);
    in.readFully(bytes, 0, newLength);
    length = newLength;
    }

    /** serialize
    * write this object to out
    * length uses zero-compressed encoding
    * @see Writable#write(DataOutput)
    */
    public void write(DataOutput out) throws IOException {
    WritableUtils.writeVInt(out, length);
    out.write(bytes, 0, length);
    }

    If you look at the various implementors of Writable, you will find plenty of
    examples in the source tree.
    For very complex objects, the simplest thing to do is to serialize them to
    an ObjectOutputStream that is backed by a ByteArrayOutputStream then use the
    write/read byte array method on the DataOutput/DataInput

    In my code I check to see if in/out implement the the ObjectXXXXXStream and
    use it directly, or use an intermediate byte array.




    On Tue, Jun 9, 2009 at 9:23 AM, HRoger wrote:


    hello:
    I write a class containing a treeset to implements writable , how should I
    to implements the two methods?
    Any help would be appreciated!
    --
    View this message in context:
    http://www.nabble.com/What-should-I-do-to-implements-writable--tp23946397p23946397.html
    Sent from the Hadoop core-user mailing list archive at Nabble.com.

    --
    Alpha Chapters of my book on Hadoop are available
    http://www.apress.com/book/view/9781430219422
    www.prohadoopbook.com a community for Hadoop Professionals

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcommon-user @
categorieshadoop
postedJun 9, '09 at 4:23p
activeJun 10, '09 at 3:52a
posts2
users2
websitehadoop.apache.org...
irc#hadoop

2 users in discussion

HRoger: 1 post Jason hadoop: 1 post

People

Translate

site design / logo © 2022 Grokbase