【问题标题】:Avro Generic Record not taking aliases into accountAvro 通用记录未考虑别名
【发布时间】:2018-08-09 16:02:29
【问题描述】:

我有一些 JsonData(fastxml.jackson 对象),我想将其转换为 GenericAvro 记录。因为我不知道我会得到什么数据,只是模式存储库中有一个可用的 Avro 模式。我不能有预定义的类。因此,因此是通用记录。

当我漂亮地打印我的架构时,我可以看到我的键/值及其别名。但是通用记录“put”方法似乎不知道这些别名。

我收到以下异常Exception in thread "main" org.apache.avro.AvroRuntimeException: Not a valid schema field: device/id

这是设计使然吗?我怎样才能让这个架构也能看到别名?

模式提取:

"fields" : [ {
 "name" : "device_id",
 "type" : "long",
 "doc" : " The id of the device.",
 "aliases" : [ "deviceid", "device/id" ]
}, {
    ............

}]

代码:

def jsonToAvro(jSONObject: JsonNode, schema: Schema): GenericRecord = {
 val converter = new JsonAvroConverter
 println(jSONObject.toString) // correct
 println(schema.toString(true)) // correct
 println(schema.getField("device_id")) //correct
 println(schema.getField("device_id").aliases()toString) //correct

 val avroRecord = new GenericData.Record(schema)

 val iter = jSONObject.fields()

 while (iter.hasNext) {
   import java.util
   val e = jSONObject.fields()
   val entry = iter.next.asInstanceOf[util.Map.Entry[String, Nothing]]
  println(s"adding ${entry.getKey.toString} and ${entry.getValue} with ${entry.getValue.getClass.getName}") // adding device/id and 8711 with com.fasterxml.jackson.databind.node.IntNode

  avroRecord.put(entry.getKey.toString, entry.getValue) // throws 
 }

avroRecord

}

【问题讨论】:

    标签: scala avro


    【解决方案1】:

    我在 Avro 1.8.2 上尝试过,当我将 json 字符串读入 GenericRecord 时,它仍然抛出此异常:

    org.apache.avro.AvroTypeException: Expected field name not found:
    

    但我在两年前看到了一些正确使用别名的示例:

    https://www.waitingforcode.com/apache-avro/serialization-and-deserialization-with-schemas-in-apache-avro/read

    所以我猜 Avro 最近改变了这种行为

    【讨论】:

      【解决方案2】:

      似乎架构在阅读时只有这种灵活性。 编写 AVRO 只查看当前字段名称。

      不仅如此,我还在我的字段名称 (json) 中使用 "/",这是不支持作为字段名称。

      架构验证在别名中时不会抱怨,所以这可能有效(尚未测试过)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-28
        • 1970-01-01
        • 2015-08-26
        • 1970-01-01
        • 2014-12-25
        • 1970-01-01
        相关资源
        最近更新 更多