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

John Jenkins commented on AVRO-1099:
------------------------------------

Sure. I am using the "avro-1.6.3.jar" and the "jackson-all-1.9.7.jar" files. 
Here is some example code all in Java:

{code}

package test;

import org.apache.avro.Schema;
import org.apache.avro.Schema.Parser;
import org.apache.avro.generic.GenericContainer;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;

/**
 * An example use of Avro.
 *
 * @author John Jenkins
 */
public class AvroIntTest {
        /**
         * This is a sample Avro schema for a double.
         */
        private static final String DEFINITION = 
                "{" +
                        "\"namespace\":\"test\"," +
                        "\"type\":\"record\"," +
                        "\"name\":\"DoubleTest\"," +
                        "\"fields\":[" +
                                "{" +
                                        "\"type\":\"double\"," +
                                        "\"name\":\"DoubleValue\"" +
                                "}" +
                        "]" +
                "}";
        
        /**
         * This is a sample record that matches the above schema.
         */
        private static final String EXAMPLE_RECORD =
                "{" +
                        "\"DoubleValue\":0" + // This will fail.
                        // "\"DoubleValue\":0.0" + // This will succeed.
                "}";

        /**
         * This is the driver of this example.
         * 
         * @param args None accepted.
         * 
         * @throws Exception Never actually thrown, this is stashed here because
         *                                       this is an example.
         */
        public static void main(String[] args) throws Exception {
                // Compile the schema.
                Schema schema = (new Parser()).parse(DEFINITION);
                
                // Create the reader to read the data that has been decoded.
                GenericDatumReader<GenericContainer> reader =
                        new GenericDatumReader<GenericContainer>(schema);
                
                // Create the decoder to decode our sample record.
                Decoder decoder =
                        (new DecoderFactory()).jsonDecoder(schema, 
EXAMPLE_RECORD);
                
                // Retrieve the container.
                GenericContainer avroContainer = reader.read(null, decoder);
        }
}

{code}

We are trying out Avro to see if it will work for our system, so I am very new 
to it. If there is a more appropriate way to handle this, please let me know.
                
> Int / Float cannot be decoded
> -----------------------------
>
>                 Key: AVRO-1099
>                 URL: https://issues.apache.org/jira/browse/AVRO-1099
>             Project: Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.6.3
>         Environment: Mac OS X 10.7.3. Eclipse IDE for Java EE Developers 
> 1.4.2.20120213
>            Reporter: John Jenkins
>              Labels: patch
>
> I am currently using a JSON library that is encoding 0 for a double to "0" 
> instead of "0.0", without the quotes. First, I feel that "0" is a perfectly 
> valid value for 0 as a double. But, I tried to code around it by changing the 
> "type" of the field to "["double", "int"]", without the quotes and it is 
> giving me this error:
> Exception in thread "main" org.apache.avro.AvroTypeException: Expected 
> start-union. Got VALUE_NUMBER_INT
>       at org.apache.avro.io.JsonDecoder.error(JsonDecoder.java:697)
>       at org.apache.avro.io.JsonDecoder.readIndex(JsonDecoder.java:441)
>       at 
> org.apache.avro.io.ResolvingDecoder.doAction(ResolvingDecoder.java:229)
>       at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
>       at 
> org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:206)
>       at 
> org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:148)
>       at 
> org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:173)
>       at 
> org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:144)
>       at 
> org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:135)
> If I change it to just "int", then it works for a few records until it 
> reaches a double.
> Thank you,
> John

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to