View Source ConfluentSchemaRegistry (confluent_schema_registry v0.1.1)

Elixir client for Confluent Schema Registry API.

Link to this section Summary

Functions

Create client to talk to Schema Registry.

Deletes the specified subject and its associated compatibility level if registered. It is recommended to use this API only when a topic needs to be recycled or in development environment.

Deletes a specific version of the schema registered under this subject. This only deletes the version and the schema ID remains intact making it still possible to decode data using the schema ID.

Get global compatibility level.

Get compatibility level for a subject.

Get the schema string identified by the input ID.

Get a specific version of the schema registered under this subject

Get a list of registered subjects.

Get a list of versions registered under the specified subject.

Test input schema against a particular version of a subject's schema for compatibility. Note that the compatibility level applied for the check is the configured compatibility level for the subject (get_compatibility/2). If this subject's compatibility level was never changed, then the global compatibility level applies (get_compatibility/1).

Check if a schema has already been registered under the specified subject. If so, this returns the schema string along with its globally unique identifier, its version under this subject and the subject name.

Register a new schema under the specified subject. If successfully registered, this returns the unique identifier of this schema in the registry.

Update global compatibility level.

Update compatibility level for the specified subject.

Link to this section Types

Specs

code() :: non_neg_integer()

Specs

id() :: pos_integer()

Specs

level() :: binary()

Specs

reason() :: any()

Specs

schema() :: binary()

Specs

subject() :: binary()

Specs

version() :: pos_integer() | binary()

Link to this section Functions

Specs

client(Keyword.t()) :: Tesla.Client.t()

Create client to talk to Schema Registry.

Options are:

  • base_url: URL of schema registry (optional, default "http://localhost:8081")
  • username: username for BasicAuth (optional)
  • password: password for BasicAuth (optional)
  • adapter: Tesla adapter config
  • middleware: List of additional middleware module config (optional)

examples

Examples

iex> client = ConfluentSchemaRegistry.client()
%Tesla.Client{
  adapter: nil,
  fun: nil,
  post: [],
  pre: [
    {Tesla.Middleware.BaseUrl, :call, ["http://localhost:8081"]},
    {Tesla.Middleware.Headers, :call,
     [[{"content-type", "application/vnd.schemaregistry.v1+json"}]]},
    {Tesla.Middleware.JSON, :call,
     [[decode_content_types: ["application/vnd.schemaregistry.v1+json"]]]}
  ]
}
Link to this function

delete_subject(client, subject)

View Source

Specs

delete_subject(Tesla.Client.t(), subject()) ::
  {:ok, [id()]} | {:error, code(), reason()}

Deletes the specified subject and its associated compatibility level if registered. It is recommended to use this API only when a topic needs to be recycled or in development environment.

https://docs.confluent.io/current/schema-registry/develop/api.html#delete--subjects-(string-%20subject)

Returns list of integer ids.

examples

Examples

iex> ConfluentSchemaRegistry.delete_subject(client, "test")
{:ok, [1]}
Link to this function

delete_version(client, subject, version \\ "latest")

View Source

Specs

delete_version(Tesla.Client.t(), subject(), version()) ::
  {:ok, id()} | {:error, code(), reason()}

Deletes a specific version of the schema registered under this subject. This only deletes the version and the schema ID remains intact making it still possible to decode data using the schema ID.

https://docs.confluent.io/current/schema-registry/develop/api.html#delete--subjects-(string-%20subject)-versions-(versionId-%20version)

{:ok, 1} = ConfluentSchemaRegistry.delete_version(client, "test") # latest
{:ok, 1} = ConfluentSchemaRegistry.delete_version(client, "test", 1)

Returns integer id of deleted version.

Link to this function

get_compatibility(client)

View Source

Specs

get_compatibility(Tesla.Client.t()) :: {:ok, level} | {:error, code, reason}
when level: binary(), code: non_neg_integer(), reason: any()

Get global compatibility level.

Level is a string which will be one of BACKWARD, BACKWARD_TRANSITIVE, FORWARD, FORWARD_TRANSITIVE, FULL, FULL_TRANSITIVE, NONE

https://docs.confluent.io/current/schema-registry/develop/api.html#put--config

Returns string.

examples

Examples

iex> ConfluentSchemaRegistry.get_compatibility(client)
{:ok, "BACKWARD"}
Link to this function

get_compatibility(client, subject)

View Source

Specs

