Package org.apache.avro.reflect

Examples of org.apache.avro.reflect.ReflectDatumWriter


    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


  }

  @Test
  public void testRecord() throws IOException {
    Schema schm = ReflectData.get().getSchema(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);
    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);
    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);
    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

   * @return true if SpecificData can be properly read using a
   *         ReflectDatumReader
   */
  static boolean canDecodeSpecificSchemaWithReflectDatumReader() {
    ReflectDatumReader<Record> datumReader = new ReflectDatumReader(Record.SCHEMA$);
    ReflectDatumWriter<Record> datumWriter = new ReflectDatumWriter(Record.SCHEMA$);

    Record record = new Record();
    record.subrecords = Lists.<CharSequence> newArrayList("a", "b");

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(byteArrayOutputStream, null);

    try {
      datumWriter.write(record, encoder);
      encoder.flush();
      BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(
          byteArrayOutputStream.toByteArray(), null);
      datumReader.read(record, decoder);
    } catch (IOException ioe) {
View Full Code Here

  }

  @InterfaceAudience.Private
  @Override
  public DatumWriter getWriter(Class<Object> clazz) {
    return new ReflectDatumWriter();
  }
View Full Code Here

  }

  /**
   */
  void emitData(File outData, Schema schema, int numRecords, Instantiator inster) throws IOException, InstantiationException {
    DatumWriter dout = new ReflectDatumWriter(schema);

    DataFileWriter out = new DataFileWriter(dout);
    out = out.create(schema, outData);
    try {
      for (int i = 0; i < numRecords; i++) {
View Full Code Here

  /**
   */
  public void generateData(boolean encodeJson, File outfile, int numRecords) throws IOException {
    Schema schema = ReflectData.get().getSchema(TestRecord.class);
    DatumWriter dout = new ReflectDatumWriter(schema);

    if (encodeJson) {
      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
      try {
        Encoder encoder = EncoderFactory.get().jsonEncoder(schema, (OutputStream) out);
        for (int i = 0; i < numRecords; i++) {
          TestRecord tr = new TestRecord();
          dout.write(tr, encoder);
        }
        encoder.flush();
      } finally {
        out.close();
      }
View Full Code Here

  }

  @InterfaceAudience.Private
  @Override
  public DatumWriter getWriter(Class<Object> clazz) {
    return new ReflectDatumWriter();
  }
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.