Package org.apache.avro.reflect

Examples of org.apache.avro.reflect.ReflectDatumWriter


  @SuppressWarnings("unchecked")
  public static byte[] serialize(Object datum, Schema schema) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
    ReflectDatumWriter writer = new ReflectDatumWriter(schema);
    try {
      writer.write(datum, encoder);
      encoder.flush();
    } catch (IOException ex) {
      Throwables.propagate(ex);
    }
    return out.toByteArray();
View Full Code Here


  @SuppressWarnings("unchecked")
  public static byte[] serialize(Object datum, Schema schema) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
    ReflectDatumWriter writer = new ReflectDatumWriter(schema);
    try {
      writer.write(datum, encoder);
      encoder.flush();
    } catch (IOException ex) {
      Throwables.propagate(ex);
    }
    return out.toByteArray();
View Full Code Here

  @Test
  public void testRecord() throws IOException {
    Schema schm = ReflectData.get().getSchema(SampleRecord.class);
    String prefix = getPrefix(SampleRecord.class);
    ReflectDatumWriter writer = new ReflectDatumWriter(schm);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SampleRecord record = new SampleRecord();
    record.x = 5;
    record.y = 10;
    writer.write(record, new BinaryEncoder(out));
    ReflectDatumReader reader = new ReflectDatumReader(schm, prefix);
    Object decoded =
      reader.read(null, new BinaryDecoder
                  (new ByteArrayInputStream(out.toByteArray())));
    assertEquals(record, decoded);
View Full Code Here

  @Test
  public void testRecordWithNull() throws IOException {
    ReflectData reflectData = ReflectData.AllowNull.get();
    Schema schm = reflectData.getSchema(AnotherSampleRecord.class);
    String prefix = getPrefix(AnotherSampleRecord.class);
    ReflectDatumWriter writer = new ReflectDatumWriter(schm);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // keep record.a null and see if that works
    AnotherSampleRecord a = new AnotherSampleRecord();
    writer.write(a, new BinaryEncoder(out));
    AnotherSampleRecord b = new AnotherSampleRecord(10);
    writer.write(b, new BinaryEncoder(out));
    ReflectDatumReader reader = new ReflectDatumReader(schm, prefix);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    Object decoded = reader.read(null, new BinaryDecoder(in));
    assertEquals(a, decoded);
    decoded = reader.read(null, new BinaryDecoder(in));
View Full Code Here

    List<Schema> schemas = Arrays.asList(new Schema[] {
        reflectData.getSchema(FooRecord.class),
        reflectData.getSchema(BarRecord.class) });
    Schema union = Schema.createUnion(schemas);
    DataFileWriter<Object> writer = new DataFileWriter<Object>(union, fos,
        new ReflectDatumWriter(union));

    // test writing to a file
    CheckList check = new CheckList();
    write(writer, new BarRecord("One beer please"), check);
    write(writer, new FooRecord(10), check);
View Full Code Here

    ReflectData reflectData = ReflectData.get();
    List<Schema> schemas = new ArrayList<Schema>();
    schemas.add(reflectData.getSchema(FooRecord.class));
    Schema union = Schema.createUnion(schemas);
    DataFileWriter<Object> writer = new DataFileWriter<Object>(union, fos,
        new ReflectDatumWriter(union));

    CheckList check = new CheckList();
    // write known type
    write(writer, new FooRecord(10), check);
    write(writer, new FooRecord(15), check);
View Full Code Here

    FileOutputStream fos = new FileOutputStream(FILE);

    ReflectData reflectData = ReflectData.AllowNull.get();
    Schema schema = reflectData.getSchema(BarRecord.class);
    DataFileWriter<Object> writer = new DataFileWriter<Object>(schema, fos,
        new ReflectDatumWriter(schema, reflectData));

    // test writing to a file
    CheckList check = new CheckList();
    write(writer, new BarRecord("One beer please"), check);
    // null record here, fails when using the default reflectData instance
View Full Code Here

  public void testNestedClass() throws IOException {
    FileOutputStream fos = new FileOutputStream(FILE);

    Schema schema = ReflectData.get().getSchema(BazRecord.class);
    DataFileWriter<Object> writer = new DataFileWriter<Object>(schema, fos,
        new ReflectDatumWriter(schema));

    // test writing to a file
    CheckList check = new CheckList();
    write(writer, new BazRecord(10), check);
    write(writer, new BazRecord(20), check);
View Full Code Here

    List<Schema> schemas = Arrays.asList(new Schema[] {
        reflectData.getSchema(FooRecord.class),
        reflectData.getSchema(BarRecord.class) });
    Schema union = Schema.createUnion(schemas);
    DataFileWriter<Object> writer = new DataFileWriter<Object>(union, fos,
        new ReflectDatumWriter(union));

    // test writing to a file
    CheckList check = new CheckList();
    write(writer, new BarRecord("One beer please"), check);
    write(writer, new FooRecord(10), check);
View Full Code Here

    ReflectData reflectData = ReflectData.get();
    List<Schema> schemas = new ArrayList<Schema>();
    schemas.add(reflectData.getSchema(FooRecord.class));
    Schema union = Schema.createUnion(schemas);
    DataFileWriter<Object> writer = new DataFileWriter<Object>(union, fos,
        new ReflectDatumWriter(union));

    CheckList check = new CheckList();
    // write known type
    write(writer, new FooRecord(10), check);
    write(writer, new FooRecord(15), check);
View Full Code Here

TOP

Related Classes of org.apache.avro.reflect.ReflectDatumWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.