avro-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Brock Noland (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (AVRO-1240) SpecificDataumReader.next() can return a GenericData.Record resulting in a ClassCastException
Date Sat, 02 Feb 2013 18:18:12 GMT

    [ https://issues.apache.org/jira/browse/AVRO-1240?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13569610#comment-13569610
] 

Brock Noland commented on AVRO-1240:
------------------------------------

Doug,

Yes that the same approach I was considering and fixes the issue locally (below). I do have
one additional item to discuss. The Specific* readers are generics and as such, the user expects
that the next() method will never return anything but the Specific class in question. Furthermore,
if using the Specific* it's likely a ClassCastException is going to be returned by returning
anything but the Specific class in question.

As such, inline with the fail fast and loudly principle, I wonder if instead of punting up
to the Generic*, a runtime exception with a good error message should be thrown when the class
for a schema cannot be found. I am not overly familiar with the Avro source, so it's possible
this would break something badly. If the change is safe, then at least in this case, it would
have made for a much better user experience.

{noformat}
$ java -cp avro-1.7.4-SNAPSHOT.jar:$(hadoop classpath) org.apache.hadoop.util.RunJar target/avro-example-1.0-jar-with-dependencies.jar
Sample target/test-file
{"left": "l", "right": "r"}
{noformat}
                
> SpecificDataumReader.next() can return a GenericData.Record resulting in a ClassCastException
> ---------------------------------------------------------------------------------------------
>
>                 Key: AVRO-1240
>                 URL: https://issues.apache.org/jira/browse/AVRO-1240
>             Project: Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.7.3
>            Reporter: Brock Noland
>            Assignee: Brock Noland
>             Fix For: 1.7.4
>
>         Attachments: AVRO-1240.patch
>
>
> The following reasonable code:
> {noformat}
>     DatumReader<Test> reader = new SpecificDatumReader<Test>(Test.class);

>     DataFileReader<Test> dataFileReader = new DataFileReader<Test>(file,
reader); 
>     Test datum = null;
>     while(dataFileReader.hasNext()) { 
>       datum = dataFileReader.next(datum); 
>       System.out.println(datum); 
>     }
> {noformat}
> Can result in a runtime exception:
> {noformat}
> $ java -cp avro-1.7.3.jar:$(hadoop classpath) org.apache.hadoop.util.RunJar target/avro-example-1.0-jar-with-dependencies.jar
Sample target/test-file 
> Exception in thread "main" java.lang.ClassCastException: org.apache.avro.generic.GenericData$Record
cannot be cast to Test
> 	at Sample.main(Sample.java:27)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> 	at java.lang.reflect.Method.invoke(Method.java:597)
> 	at org.apache.hadoop.util.RunJar.main(RunJar.java:208)
> {noformat}
> when the jar containing the specific class (Test in my example) is loaded from a different
classloader than the avro jar itself. This occurs when run via the hadoop jar command but
could occur in web containers or other locations as well.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message