View Source Avrora.Encoder (avrora v0.28.0)
Wraps internal codec interface to add syntax sugar which will be exposed to client.
Summary
Functions
Decode binary Avro message, loading schema from Schema Registry or Object Container Files.
Decode binary Avro message, loading schema from local file or Schema Registry.
Decode binary Avro message with a :plain format and load schema from local file.
Encode message map in Avro format, loading schema from local file or Schema Registry.
Encode binary Avro message with a :plain format and load schema from local file.
Extract schema from the binary Avro message.
Functions
Decode binary Avro message, loading schema from Schema Registry or Object Container Files.
Examples
...> payload = <<0, 0, 0, 0, 8, 72, 48, 48, 48, 48, 48, 48, 48, 48, 45, 48,
48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 123, 20, 174, 71, 225, 250, 47, 64>>
...> Avrora.Encoder.decode(payload)
{:ok, %{"id" => "00000000-0000-0000-0000-000000000000", "amount" => 15.99}}
Decode binary Avro message, loading schema from local file or Schema Registry.
Examples
...> payload = <<72, 48, 48, 48, 48, 48, 48, 48, 48, 45, 48, 48, 48, 48, 45,
48, 48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 123, 20, 174, 71, 225, 250, 47, 64>>
...> Avrora.Encoder.decode(payload, schema_name: "io.confluent.Payment")
{:ok, %{"id" => "00000000-0000-0000-0000-000000000000", "amount" => 15.99}}
Decode binary Avro message with a :plain format and load schema from local file.
Examples
...> payload = <<0, 232, 220, 144, 233, 11, 200, 1>>
...> Avrora.Encoder.decode_plain(payload,"io.confluent.NumericTransfer")
{:ok, %{"link_is_enabled" => false, "updated_at" => 1586632500, "updated_by_id" => 100}
@spec encode(map(), schema_name: String.t(), format: :guess | :registry | :ocf | :plain ) :: {:ok, binary()} | {:error, term()}
Encode message map in Avro format, loading schema from local file or Schema Registry.
The :format
argument controls output format:
:plain
- Just return Avro binary data, with no header or embedded schema:ocf
- Use [Object Container File]https://avro.apache.org/docs/1.8.1/spec.html#Object+Container+Files) format, embedding the full schema with the data:registry
- Write data with Confluent Schema Registry Wire Format, which prefixes the data with the schema id:guess
- Use:registry
if possible, otherwise use:ocf
(default)
Examples
...> payload = %{"id" => "00000000-0000-0000-0000-000000000000", "amount" => 15.99}
...> Avrora.Encoder.encode(payload, schema_name: "io.confluent.Payment", format: :plain)
{:ok, <<72, 48, 48, 48, 48, 48, 48, 48, 48, 45, 48, 48, 48, 48, 45,
48, 48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 123, 20, 174, 71, 225, 250, 47, 64>>}
Encode binary Avro message with a :plain format and load schema from local file.
Examples
...> payload = %{"link_is_enabled" => false, "updated_at" => 1586632500, "updated_by_id" => 100}
...> Avrora.Encoder.encode_plain(payload,"io.confluent.NumericTransfer")
{:ok, <<0, 232, 220, 144, 233, 11, 200, 1>>}
@spec extract_schema(binary()) :: {:ok, Avrora.Schema.t()} | {:error, term()}
Extract schema from the binary Avro message.
Examples
...> payload = <<0, 0, 0, 0, 8, 72, 48, 48, 48, 48, 48, 48, 48, 48, 45, 48,
48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 123, 20, 174, 71, 225, 250, 47, 64>>
...> {:ok, schema} = Avrora.Encoder.extract_schema(payload)
...> schema.id
42
...> schema.full_name
"io.confluent.Payment"