title: Writing to Avro Data file
body: <p>The following code simply writes data into avro format and reads and displays the same from the avro file written too. I was just trying out the example in the Hadoop definitive guide book. I was able to execute this first time. Then I got the following error. It did work for the first time. So I am not sure wat mistake i am making.</p>
  
  <p>This is the exception:</p>
  
  <pre><code>Exception in thread "main" java.io.EOFException: No content to map to Object due to end of input
      at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2173)
      at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2106)
      at org.codehaus.jackson.map.ObjectMapper.readTree(ObjectMapper.java:1065)
      at org.codehaus.jackson.map.ObjectMapper.readTree(ObjectMapper.java:1040)
      at org.apache.avro.Schema.parse(Schema.java:895)
      at org.avro.example.SimpleAvro.AvroExample.avrocreate(AvroDataExample.java:23)
      at org.avro.example.SimpleAvro.AvroDataExample.main(AvroDataExample.java:55)
      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:156)
  </code></pre>
  
  <p>This is the code:</p>
  
  <pre><code>package org.avro.example.SimpleAvro;
  
  import java.io.File;
  import java.io.IOException;
  
  import org.apache.avro.Schema;
  import org.apache.avro.file.DataFileReader;
  import org.apache.avro.file.DataFileWriter;
  import org.apache.avro.generic.GenericData;
  import org.apache.avro. generic.GenericDatumReader;
  import org.apache.avro.generic.GenericDatumWriter;
  import org.apache.avro.generic.GenericRecord;
  import org.apache.avro.io.DatumReader;
  import org.apache.avro.io.DatumWriter;
  
  class AvroExample{
  
      AvroExample(){
  
      }
      void avrocreate() throws Exception{
  
          Schema schema=Schema.parse(getClass().getResourceAsStream("Pair.avsc"));
  
          GenericRecord datum=new GenericData.Record(schema);
          datum.put("left", "L");
          datum.put("right", "R");
  
          File file=new File("data.avro");
          DatumWriter&lt;GenericRecord&gt; writer=new GenericDatumWriter&lt;GenericRecord&gt;(schema);
          DataFileWriter&lt;GenericRecord&gt; dataFileWriter=new DataFileWriter&lt;GenericRecord&gt;(writer);
          dataFileWriter.create(schema, file);
          dataFileWriter.append(datum);
          dataFileWriter.close();
  
          System.out.println("Written to avro data file");
          //reading from the avro data file
  
          DatumReader&lt;GenericRecord&gt; reader= new GenericDatumReader&lt;GenericRecord&gt;();
          DataFileReader&lt;GenericRecord&gt; dataFileReader=new DataFileReader&lt;GenericRecord&gt;(file,reader);
          GenericRecord result=dataFileReader.next();
          System.out.println("data" + result.get("left").toString());
  
          result=dataFileReader.next();
          System.out.println("data :" + result.get("left").toString());
  
  
      }
  
  }
  public class AvroDataExample {
      public static void main(String args[])throws Exception{
  
          AvroExample a=new AvroExample();
          a.avrocreate();
      }
  
  
  
  }
  </code></pre>
  
  <p>The following is the Pair.avsc file [ given in the book's example code]</p>
  
  <pre><code>{
    "type": "record",
    "name": "Pair",
    "doc": "A pair of strings.",
    "fields": [
      {"name": "left", "type": "string"},
      {"name": "right", "type": "string"}
    ]
  }
  </code></pre>