get_compatibility(Tesla.Client.t(), subject()) ::
  {:ok, level()} | {:error, code(), reason()}

Get compatibility level for a subject.

Level is a string which will be one of BACKWARD, BACKWARD_TRANSITIVE, FORWARD, FORWARD_TRANSITIVE, FULL, FULL_TRANSITIVE, NONE

https://docs.confluent.io/current/schema-registry/develop/api.html#put--config

Returns string.

examples

Examples

iex> ConfluentSchemaRegistry.get_compatibility(client, "test")
{:ok, "FULL"}

Specs

get_schema(Tesla.Client.t(), id()) ::
  {:ok, schema()} | {:error, code(), reason()}

Get the schema string identified by the input ID.

https://docs.confluent.io/current/schema-registry/develop/api.html#get--schemas-ids-int-%20id

Returns binary schema.

examples

Examples

iex> ConfluentSchemaRegistry.get_schema(client, 21)
{:ok, "{\"type\":\"record\",\"name\":\"test\",\"fields\":[{\"name\":\"field1\",\"type\":\"string\"},{\"name\":\"field2\",\"type\":\"int\"}]}"}
Link to this function

get_schema(client, subject, version \\ "latest")

View Source

Specs

get_schema(Tesla.Client.t(), subject(), version()) ::
  {:ok, map()} | {:error, code(), reason()}

Get a specific version of the schema registered under this subject

https://docs.confluent.io/current/schema-registry/develop/api.html#delete--subjects-(string-%20subject)

Returns a map with the following keys:

  • subject (string) -- Name of the subject that this schema is registered under
  • id (int) -- Globally unique identifier of the schema
  • version (int) -- Version of the returned schema
  • schema (string) -- The Avro schema string
case ConfluentSchemaRegistry.get_schema(client, "test", "latest") do
  {:ok, reg} ->
    # Already registered
    schema = reg["schema"]
    schema_id = reg["id"]
  {:error, 404, %{"error_code" => 40401}} ->
    # Subject not found
  {:error, 404, %{"error_code" => 40402}} ->
    # Version not found
  {:error, 422, reason} ->
    # Unprocessable Entity, Invalid Avro version
  {:error, code, reason} ->
    # Other error
end

examples

Examples

iex> ConfluentSchemaRegistry.get_schema(client, "test")
{:ok,
 %{
   "id" => 21,
   "schema" => "{\"type\":\"record\",\"name\":\"test\",\"fields\":[{\"name\":\"field1\",\"type\":\"string\"},{\"name\":\"field2\",\"type\":\"int\"}]}",
   "subject" => "test",
   "version" => 13
 }}

iex> ConfluentSchemaRegistry.get_schema(client, "test", 13)
{:ok,
 %{
   "id" => 21,
   "schema" => "{\"type\":\"record\",\"name\":\"test\",\"fields\":[{\"name\":\"field1\",\"type\":\"string\"},{\"name\":\"field2\",\"type\":\"int\"}]}",
   "subject" => "test",
   "version" => 13
 }}

Specs

get_subjects(Tesla.Client.t()) ::
  {:ok, [subject()]} | {:error, code(), reason()}

Get a list of registered subjects.

https://docs.confluent.io/current/schema-registry/develop/api.html#get--subjects

Returns list of subject name strings.

examples

Examples

iex> ConfluentSchemaRegistry.get_subjects(client)
{:ok, ["test"]}
Link to this function

get_versions(client, subject)

View Source

Specs

get_versions(Tesla.Client.t(), subject()) ::
  {:ok, [id()]} | {:error, code(), reason()}

Get a list of versions registered under the specified subject.

https://docs.confluent.io/current/schema-registry/develop/api.html#get--subjects-(string-%20subject)-versions

{:ok, [1, 2, 3, 4]} = ConfluentSchemaRegistry.get_versions(client, "test")

Returns list of integer ids.

Link to this function

is_compatible(client, subject, schema, version \\ "latest")

View Source

Specs

is_compatible(Tesla.Client.t(), subject(), schema(), version()) ::
  {:ok, boolean()} | {:error, code(), reason()}

Test input schema against a particular version of a subject's schema for compatibility. Note that the compatibility level applied for the check is the configured compatibility level for the subject (get_compatibility/2). If this subject's compatibility level was never changed, then the global compatibility level applies (get_compatibility/1).

https://docs.confluent.io/current/schema-registry/develop/api.html#post--compatibility-subjects-(string-%20subject)-versions-(versionId-%20version)

