Writing your first Elixir code check TIL How to Select Merge with Ecto.Query

Avrora – convenient encoding/decoding messages with Avro

I would like to present a new library for convenient work with Avro serialization – Avrora.

It supports encoding/decoding:

  1. Avro Object Container Files format
  2. Confluent Schema Registry format
  3. Plain messages format (when you just want a message body)

In the case of the schema registry, it will also a register new schema. Here is an example with Object Container Files.

Let’s say you have this Payment schema stored in a file priv/schemas/io/confluent/Payment.avsc

{
  "type": "record",
  "name": "Payment",
  "namespace": "io.confluent",
  "fields": [
    {
      "name": "id",
      "type": "string"
    },
    {
      "name": "amount",
      "type": "double"
    }
  ]
}

To encode payment message with Payment.avsc schema

iex> {:ok, pid} = Avrora.start_link()
iex> message = %{"id" => "tx-1", "amount" => 15.99}
iex> {:ok, encoded} = Avrora.encode(message, schema_name: "io.confluent.Payment")

<<79, 98, 106, 1, 3, 204, 2, 20, 97, 118, 114, 111, 46, 99, 111,
  100, 101, 99, 8, 110, 117, 108, 108, 22, 97, 118, 114, 111, 46,
  115, 99, 104, 101, 109, 97, 144, 2, 123, 34, 110, 97, 109, 101,
  115, 112, 97, 99, 101, 34, 58, 34, 105, 111, 46, 99, 111, 110,
  102, 108, 117, 101, 110, 116, 34, 44, 34, 110, 97, 109, 101, 34,
  58, 34, 80, 97, 121, 109, 101, 110, 116, 34, 44, 34, 116, 121,
  112, 101, 34, 58, 34, 114, 101, 99, 111, 114, 100, 34, 44, 34,
  102, 105, 101, 108, 100, 115, 34, 58, 91, 123, 34, 110, 97, 109,
  101, 34, 58, 34, 105, 100, 34, 44, 34, 116, 121, 112, 101, 34, 58,
  34, 115, 116, 114, 105, 110, 103, 34, 125, 44, 123, 34, 110, 97,
  109, 101, 34, 58, 34, 97, 109, 111, 117, 110, 116, 34, 44, 34,
  116, 121, 112, 101, 34, 58, 34, 100, 111, 117, 98, 108, 101, 34,
  125, 93, 125, 0, 138, 124, 66, 49, 157, 51, 242, 3, 33, 52, 161,
  147, 221, 174, 114, 48, 2, 26, 8, 116, 120, 45, 49, 123, 20, 174,
  71, 225, 250, 47, 64, 138, 124, 66, 49, 157, 51, 242, 3, 33, 52,
  161, 147, 221, 174, 114, 48>>

iex> {:ok, decoded} = Avrora.decode(message, schema_name: "io.confluent.Payment")

%{"id" => "tx-1", "amount" => 15.99}

Easy as it should be! Yaw ❤️

Shameless plug: Check out @elixirstatus' other community project:

Credo, a new static code analysis tool that acts as a code linter, but also focusses on teaching coding practices and code consistency.