You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Liu Bo <di...@gmail.com> on 2016/03/09 14:49:56 UTC

set avro string type to String programmatically for generic record

Hi group

We are using avro generic record as message inside our streaming processing
program.
The dynamic typing feature works greatly until we need to call remote http
service using json as the post body. The string values from the generic
record are serialized as utf8 object in JSON.

I decided to let avro deserialize strings to String instead of Utf8 to
avoid any further misuse of the Utf8 object.

One way to do this is add { "type": "string", "avro.java.string": "String" }
for every String type in our schema.
The other way I've found out is programmatically change it via:
GenericData.setStringType(schema, StringType.String);

So I write a simple method to do this after get schema from the config
center. The idea is traversal the schema tree and change the String for
type STRING, UNION, ARRAY, MAP(key and value I think). The code is attached
as a screenshot and it works.

My questions is "is there any drawbacks preventing me using this way"?
If this way is ok, is there anything missing in the attached method? Ps,
one method provided by avro to set the String type globally would be very
nice ;-)

Your help is greatly appreciated~~

-- 
All the best

Liu Bo