Skip to content

@jcoveney /gist:5459644
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
This seems to show a bug with the DataFileReader
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.