Package org.apache.avro.io

Examples of org.apache.avro.io.Encoder


    }

    public byte[] toBytes(Object object) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Encoder encoder = new BinaryEncoder(output);
        GenericDatumWriter<Object> datumWriter = null;

        output.write(newestVersion.byteValue());
        try {
            datumWriter = new GenericDatumWriter<Object>(typeDef);
            datumWriter.write(object, encoder);
            encoder.flush();
        } catch(SerializationException sE) {
            throw sE;
        } catch(IOException e) {
            throw new SerializationException(e);
        } catch(Exception aIOBE) {
View Full Code Here


     * serialize those objects without an exception
     */
    private byte[] toBytes(Object object, Schema writer, Integer writerVersion) {

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Encoder encoder = new BinaryEncoder(output);
        GenericDatumWriter<Object> datumWriter = null;

        output.write(writerVersion.byteValue());
        try {
            datumWriter = new GenericDatumWriter<Object>(writer);
            datumWriter.write(object, encoder);
            encoder.flush();
        } catch(IOException e) {
            throw new SerializationException(e);
        } catch(SerializationException sE) {
            throw sE;
        } finally {
View Full Code Here

   * @return  The binary encoded version of <tt>n</tt>.
   * @throws IOException
   */
  private static byte[] getBinary(Schema s, JsonNode n) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Encoder e = factory.binaryEncoder(out, null);
    encode(e, s, n);
    e.flush();
    return out.toByteArray();
  }
View Full Code Here

    handshakeLock.lock();
    try {
      // force handshake
      ByteBufferOutputStream bbo = new ByteBufferOutputStream();
      // direct because the payload is tiny.
      Encoder out = ENCODER_FACTORY.directBinaryEncoder(bbo, null);
      writeHandshake(out);
      out.writeInt(0);                              // empty metadata
      out.writeString("");                          // bogus message name
      List<ByteBuffer> response =
        getTransceiver().transceive(bbo.getBufferList());
      ByteBufferInputStream bbi = new ByteBufferInputStream(response);
      BinaryDecoder in =
        DecoderFactory.get().binaryDecoder(bbi, null);
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

            Schema schema = avroDatastream.getSchema();
            DatumWriter<Object> avroWriter = new GenericDatumWriter<Object>(schema);

            JsonGenerator g = new JsonFactory().createJsonGenerator(outputStream, JsonEncoding.UTF8);
            g.useDefaultPrettyPrinter();
            Encoder encoder = new JsonEncoder(schema, g);

            int lineno = 1; // line number starts from 1
            while(avroDatastream.hasNext() && lineno <= endLine) {
                Object datum = avroDatastream.next();
                if(lineno >= startLine) {
                    String record = "\n\n Record " + lineno + ":\n";
                    outputStream.write(record.getBytes("UTF-8"));
                    avroWriter.write(datum, encoder);
                    encoder.flush();
                }
                lineno++;
            }
        } catch(IOException e) {
            outputStream.write(("Error in display avro file: " + e.getLocalizedMessage()).getBytes("UTF-8"));
View Full Code Here

          //Count of all the events in the current transaction
          eventsInTransactionCount++;
          // Serialize the row
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          Encoder encoder = new BinaryEncoder(bos);
          GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(
              record.getSchema());
          writer.write(record, encoder);
          byte[] serializedValue = bos.toByteArray();
View Full Code Here

    {
      try
      {
        // Serialize the row
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Encoder encoder = new BinaryEncoder(bos);
        GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(
            record.getSchema());
        writer.write(record, encoder);
        byte[] serializedValue = bos.toByteArray();
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

  {
    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)
    {
      genericWriter.write(r, outputEncoder);
    }
    outputEncoder.flush();
    out.flush();
  }
View Full Code Here

TOP

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

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.