Returns boolean.

examples

Examples

iex> ConfluentSchemaRegistry.is_compatible(client, "test", schema)
{:ok, true}

iex> ConfluentSchemaRegistry.is_compatible(client, "test", schema, "latest")
{:ok, true}

iex> ConfluentSchemaRegistry.is_compatible(client, "test", schema, 1)
{:ok, true}
Link to this function

is_registered(client, subject, schema)

View Source

Specs

is_registered(Tesla.Client.t(), subject(), schema()) ::
  {:ok, map()} | {:error, code(), reason()}

Check if a schema has already been registered under the specified subject. If so, this returns the schema string along with its globally unique identifier, its version under this subject and the subject name.

https://docs.confluent.io/current/schema-registry/develop/api.html#post--subjects-(string-%20subject)

Returns map with the following keys:

  • subject (string) -- Name of the subject that this schema is registered under
  • id (int) -- Globally unique identifier of the schema
  • version (int) -- Version of the returned schema
  • schema (string) -- The Avro schema string
schema = "{\"type\":\"record\",\"name\":\"test\",\"fields\":[{\"name\":\"field1\",\"type\":\"string\"},{\"name\":\"field2\",\"type\":\"int\"}]}"
case ConfluentSchemaRegistry.is_registered(client, "test", schema) do
  {:ok, reg} ->
    # Found
    schema = reg["schema"]
    schema_id = reg["id"]
  {:error, 404, %{"error_code" => 40401}} ->
    # Subject not found
  {:error, 404, %{"error_code" => 40403}} ->
    # Schema not found
  {:error, code, reason} ->
    # Other error
end

examples

Examples

iex> ConfluentSchemaRegistry.is_registered(client, "test", schema)
{:ok,
 %{
   "id" => 21,
   "schema" => "{\"type\":\"record\",\"name\":\"test\",\"fields\":[{\"name\":\"field1\",\"type\":\"string\"},{\"name\":\"field2\",\"type\":\"int\"}]}",
   "subject" => "test",
   "version" => 1
 }}

iex> ConfluentSchemaRegistry.is_registered(client, "test2", schema)
{:error, 404, %{"error_code" => 40401, "message" => "Subject not found. ..."}}
Link to this function

register_schema(client, subject, schema)

View Source

Specs

register_schema(Tesla.Client.t(), subject(), schema()) ::
  {:ok, id()} | {:error, code(), reason()}

Register a new schema under the specified subject. If successfully registered, this returns the unique identifier of this schema in the registry.

https://docs.confluent.io/current/schema-registry/develop/api.html#post--subjects-(string-%20subject)-versions

Returns the integer id.

case ConfluentSchemaRegistry.register_schema(client, "test", schema) do
  {:ok, schema_id} ->
    # Already registered
  {:error, 409, reason} ->
    # Conflict -- Incompatible Avro schema
  {:error, 422, reason} ->
    # Unprocessable Entity, Invalid Avro schema
  {:error, code, reason} ->
    # Other error
end

examples

Examples

iex> ConfluentSchemaRegistry.register_schema(client, "test", schema)
{:ok, 21}
Link to this function

update_compatibility(client, level)

View Source

Specs

update_compatibility(Tesla.Client.t(), level) ::
  {:ok, level} | {:error, code, reason}
when level: binary(), code: non_neg_integer(), reason: any()

Update global compatibility level.

Level is a string which must be one of BACKWARD, BACKWARD_TRANSITIVE, FORWARD, FORWARD_TRANSITIVE, FULL, FULL_TRANSITIVE, NONE

https://docs.confluent.io/current/schema-registry/develop/api.html#put--config

Returns string.

examples

Examples

iex> ConfluentSchemaRegistry.update_compatibility(client, "test", "FULL")
{:ok, "FULL"}
Link to this function

update_compatibility(client, subject, level)

View Source

Specs

update_compatibility(Tesla.Client.t(), subject(), level()) ::
  {:ok, level()} | {:error, code(), reason()}

Update compatibility level for the specified subject.

Leve is a string which must be one of BACKWARD, BACKWARD_TRANSITIVE, FORWARD, FORWARD_TRANSITIVE, FULL, FULL_TRANSITIVE, NONE

https://docs.confluent.io/current/schema-registry/develop/api.html#put--config

Returns string.

examples

Examples

iex> ConfluentSchemaRegistry.update_compatibility(client, "test", "FULL")
{:ok, "FULL"}