Avro serialisation cast error in Scala

2019-08-03 23:30发布

问题:

I am new to Scala and Avro and I am simply trying to serialise a case class

case class MyRecord(my_number: Int)

It should not be easier...

  def serializeSubmapRecord(record: MyRecord): Array[Byte] = {
    val out = new ByteArrayOutputStream()
    val encoder = EncoderFactory.get.binaryEncoder(out, null)
    val writer = new GenericDatumWriter[MyRecord](avro_schema)

    writer.write(record, encoder)
    encoder.flush
    out.close
    out.toByteArray
  }

where I obtain avro_schema as

  val avro_my_schema = scala.io.Source.fromFile("my_schema.avsc").mkString
  val avro_schema = new Parser().parse(avro_my_schema)

It compiles but upon execution I have

[error] (run-main-0) java.lang.ClassCastException: Submap cannot be cast to org.apache.avro.generic.IndexedRecord
java.lang.ClassCastException: Submap cannot be cast to org.apache.avro.generic.IndexedRecord
    at org.apache.avro.generic.GenericData.getField(GenericData.java:666)
    at org.apache.avro.generic.GenericData.getField(GenericData.java:681)
    at org.apache.avro.generic.GenericDatumWriter.writeField(GenericDatumWriter.java:151)
    at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:143)
    at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:105)
    at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:73)
    at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:60)
    ...

The Avro schema (my_schema.avsc) is

{"namespace": "",
  "type": "record",
  "name": "MyRecord",
  "fields": [
    {"name": "my_number", "type": "int"}
  ]
}

Any help would be greatly appreciated.

标签: scala avro