avro_ex

所属分类:中间件编程
开发工具:Elixir
文件大小:0KB
下载次数:0
上传日期:2023-03-28 16:01:07
上 传 者sh-1993
说明:  一个强调可测试性和易用性的Avro库。
(An Avro Library that emphasizes testability and ease of use.)

文件列表:
.credo.exs (7075, 2023-03-28)
.formatter.exs (150, 2023-03-28)
.iex.exs (61, 2023-03-28)
CHANGELOG.md (2470, 2023-03-28)
lib/ (0, 2023-03-28)
lib/avro_ex.ex (7376, 2023-03-28)
lib/avro_ex/ (0, 2023-03-28)
lib/avro_ex/decode.ex (8947, 2023-03-28)
lib/avro_ex/decode_error.ex (315, 2023-03-28)
lib/avro_ex/encode.ex (8861, 2023-03-28)
lib/avro_ex/encode_error.ex (965, 2023-03-28)
lib/avro_ex/schema.ex (7991, 2023-03-28)
lib/avro_ex/schema/ (0, 2023-03-28)
lib/avro_ex/schema/array.ex (575, 2023-03-28)
lib/avro_ex/schema/context.ex (309, 2023-03-28)
lib/avro_ex/schema/encoder.ex (3099, 2023-03-28)
lib/avro_ex/schema/enum.ex (609, 2023-03-28)
lib/avro_ex/schema/fixed.ex (608, 2023-03-28)
lib/avro_ex/schema/map.ex (664, 2023-03-28)
lib/avro_ex/schema/parser.ex (11503, 2023-03-28)
lib/avro_ex/schema/primitive.ex (458, 2023-03-28)
lib/avro_ex/schema/record.ex (936, 2023-03-28)
lib/avro_ex/schema/record/ (0, 2023-03-28)
lib/avro_ex/schema/record/field.ex (623, 2023-03-28)
lib/avro_ex/schema/reference.ex (195, 2023-03-28)
lib/avro_ex/schema/schema_decode_error.ex (2645, 2023-03-28)
lib/avro_ex/schema/union.ex (450, 2023-03-28)
mix.exs (1419, 2023-03-28)
mix.lock (3744, 2023-03-28)
test/ (0, 2023-03-28)
test/avro_ex_test.exs (3784, 2023-03-28)
test/decode_test.exs (11903, 2023-03-28)
test/encode_test.exs (23308, 2023-03-28)
... ...

# AvroEx An [Avro](https://avro.apache.org/) encoding/decoding library written in pure Elixir. ## Documentation The docs can be found on [hex.pm](https://hexdocs.pm/avro_ex/AvroEx.html) ## Installation ```elixir def deps do [{:avro_ex, "~> 2.0"}] end ``` ## Usage ### Schema Decoding Avro uses schemas to define the shape and contract for data. The schemas that your application uses may be defined locally, or may come from a [Schema Registry](https://docs.confluent.io/platform/current/schema-registry/index.html). In either case, the first step is to decode a schema defined as JSON or Elixir terms into a `t:AvroEx.Schema.t/0` ```elixir iex> AvroEx.decode_schema!(["int", "string"]) %AvroEx.Schema{ context: %AvroEx.Schema.Context{names: %{}}, schema: %AvroEx.Schema.Union{ possibilities: [ %AvroEx.Schema.Primitive{metadata: %{}, type: :int}, %AvroEx.Schema.Primitive{metadata: %{}, type: :string} ] } } ``` `AvroEx` will automatically detect Elixir terms or JSON, so you can decode JSON schemas directly ``` elixir iex> AvroEx.decode_schema!("[\"int\",\"string\"]") %AvroEx.Schema{ context: %AvroEx.Schema.Context{names: %{}}, schema: %AvroEx.Schema.Union{ possibilities: [ %AvroEx.Schema.Primitive{metadata: %{}, type: :int}, %AvroEx.Schema.Primitive{metadata: %{}, type: :string} ] } } ``` #### Strict Schema Decoding When writing an Avro schema, it is helpful to get feedback on unrecognized fields. For this purpose, it is recommended to use the `:strict` option to provide additional checks. Note that it is not recommended to use this option in production when pulling externally defined schemas, as they may have published a schema with looser validations. ``` elixir iex> AvroEx.decode_schema!(%{"type" => "map", "values" => "int", "bogus" => "value"}, strict: true) ** (AvroEx.Schema.DecodeError) Unrecognized schema key `bogus` for AvroEx.Schema.Map in %{"bogus" => "value", "type" => "map", "values" => "int"} (avro_ex 1.2.0) lib/avro_ex/schema/parser.ex:43: AvroEx.Schema.Parser.parse!/2 ``` ## Encoding When publishing Avro data, it first must be encoded using the schema. ```elixir iex> schema = AvroEx.decode_schema!(%{ "type" => "record", "name" => "MyRecord", "fields" => [ %{"name" => "a", "type" => "int"}, %{"name" => "b", "type" => "string"}, ] }) iex> AvroEx.encode!(schema, %{a: 1, b: "two"}) <<2, 6, 116, 119, 111> ``` ## Decoding When receiving Avro data, decode it using the schema ``` elixir iex> AvroEx.decode!(schema, <<2, 6, 116, 119, 111>>) %{"a" => 1, "b" => "two"} ``` ## Schema Encoding `AvroEx` also supports encoding schemas back to JSON. This may be needed when registering schemas or serializing them to disk. ``` elixir iex> AvroEx.encode_schema(schema) "{\"fields\":[{\"name\":\"a\",\"type\":{\"type\":\"int\"}},{\"name\":\"b\",\"type\":{\"type\":\"string\"}}],\"name\":\"MyRecord\",\"type\":\"record\"}" ``` Additionally, schemas can be encoded to [Parsing Canonical Form](https://avro.apache.org/docs/current/spec.html#Parsing+Canonical+Form+for+Schemas) using the `:canonical` option. ``` elixir iex> AvroEx.encode_schema(schema, canonical: true) "{\"name\":\"MyRecord\",\"type\":\"record\",\"fields\":[{\"name\":\"a\",\"type\":\"int\"},{\"name\":\"b\",\"type\":\"string\"}]}" ``` ### Testing For testing convenience, `AvroEx.encodable?/2` is exported to check if data can be encoded against the given schema. Note that in production scenarios, it is not recommended to use this function. ```elixir defmodule MyModule.Test do use ExUnit.Case setup do data = ... schema = ... {:ok, %{data: data, schema: schema}} end describe "my_function/1" do test "builds a structure that can be encoded with our avro schema", context do result = MyModule.my_function(context.data) assert AvroEx.encodable?(context.schema, result) end end end ```

近期下载者

相关文件


收藏者