Writing Apache Avro Schemas with Jsonnet

Apache Avro enables schemas to describe data objects across languages and system that serializes to binary for interoperation and transmission. Avro schemas ultimately end up describing your domain. These schemas are in json format and working with json is tedious. There is a lot of copying and pasting and wasted opportunities for being DRY like when you have a nested record in a schema.

In comes Jsonnet which is a configuration language for app and tool developers. It was open sourced by Google and is easy to pick up.

I open sourced what you can do with Avro and Jsonnet here.

Here is how you can express an Avro schema for a user with a nested address using the little Jsonnet library I created (17 lines of code).

local avro = import 'avro.libsonnet';

local email = avro.Field("email");
local firstName = avro.Field("firstName");
local lastName = avro.Field("lastName");

local street_address = avro.Field("street_address", nullable=true);
local preferred = avro.Record(
name="preferred",
fields=[street_address]
);

local address = avro.Field("address", preferred, nullable=true);

local user = avro.Record(
name="user",
fields=[
email,
firstName,
lastName,
address
],
namespace="domain",
doc="main user object"
);

user

Take a look at the repo and dig into the code and let me know what you think. I have used Jsonnet in production before and it was a great addition to how we were building our systems. I have used Apache Avro many times and in fact when I start a new project and need to describe the domain I usually create avro objects (from jsonnet) as you can use those json schemas to parse and create DDL and do other things too.

Thanx =8^) Joe Stein

https://www.twitter.com/charmalloc

https://www.linkedin.com/in/charmalloc

Leave a Reply

Discover more from Hello BitsNBytes World

Subscribe now to keep reading and get access to the full archive.

Continue reading