You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by Oded Zahavi <od...@fiverr.com.INVALID> on 2018/12/17 12:25:36 UTC

Reading data with schema of type map

Hi,

I’m working with Avro Turf which in turn works with Avro. Currently using version 1.8.2. I have the following schema:
{
  "type": "record",
  "name": “my_record",
  "fields": [
    {
      "name": "work_sample",
      "type": [
        {
          "type": "map",
          "values": "long"
        },
        "null"
      ],
      "default": null
    }
  ]
}

and I am trying to read the following data with this schema:
{
  "id": 457604,
  "updated_at": 1531640762,
  "is_allowed": true
}

Since I have default null, I expected to get 
{"work_sample":null}

However, what I get is an error. This seems to work as expected for the array case. It looks like since in the array case a type check is done before, allowing it to receive null values and apply the default. I fixed this locally and it looks okay. I have opened a pull request I’d love you guys to have a look:
https://github.com/apache/avro/pull/403/files <https://github.com/apache/avro/pull/403/files>

Thanks,
Oded Zahavi

Re: Reading data with schema of type map

Posted by RumeshKrishnan Mohan <ru...@gmail.com>.
Hi,

Hi,

Please go through the union type default behaviour. It says you can define default for union type with first type of union value. Hence you defined map as first union type you must define default value with map type. Otherwise you can change the schema like below for your requirements.

Schema :

> {
>  "type": "record",
>  "name": “my_record",
>  "fields": [
>    {
>      "name": "work_sample",
>      "type": [
         “null”,
>        {
>          "type": "map",
>          "values": "long"
>        }
>      ],
>      "default": null
>    }
>  ]
> }

Hope this will help you.

For more details: 
https://avro.apache.org/docs/1.8.1/spec.html#Unions

Sent from my iPhone

> On 17-Dec-2018, at 1:25 PM, Oded Zahavi <od...@fiverr.com.INVALID> wrote:
> 
> Hi,
> 
> I’m working with Avro Turf which in turn works with Avro. Currently using version 1.8.2. I have the following schema:
> {
>  "type": "record",
>  "name": “my_record",
>  "fields": [
>    {
>      "name": "work_sample",
>      "type": [
>        {
>          "type": "map",
>          "values": "long"
>        },
>        "null"
>      ],
>      "default": null
>    }
>  ]
> }
> 
> and I am trying to read the following data with this schema:
> {
>  "id": 457604,
>  "updated_at": 1531640762,
>  "is_allowed": true
> }
> 
> Since I have default null, I expected to get 
> {"work_sample":null}
> 
> However, what I get is an error. This seems to work as expected for the array case. It looks like since in the array case a type check is done before, allowing it to receive null values and apply the default. I fixed this locally and it looks okay. I have opened a pull request I’d love you guys to have a look:
> https://github.com/apache/avro/pull/403/files <https://github.com/apache/avro/pull/403/files>
> 
> Thanks,
> Oded Zahavi