ramu
ramu

Reputation: 1325

Problems in avro schema .. String, null

In this avro schema

{"type": "record",
"name": "Member",
"fields": [
 {"name": "name", "type": ["string", "null"] },
 {"name": "skill", "type": "string"}
]}

When I try to assign multiple values to name like multiple records ...

ByteArrayOutputStream bos=new ByteArrayOutputStream();
Encoder e= EncoderFactory.get().binaryEncoder(bos, null);
GenericRecord rec=new GenericData.Record(schema);
GenericDatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(schema);

for(int i=0;i<memlist.size();i++)
{
   rec.put("name", memlist.get(i).name);
   rec.put("skill", memlist.get(i).skill);
   datumWriter.write(rec, e);
}

e.flush();
bos.close();
System.out.println(bos.toString());

It always assign Null FOR the NAME field in the schema ... except for the first record that I put ... Is there a way I can use default as STRING ... and if there is no string then only it uses null mentioned in the schema

Unions, as mentioned above, are represented using JSON arrays. For example, ["string", "null"] declares a schema which may be either a string or null.

Unions may not contain more than one schema with the same type, except for the named types record, fixed and enum. For example, unions containing two array types or two map types are not permitted, but two types with different names are permitted. (Names permit efficient resolution when reading and writing unions.)

So it always take NULL even though string value exist except for the first record

Upvotes: 0

Views: 12051

Answers (1)

LiMuBei
LiMuBei

Reputation: 3078

Not sure I understand correctly what you want, but why don't you specify a default value for the name field?

{"type": "record",
 "name": "Member",
 "fields": [
    {"name": "name", "type": ["string", "null"], "default": "unknown" },
    {"name": "skill", "type": "string"}
]}

Then, if you don't explicitly set the field value to null it should use the string default.

Upvotes: 0

Related Questions