Ah - this was the problem! Now that I have the constructor, I am able
to serialize either way - using Java's serialization or my own. For
this app, I'm happy either way, but I'll think about sticking with my
own serialization in the future.

Thanks for the help!

On 10/10/07, Christopher Douglas wrote:
It looks like you're missing a *public*, default constructor; the
framework can't create an instance to call readFields() on.

That said: you might want to reconsider using Java serialization within
Writables. I know it seems restrictive, but you're opening yourself up
to a rancid cornucopia of ClassLoader, static, etc. issues. -C
-----Original Message-----
From: Steve Schlosser
Sent: Wednesday, October 10, 2007 7:57 AM
To: hadoop-user@lucene.apache.org
Subject: Using my own classes as keys and values

Hello all

Is there a best practice for using my own classes as keys and values?

My first attempt at doing this was successful - I built a
BigIntegerWritable class using IntWritable as a template. It was easy
because BigInteger has methods converting to and from byte arrays,
which I could then write into the DataOutput or read from the

It seems like I should be able to use object serialization to write
to/read from the DataOutput/Input objects and make my own classes
implement the Writable interface. It seems like I should be able to
do something like this:

import java.io.*;

import org.apache.hadoop.io.*;

public class Sample implements Writable {

Address address;
SampleValue value; // sampled value at this point

public Sample(Address a, SampleValue v) {
address = a;
value = v;

public SampleValue getValue() { return value;}
public Address getAddress() { return address; }

public String toString () {
return (address.toString() + " " + value.toString());


public void readFields(DataInput in) throws IOException {
ObjectInputStream oin = new

try {
address = (Address)oin.readObject();
value = (SampleValue)oin.readObject();
} catch (ClassNotFoundException e) {
throw new IOException(e.toString());


public void write(DataOutput out) throws IOException {
ObjectOutputStream oout = new


This code compiles, but throws exceptions at runtime, complaining that
WritableComparator can not access a member of class Sample with
modifiers "". Can someone tell me what this exception is talking

Do I need to implement a WritableComparator for each class that I want
to implement Writable?

Thanks again for the help.


Search Discussions

Discussion Posts


Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 9 of 9 | next ›
Discussion Overview
groupcommon-user @
postedOct 10, '07 at 3:03p
activeOct 11, '07 at 1:29a



site design / logo © 2022 Grokbase