Scala中的Avro序列化转换错误

时间:2016-08-13 19:42:14

标签: scala avro

我是Scala和Avro的新手,我只想尝试序列化case class

case class MyRecord(my_number: Int)

这应该不容易......

  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
  }

我获取avro_schema

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

它编译但是在执行时我有

[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)
    ...

Avro架构(my_schema.avsc)是

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

非常感谢任何帮助。

0 个答案:

没有答案