0

JSON Payload:

{
"BILLING_EVENT_RULE_MET_DT": "lsks",
"PlanType":"hhh"
}

AVRO Schema:

{
  "name": "Subscription",
  "type":"record",
  "doc": "Subscription details",
  "fields": 
    [
    { "name": "BILLING_EVENT_RULE_MET_DT",  "type":[ "null","string"],"default": null },

    {"name": "PlanType",
    "type":
         {
         "name":"PlanType",
         "type": "fixed",
            "size": 4
         }
    }
    ]
}

ERROR:
The value [hhh] for field [PlanType] should be [FixedType <size: 4, name: PlanType, namespace: None, aliases: None>].

While validating its giving me the following error, what I am supposed to write in the json payload for PlanType field? Actually this all I am exploring as I want to define the maxlength and minlength for a field in AVRO schema similar to XML schema.

Apu
  • 147
  • 1
  • 4
  • 12

1 Answers1

0

If the field size of "PlanType" is not a constant you cannot use the 'fixed' type.

You can either use 'string' type if the data is string or 'bytes' type if the data type is any bytes sequence.

Note that using 'string' or 'bytes' means it cannot have no characters/ zero bytes.

For enabling null value, you shall use union type (i.e. union of 'null' and 'string' or union of 'null' and 'bytes').

There is not way to have min size or max size as part of avro schema. See avro specification for that.

Eliyahu Machluf
  • 1,251
  • 8
  • 17
  • So can you give me a json example for fixed type so that I can run AVRO validation? – Apu Dec 13 '19 at 20:25
  • If the size of the field is not fixed, you should not use 'fixed' type. use 'string' instead or 'bytes' instead. At the json schema, simply replace the word 'fixed' with 'string'. – Eliyahu Machluf Dec 14 '19 at 21:41
  • And remove the "size" field – Eliyahu Machluf Dec 15 '19 at 05:41
  • No, my main objective is to know how to use the fixed type in AVRO. So I need a JSON payload which will validate to a AVRO schema which has a fixed type defined. So can you give me an example of fixed type JSON payload according to the fixed type schema I defined in AVRO?? – Apu Dec 16 '19 at 01:58
  • I don't understand entirely the question. Fixed type means the size of field is fixed. if the size is 4 bytes, the data json should contain 4 characters at the field 'PlanType' – Eliyahu Machluf Dec 18 '19 at 12:47
  • so you mean to say this payload should be validated successfully against the given schema right? { "BILLING_EVENT_RULE_MET_DT": "lsks", "PlanType":"hhhh" } – Apu Dec 19 '19 at 20:35
  • Yes. "PlanType":"hhhh" match the schema of fixed type sized 4 bytes – Eliyahu Machluf Dec 22 '19 at 08:50
  • Ut still gives me the following error: The value [hhhh] for field [PlanType] should be [FixedType ]. – Apu Dec 23 '19 at 18:45