Antony Stubbs
Antony Stubbs

Reputation: 13585

Possible to use complex object types as keys in maps in Avro?

Is it possible to use non Strings as keys in a Map with Avro?

I found this issue AVRO-680 which claims to have added non string key support, but I cannot find any examples, or work out from the patch how the new support works with a schema.

I would like to do something like this:

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

Or in avdl:

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

Exactly like this question for protobuf: Protobuf objects as Keys in Maps

Upvotes: 2

Views: 2528

Answers (1)

Mike Bingham
Mike Bingham

Reputation: 21

Looking at the PR, it doesn't appear that this enhancement is actually designed to work with SpecificRecord. Looks like the code changes and tests are focused on GenericRecord, and deriving schema information via reflection from a Java class containing map(s) with non-string keys.

Below is an example of a schema derived from a POJO with a Map containing non-string keys (from TestNonStringMapKeys.java). Notice it’s an array of records instead of a map type:

{
  "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
  } ]
}

Upvotes: 2

Related Questions