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

Balaji Seshadri commented on AVRO-1392:
---------------------------------------

Uploading Avro Encoder code where loss is happening.

package com.dish.des.msgs.encoders.avro;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.avro.Schema;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.DecoderFactory;
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 com.dish.des.msgs.encoders.Encoder;

public class AvroEncoder<Pojo> extends Encoder<Pojo,String> {
        
        public byte [] encodeAsBytes(Pojo pojo) {
                
                ByteArrayOutputStream os = new ByteArrayOutputStream();
        BinaryEncoder e = EncoderFactory.get().binaryEncoder(os, null);
        Schema schema = ReflectData.get().getSchema(pojo.getClass());
        DatumWriter<Pojo> writer = new ReflectDatumWriter<Pojo>(schema);
        
        try {
                        writer.write(pojo, e);
                        e.flush();
                } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        throw new IllegalStateException("bad encode: ", e1);
                }
        finally{
                try {
                                os.close();
                        } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                throw new IllegalStateException("bad encode: ", 
e1);
                        }
        }
        
        return os.toByteArray();
        }


        
        @Override
        public String encode(Pojo pojo) {
                
                ByteArrayOutputStream os = new ByteArrayOutputStream();
        BinaryEncoder e = EncoderFactory.get().binaryEncoder(os, null);
        Schema schema = ReflectData.get().getSchema(pojo.getClass());
        DatumWriter<Pojo> writer = new ReflectDatumWriter<Pojo>(schema);
        String data="";
        
        try {
                        writer.write(pojo, e);
                        e.flush();
                        data = os.toString();
                } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        throw new IllegalStateException("bad encode: ", e1);
                }
        finally{
                try {
                                os.close();
                        } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                throw new IllegalStateException("bad encode: ", 
e1);
                        }
        }
        
        return data;
        }

        @Override
        public Pojo decode(String encoded, Class<Pojo> clazz) {
                ByteArrayInputStream bais = new 
ByteArrayInputStream(encoded.getBytes());
                BinaryDecoder in = new DecoderFactory().binaryDecoder(bais, 
null);
                Schema schema = ReflectData.get().getSchema(clazz);
                try {
                DatumReader<Pojo> reader = new ReflectDatumReader<Pojo>(schema);
                Pojo pojo=null;
                return reader.read(pojo, in);
                } catch (Exception e) {
                        processException(e);
                        throw new IllegalStateException("bad decode: ", e);
                }
        }

}


package com.dish.des.msgs.encoders;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlTransient;

@XmlTransient
public abstract class Encoder<Pojo,Representation> {

        public abstract Representation encode(Pojo pojo);
        public abstract Pojo decode(Representation encoded, Class<Pojo> clazz);

        protected void processException(Exception e) {}
        
        @SuppressWarnings("serial")
        protected static List<String> getPackageImportStringList() {
                return new ArrayList<String>() {
                        {
                                add("com.dish.des.msgs.common.v1");
                                add("com.dish.des.msgs.common.v2");
                                add("com.dish.des.msgs.accountAccess");
                                add("com.dish.des.msgs.accountCreated");
                                add("com.dish.des.msgs.accountInfoUpdated");
                                add("com.dish.des.msgs.customerCues");
                                add("com.dish.des.msgs.outboundCommunications");
                                add("com.dish.des.msgs.subscriberEvents");
                        }
                };
        }

        protected static String buildPackageImportString() {
                StringBuilder builder = new StringBuilder();
                for (String packageImport : getPackageImportStringList()) {
                        builder.append(packageImport);
                        builder.append(":");
                }
                return builder.toString();
        }
}





> 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)

Reply via email to