public
Last active

Avro version: 1.7.4 This doesn't work.

  • Download Gist
avro_issue_2
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 41 42 43 44 45
import java.io.FileOutputStream;
import org.apache.avro.Schema;
import org.apache.avro.file.CodecFactory;
import org.apache.avro.file.DataFileWriter;
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 a = "{\"type\": \"record\", \"namespace\": \"example.avro\", \"name\": \"User\", \"fields\": [{\"type\": \"string\", \"name\": \"name\"}, {\"type\": [\"null\", \"int\"], \"name\": \"favorite_number\"}, {\"type\": [\"null\", \"string\"], \"name\": \"favorite_color\"}]}";
String b = "{\"name\": \"Alyssa\", \"favorite_number\": null, \"favorite_color\": null}"; //works fine
String c = "{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": \"blue\"}"; //does not work
 
Schema avroSchema = new Schema.Parser().parse(a);
 
DataFileWriter<Object> writer = new DataFileWriter<Object>(new GenericDatumWriter<Object>(avroSchema));
writer.setCodec(CodecFactory.deflateCodec(6));
writer.create(avroSchema, new FileOutputStream("ab_java.avro"));
 
GenericDatumReader<Object> reader = new GenericDatumReader<Object>(avroSchema);
Object datum = reader.read(null, DecoderFactory.get().jsonDecoder(avroSchema, b));
writer.append(datum);
writer.close();
System.out.println("First successful!");
writer = new DataFileWriter<Object>(new GenericDatumWriter<Object>(avroSchema));
writer.setCodec(CodecFactory.deflateCodec(6));
writer.create(avroSchema, new FileOutputStream("ab_java.avro"));
 
reader = new GenericDatumReader<Object>(avroSchema);
datum = reader.read(null, DecoderFactory.get().jsonDecoder(avroSchema, c));
writer.append(datum);
writer.close();
System.out.println("Second successful!");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.