Try

SpecificDatumReader<LogMessage> din = new 
SpecificDatumReader<LogMessage>(LogMessage.class);

instead of the GenericDatumReader below.


On Apr 26, 2010, at 7:59 AM, Jaydeep Ayachit wrote:

Hello,

I am trying to read from avro data file and want to use specific object (exa 
sample.LogMessage)
The following code gives exception mentioned below. Reader.next() when 
collected in Object/Record, no exception is thrown which is expected.

Any help is appreciated.

public void testMethod5() throws Exception {
            DatumWriter<LogMessage> writer = new 
GenericDatumWriter<LogMessage>(LogMessage.SCHEMA$);
            DataFileWriter<LogMessage> out = new 
DataFileWriter<LogMessage>(writer);

            out.create(LogMessage.SCHEMA$, <File>);
            for (int i = 0; i < 100; i++) {
                  LogMessage msg = new LogMessage();
                  msg.applicationName = new Utf8("app" + i);
                  msg.userid = new Utf8("user" + i);
                  msg.message = new Utf8("this is test log message " + i);

                  out.append(msg);
            }
            out.close();

            DataFileReader<LogMessage> reader;
            GenericDatumReader<LogMessage> din = new 
GenericDatumReader<LogMessage>(LogMessage.SCHEMA$);
            reader = new DataFileReader<LogMessage>(new File(<>), din);

            while (reader.hasNext()) {
                  LogMessage msg = reader.next();
                  System.out.println(msg.toString());
            }
            reader.close();
      }

Exception

java.lang.ClassCastException: org.apache.avro.generic.GenericData$Record cannot 
be cast to sample.LogMessage
      at com.ibm.clas.ReaderWriter.testMethod5(ReaderWriter.java:313)
      at com.ibm.clas.ReaderWriter.main(ReaderWriter.java:322)


Thanks
Jaydeep


DISCLAIMER ========== This e-mail may contain privileged and confidential 
information which is the property of Persistent Systems Ltd. It is intended 
only for the use of the individual or entity to which it is addressed. If you 
are not the intended recipient, you are not authorized to read, retain, copy, 
print, distribute or use this message. If you have received this communication 
in error, please notify the sender and delete all copies of this message. 
Persistent Systems Ltd. does not accept any liability for virus infected mails.

Reply via email to