Skip to content

Instantly share code, notes, and snippets.

@jcoveney
Last active December 15, 2015 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcoveney/5317904 to your computer and use it in GitHub Desktop.
Save jcoveney/5317904 to your computer and use it in GitHub Desktop.
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