[
https://issues.apache.org/jira/browse/AVRO-1392?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13815137#comment-13815137
]
Ryan Blue commented on AVRO-1392:
---------------------------------
I just tried the following, and it works for me if I *don't* add the Stringable
annotation (for both values of the enum).
{code:java}
import org.apache.avro.Schema;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.reflect.ReflectData;
import org.apache.avro.reflect.ReflectDatumReader;
import org.apache.avro.reflect.ReflectDatumWriter;
import org.junit.Assert;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class AvroRoundTrip {
public enum UserType {
ANNOYING,
ANGRY
}
public static class User {
public UserType type;
public String name;
}
@Test
public void testRoundTrip() throws IOException {
Schema reflectedSchema = ReflectData.get().getSchema(User.class);
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().binaryEncoder(byteOutStream, null);
DatumWriter<User> writer = new ReflectDatumWriter(reflectedSchema);
User u = new User();
u.type = UserType.ANNOYING;
u.name = "Blue";
writer.write(u, encoder);
encoder.flush();
Decoder decoder =
DecoderFactory.get().binaryDecoder(byteOutStream.toByteArray(), null);
DatumReader<User> reader = new ReflectDatumReader(reflectedSchema);
User u2 = reader.read(null, decoder);
Assert.assertEquals(u.type, u2.type);
Assert.assertEquals(u.name, u2.name);
}
}
{code}
When I add {{Stringable}} to the enum, I get this:
{code}
org.apache.avro.AvroRuntimeException: Failed to read Stringable
at
org.apache.avro.reflect.ReflectDatumReader.readField(ReflectDatumReader.java:225)
at
org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:183)
at
org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:151)
at
org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:142)
at
com.cloudera.cdk.data.AvroRoundTrip.testRoundTrip(AvroRoundTrip.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.apache.avro.AvroRuntimeException:
java.lang.NoSuchMethodException:
com.cloudera.cdk.data.AvroRoundTrip$UserType.<init>(java.lang.String)
at
org.apache.avro.generic.GenericDatumReader.newInstanceFromString(GenericDatumReader.java:413)
at
org.apache.avro.reflect.ReflectDatumReader.readField(ReflectDatumReader.java:220)
... 30 more
Caused by: java.lang.NoSuchMethodException:
com.cloudera.cdk.data.AvroRoundTrip$UserType.<init>(java.lang.String)
at java.lang.Class.getConstructor0(Class.java:2810)
at java.lang.Class.getDeclaredConstructor(Class.java:2053)
at
org.apache.avro.generic.GenericDatumReader.newInstanceFromString(GenericDatumReader.java:407)
... 31 more
{code}
> Unable to marshal enum to Avro
> ------------------------------
>
> Key: AVRO-1392
> URL: https://issues.apache.org/jira/browse/AVRO-1392
> Project: Avro
> Issue Type: Bug
> Components: java
> Environment: DEV
> Reporter: Balaji Seshadri
> Priority: Blocker
>
> I'm trying to marshall below object to Avro format and unable to marshall it
> using ReflectDatum Writer.Enums are being rejected while marshalling.
> {code:java}
> public class AccountAccess extends DESBusinessEvent<String> {
> @XmlElement(required = true)
> protected String accountId;
> @XmlElement(required = true)
> @XmlSchemaType(name = "dateTime")
> @AvroEncode(using=XmlDateAsLong.class)
> protected XMLGregorianCalendar dateTime;
> // @XmlElement(namespace = DESNamespaces.ENUM_NAMESPACE, required = true)
> @XmlElement(required = true)
> @Stringable
> protected ApplicationEnum application;
> // @XmlElement(namespace = DESNamespaces.ENUM_NAMESPACE, required = true)
> @XmlElement(required = true)
> @Stringable
> protected UserTypeEnum userType;
> protected String userId;
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.1#6144)