Package org.apache.avro.io

Examples of org.apache.avro.io.BinaryEncoder


    JsonGenerator jsonGenerator = (new JsonFactory()).createJsonGenerator(new OutputStreamWriter(out));
    if (AvroFormat.JSON == _outputFormat) jsonGenerator.useDefaultPrettyPrinter();

    List<GenericRecord> result = convert(in);
    Encoder outputEncoder = (AvroFormat.BINARY == _outputFormat) ?
        new BinaryEncoder(out) :
        new JsonEncoder(_outputSchema, jsonGenerator);

    GenericDatumWriter<GenericRecord> genericWriter = new GenericDatumWriter<GenericRecord>(_outputSchema);
    for (GenericRecord r: result)
    {
View Full Code Here


  {
    ConsumerCallbackResult result = ConsumerCallbackResult.SUCCESS;
    try
    {

      BinaryEncoder binEnc = _binEncoders.get(r.getSchema());
      if (null == binEnc)
      {
        binEnc = new BinaryEncoder(_out);
        _binEncoders.put(r.getSchema(), binEnc);
      }

      GenericDatumWriter<GenericRecord> datumWriter = binWriters.get(r.getSchema());
      if (null == datumWriter)
      {
        datumWriter = new GenericDatumWriter<GenericRecord>(r.getSchema());
        binWriters.put(r.getSchema(), datumWriter);
      }

      datumWriter.write(r, binEnc);
      binEnc.flush();
      _out.write('\n');
    }
    catch (RuntimeException re)
    {
      LOG.error("event dump error: " + re.getMessage(), re);
View Full Code Here

    // Serialize the row
    byte[] serializedValue;
    try
    {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      Encoder encoder = new BinaryEncoder(bos);
      GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(record.getSchema());
      writer.write(record, encoder);
      serializedValue = bos.toByteArray();
    }
    catch(IOException ex)
View Full Code Here

    {
      GenericRecord r = new GenericData.Record(SOURCE1_SCHEMA);
      String s = RngUtils.randomString(rng.nextInt(100));
      r.put("s", s);
      ByteArrayOutputStream baos = new ByteArrayOutputStream(s.length() + 100);
      BinaryEncoder out = new BinaryEncoder(baos);
      try
      {
        writer.write(r, out);
        out.flush();

        result[i] = new DbusEventInfo(DbusOpcode.UPSERT, 1, (short)1, (short)1,
                                      System.nanoTime(),
                                      (short)1, SOURCE1_SCHEMAID,
                                      baos.toByteArray(), false, true);
View Full Code Here

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write( AvroSerializationProvider.getMajorVersion() );
    out.write( AvroSerializationProvider.getMinorVersion() );
    Schema msgSchema = protocol.getType( "Message" );
    GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>( msgSchema );
    BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder( out, null );
    GenericRecord message = new GenericData.Record( msgSchema );
    message.put( "classReferences", classReferences );
    message.put( "operations", operations );
    operations = null;
    try {
      writer.write( message, encoder );
      encoder.flush();
    }
    catch (IOException e) {
      throw log.unableToSerializeInAvro( e );
    }
    return out.toByteArray();
View Full Code Here

   }

   @Override
   protected ByteBuffer objectToBuffer(Object o, int estimatedSize) throws IOException {
      ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream(estimatedSize);
      Encoder encoder = new BinaryEncoder(baos);
      objectToBuffer(o, encoder);
      return new ByteBuffer(baos.getRawBuffer(), 0, baos.size());
   }
View Full Code Here

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write( AvroSerializationProvider.getMajorVersion() );
    out.write( AvroSerializationProvider.getMinorVersion() );
    Schema msgSchema = protocol.getType( "Message" );
    GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>( msgSchema );
    BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder( out, null );
    GenericRecord message = new GenericData.Record( msgSchema );
    message.put( "classReferences", classReferences );
    message.put( "operations", operations );
    operations = null;
    try {
      writer.write( message, encoder );
      encoder.flush();
    }
    catch ( IOException e ) {
      throw log.unableToSerializeInAvro(e);
    }
    return out.toByteArray();
View Full Code Here

    Schema record = Schema.createRecord("test", null, null, false);
    record.setFields(fields);
   
    ByteArrayOutputStream b1 = new ByteArrayOutputStream(5);
    ByteArrayOutputStream b2 = new ByteArrayOutputStream(5);
    BinaryEncoder b1Enc = EncoderFactory.get().binaryEncoder(b1, null);
    BinaryEncoder b2Enc = EncoderFactory.get().binaryEncoder(b2, null);
    // Prepare two different datums
    Record testDatum1 = new Record(record);
    testDatum1.put(0, 1);
    Record testDatum2 = new Record(record);
    testDatum2.put(0, 2);
    GenericDatumWriter<Record> gWriter = new GenericDatumWriter<Record>(record);
    Integer start1 = 0, start2 = 0;
    try {
      // Write two datums in each stream
      // and get the offset length after the first write in each.
      gWriter.write(testDatum1, b1Enc);
      b1Enc.flush();
      start1 = b1.size();
      gWriter.write(testDatum1, b1Enc);
      b1Enc.flush();
      b1.close();
      gWriter.write(testDatum2, b2Enc);
      b2Enc.flush();
      start2 = b2.size();
      gWriter.write(testDatum2, b2Enc);
      b2Enc.flush();
      b2.close();
      // Compare to check if offset-based compare works right.
      assertEquals(-1, BinaryData.compare(b1.toByteArray(), start1, b2.toByteArray(), start2, record));
    } catch (IOException e) {
      fail("IOException while writing records to output stream.");
View Full Code Here

    Schema record = Schema.createRecord("test", null, null, false);
    record.setFields(fields);
   
    ByteArrayOutputStream b1 = new ByteArrayOutputStream(5);
    ByteArrayOutputStream b2 = new ByteArrayOutputStream(5);
    BinaryEncoder b1Enc = EncoderFactory.get().binaryEncoder(b1, null);
    BinaryEncoder b2Enc = EncoderFactory.get().binaryEncoder(b2, null);
    // Prepare two different datums
    Record testDatum1 = new Record(record);
    testDatum1.put(0, 1);
    Record testDatum2 = new Record(record);
    testDatum2.put(0, 2);
    GenericDatumWriter<Record> gWriter = new GenericDatumWriter<Record>(record);
    Integer start1 = 0, start2 = 0;
    try {
      // Write two datums in each stream
      // and get the offset length after the first write in each.
      gWriter.write(testDatum1, b1Enc);
      b1Enc.flush();
      start1 = b1.size();
      gWriter.write(testDatum1, b1Enc);
      b1Enc.flush();
      b1.close();
      gWriter.write(testDatum2, b2Enc);
      b2Enc.flush();
      start2 = b2.size();
      gWriter.write(testDatum2, b2Enc);
      b2Enc.flush();
      b2.close();
      // Compare to check if offset-based compare works right.
      assertEquals(-1, BinaryData.compare(b1.toByteArray(), start1, b2.toByteArray(), start2, record));
    } catch (IOException e) {
      fail("IOException while writing records to output stream.");
View Full Code Here

    // If not cached, get the default Java value by encoding the default JSON
    // value and then decoding it:
    if (defaultValue == null)
      try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(baos, null);
        ResolvingGrammarGenerator.encode(encoder, field.schema(), json);
        encoder.flush();
        BinaryDecoder decoder =
          DecoderFactory.get().binaryDecoder(baos.toByteArray(), null);
        defaultValue =
          createDatumReader(field.schema()).read(null, decoder);
View Full Code Here

TOP

Related Classes of org.apache.avro.io.BinaryEncoder

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.