Question

I want to serialize a JSON object that has a potentially variable number of keys, representing cell phone deviceid's (due to Android and iPhones differences). The JSON object might look like this for example (for Android):

"deviceids":{
        "openudid":"", 
        "androidid":"dcbfXXXXXc2d5f", 
        "imei":"3533XXXXX941712"
        } 

whereas an iPhone looks like this:

"deviceids":
    {
        "openudid":"37368a5361XXXXXXXXXXdaedc186b4acf4cd4", 
        "ifv":"BD87ECBF-XXXXXXXXXX-DDF46E18129A", 
        "mac":"XXXXXXXXXX", 
        "odin":"2f5672cXXXXXXXXXX2022a5349939a2d7b952", 
        "ifa":"82F7B2AXXXXXXXXXX5-A2DADA99D05B"
        }

In Avro, I was thinking a schema like this could account for the differences:

{
      "name":"deviceids",
      "type":"record",
      "fields":[
        {
          "type":"array",
          "items":{
            "type":"map",
            "values":"string"
          }
        }
      ]
    }

Is this valid Avro schema?

Was it helpful?

Solution

Yes, a map is a valid type for an array. Your particular schema is not legal however, as it should be

{
  "name":"deviceids",
  "type":"record",
  "fields":[
    { "name": "arrayOfMaps",
      "type":{
          "type": "array",
          "items":{
            "type":"map",
            "values":"string"
            }
      }
    }
  ]
}

That is, the fields of your record must be named, and the type definition for array and map both have to be full definition giving both the outer complex type (map/array) and contained type.

Since it can be hard sometimes to answer specific Avro questions based on the available documentation and repository of examples, the easiest way to answer this sort of question is probably to just try to compile it using the Avro tools jar, which can be found alongside the regular jars in the Avro releases.

java -jar avro-tools-1.7.5.jar compile schema /path/to/schema .

This will quickly resolve the concern over whether or not it is valid. If this still doesn't resolve the issue, the Avro mailing lists seem fairly active.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top