【问题标题】:Possible to use complex object types as keys in maps in Avro?可以在 Avro 的地图中使用复杂的对象类型作为键吗?
【发布时间】:2019-11-29 15:52:14
【问题描述】:

Avro 是否可以在 Map 中使用非字符串作为键?

我发现了这个问题AVRO-680,它声称添加了非字符串键支持,但我找不到任何示例,或者从补丁中找出新支持如何与架构一起使用。

我想做这样的事情:

    "name": "aThing",
    "type": "record",
    "fields": [
        { "name": "aMapOfThings", "type": {
             "type": "map", "keys": "MyKeyType", "values": "MyValueType"
           }
        }
    ]

或在avdl

  record aThing {
    map<MyKeyType, MyValueType> aMapOfThings = [:];
  }

就像 protobuf 的这个问题一样:Protobuf objects as Keys in Maps

【问题讨论】:

  • 您找到解决方案了吗?我也对这个感兴趣,AVRO-680上的cmets不是很……清楚……
  • 不,抱歉,仍在使用有气味的字符串...

标签: java serialization avro


【解决方案1】:

查看 PR,似乎此增强功能实际上并非旨在与 SpecificRecord 一起使用。看起来代码更改和测试都集中在GenericRecord 上,并通过反射从包含非字符串键的映射的 Java 类派生模式信息。

下面是从 POJO 派生的模式示例,其中包含非字符串键的 Map(来自 TestNonStringMapKeys.java)。请注意,它是一个记录数组,而不是 map 类型:

{
  "type" : "record",
  "name" : "Company",
  "namespace" : "org.apache.avro.reflect",
  "fields" : [ {
    "name" : "employees",
    "type" : [ "null", {
      "type" : "array",
      "items" : {
        "type" : "record",
        "name" : "Pair487429dd20c65898",
        "fields" : [ {
          "name" : "key",
          "type" : {
            "type" : "record",
            "name" : "EmployeeId",
            "fields" : [ {
              "name" : "id",
              "type" : [ "null", "int" ],
              "default" : null
            } ]
          }
        }, {
          "name" : "value",
          "type" : {
            "type" : "record",
            "name" : "EmployeeInfo",
            "fields" : [ {
              "name" : "name",
              "type" : [ "null", "string" ],
              "default" : null
            } ]
          }
        } ]
      },
      "java-class" : "java.util.HashMap"
    } ],
    "default" : null
  } ]
}

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2022-01-17
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多