I am developing an e-commerce site and faced this issue. Here is my code.

  def toBytes[A](a: A, schema: => org.apache.avro.Schema) = {
    System.out.println("===============toBytes==============")
    val out = new ByteArrayOutputStream()
    val encoder = EncoderFactory.get().binaryEncoder(out, null)
    SessionDto.getClassSchema
    val writer = new SpecificDatumWriter[A](schema)
    writer.write(a, encoder)
    encoder.flush()
    out.close()
    out.toByteArray
  }
    def fromBytes[A](bytes: Array[Byte], schema: => org.apache.avro.Schema)
= {
    val reader = new SpecificDatumReader[A](schema)
    val decoder = DecoderFactory.get().binaryDecoder(bytes, null)
    reader.read(null.asInstanceOf[A], decoder)
  }

>From Avro to byte array is okay but otherwise getting an error.

Please help me if you have any solution. How can I get Avro schema from
byte array?

Reply via email to