Storing null values in avro files

前端 未结 2 1078
无人及你
无人及你 2021-02-14 05:06

I have some json data that looks like this:

  {
    \"id\": 1998983092,
    \"name\": \"Test Name 1\",
    \"type\": \"search string\",
    \"creationDate\": \"2         


        
2条回答
  •  青春惊慌失措
    2021-02-14 05:30

    To be able to set Avro field to null you should allow this in Avro schema, by adding null as one of the possible types of the field. Take a look on example from Avro documentation:

    {
      "type": "record",
      "name": "MyRecord",
      "fields" : [
        {"name": "userId", "type": "long"},              // mandatory field
        {"name": "userName", "type": ["null", "string"]} // optional field 
      ]
    }
    

    here userName is declared as composite type which could be either null or string. This kind of definition allows to set userName field to null. As contrast userId can only contain long values, hence attempt to set userId to null will result in NullPointerException.

提交回复
热议问题