Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Avro version: 1.7.4 This doesn't work.
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);
}
}
}
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.