public
Created

This seems to show a bug with the DataFileReader

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
 
import org.apache.avro.Schema;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.file.FileReader;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.io.DecoderFactory;
 
public class Hrm {
public static void main(String[] args) {
try {
String schema = "{\"fields\": [{\"type\": [\"null\", \"bytes\"], \"default\": null, \"name\": \"field1\"}, {\"type\": [\"null\", \"string\"], \"default\": null, \"name\": \"field2\"}], \"type\": \"record\", \"name\": \"test\"}";
String record = "{\"field1\": {\"bytes\": \"i am a banana\"}, \"field2\": {\"string\": \"i am a banana\"}}";
 
Schema avroSchema = new Schema.Parser().parse(schema);
 
GenericDatumReader<Object> reader = new GenericDatumReader<Object>(avroSchema);
DataFileWriter<Object> writer = new DataFileWriter<Object>(new GenericDatumWriter<Object>(avroSchema));
File output = new File("test.avro");
output.delete();
OutputStream os = new FileOutputStream(output);
writer.create(avroSchema, os);
 
Object datum = reader.read(null, DecoderFactory.get().jsonDecoder(avroSchema, record));
writer.append(datum);
writer.close();
os.close();
FileReader<Object> dfr = DataFileReader.openReader(new File("test.avro"), new GenericDatumReader<Object>());
System.out.println(dfr.next()); // {"field1": {"bytes": "i am a banana"}, "field2": "i am a banana"} !
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.