Documentation
¶
Overview ¶
Package registry implements a Confluent Schema Registry compliant client.
See the Confluent Schema Registry docs for an understanding of the API: https://docs.confluent.io/current/schema-registry/docs/api.html
Example ¶
package main
import (
"context"
"fmt"
"log"
"github.com/hamba/avro/v2/registry"
)
func main() {
reg, err := registry.NewClient("http://example.com")
if err != nil {
log.Fatal(err)
}
schema, err := reg.GetSchema(context.Background(), 5)
if err != nil {
log.Fatal(err)
}
fmt.Println("schema: ", schema)
schemaRaw := `["null","string","int"]`
id, schema, err := reg.IsRegistered(context.Background(), "foobar", schemaRaw)
if err != nil {
id, schema, err = reg.CreateSchema(context.Background(), "foobar", schemaRaw)
if err != nil {
log.Fatal(err)
}
}
fmt.Println("id: ", id)
fmt.Println("schema: ", schema)
}
Index ¶
- Constants
- type Client
- func (c *Client) CreateSchema(ctx context.Context, subject, schema string, references ...SchemaReference) (int, avro.Schema, error)
- func (c *Client) DeleteSubject(ctx context.Context, subject string) ([]int, error)
- func (c *Client) GetCompatibilityLevel(ctx context.Context, subject string) (string, error)
- func (c *Client) GetGlobalCompatibilityLevel(ctx context.Context) (string, error)
- func (c *Client) GetLatestSchema(ctx context.Context, subject string) (avro.Schema, error)
- func (c *Client) GetLatestSchemaInfo(ctx context.Context, subject string) (SchemaInfo, error)
- func (c *Client) GetSchema(ctx context.Context, id int) (avro.Schema, error)
- func (c *Client) GetSchemaByVersion(ctx context.Context, subject string, version int) (avro.Schema, error)
- func (c *Client) GetSchemaInfo(ctx context.Context, subject string, version int) (SchemaInfo, error)
- func (c *Client) GetSubjects(ctx context.Context) ([]string, error)
- func (c *Client) GetVersions(ctx context.Context, subject string) ([]int, error)
- func (c *Client) IsCompatible(ctx context.Context, subject, schema string) (bool, error)
- func (c *Client) IsCompatibleWithRefs(ctx context.Context, subject, schema string, references ...SchemaReference) (bool, error)
- func (c *Client) IsRegistered(ctx context.Context, subject, schema string) (int, avro.Schema, error)
- func (c *Client) IsRegisteredWithRefs(ctx context.Context, subject, schema string, references ...SchemaReference) (int, avro.Schema, error)
- func (c *Client) SetCompatibilityLevel(ctx context.Context, subject, lvl string) error
- func (c *Client) SetGlobalCompatibilityLevel(ctx context.Context, lvl string) error
- type ClientFunc
- type Decoder
- type DecoderFunc
- type Error
- type Registry
- type SchemaInfo
- type SchemaMetadata
- type SchemaReference
Examples ¶
Constants ¶
const ( BackwardCL string = "BACKWARD" BackwardTransitiveCL string = "BACKWARD_TRANSITIVE" ForwardCL string = "FORWARD" ForwardTransitiveCL string = "FORWARD_TRANSITIVE" FullCL string = "FULL" FullTransitiveCL string = "FULL_TRANSITIVE" NoneCL string = "NONE" )
Compatibility levels.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP registry client.
func NewClient ¶
func NewClient(baseURL string, opts ...ClientFunc) (*Client, error)
NewClient creates a schema registry Client with the given base url.
func (*Client) CreateSchema ¶
func (c *Client) CreateSchema( ctx context.Context, subject, schema string, references ...SchemaReference, ) (int, avro.Schema, error)
CreateSchema creates a schema in the registry, returning the schema id.
func (*Client) DeleteSubject ¶ added in v2.7.0
DeleteSubject delete subject.
func (*Client) GetCompatibilityLevel ¶ added in v2.5.0
GetCompatibilityLevel gets the compatibility level of a subject.
func (*Client) GetGlobalCompatibilityLevel ¶ added in v2.5.0
GetGlobalCompatibilityLevel gets the global compatibility level.
func (*Client) GetLatestSchema ¶
GetLatestSchema gets the latest schema for a subject.
func (*Client) GetLatestSchemaInfo ¶
GetLatestSchemaInfo gets the latest schema and schema metadata for a subject.
func (*Client) GetSchema ¶
GetSchema returns the schema with the given id.
GetSchema will cache the schema in memory after it is successfully returned, allowing it to be used efficiently in a high load situation.
func (*Client) GetSchemaByVersion ¶
func (c *Client) GetSchemaByVersion(ctx context.Context, subject string, version int) (avro.Schema, error)
GetSchemaByVersion gets the schema by version.
func (*Client) GetSchemaInfo ¶ added in v2.1.0
func (c *Client) GetSchemaInfo(ctx context.Context, subject string, version int) (SchemaInfo, error)
GetSchemaInfo gets the schema and schema metadata for a subject and version.
func (*Client) GetSubjects ¶
GetSubjects gets the registry subjects.
func (*Client) GetVersions ¶
GetVersions gets the schema versions for a subject.
func (*Client) IsCompatible ¶ added in v2.30.0
IsCompatible determines if the schema is compatible with all schemas in the subject.
func (*Client) IsCompatibleWithRefs ¶ added in v2.30.0
func (c *Client) IsCompatibleWithRefs( ctx context.Context, subject, schema string, references ...SchemaReference, ) (bool, error)
IsCompatibleWithRefs determines if the schema is compatible with all schemas in the subject, with optional referenced schemas.
func (*Client) IsRegistered ¶
func (c *Client) IsRegistered(ctx context.Context, subject, schema string) (int, avro.Schema, error)
IsRegistered determines if the schema is registered.
func (*Client) IsRegisteredWithRefs ¶ added in v2.3.0
func (c *Client) IsRegisteredWithRefs( ctx context.Context, subject, schema string, references ...SchemaReference, ) (int, avro.Schema, error)
IsRegisteredWithRefs determines if the schema is registered, with optional referenced schemas.
func (*Client) SetCompatibilityLevel ¶ added in v2.5.0
SetCompatibilityLevel sets the compatibility level of a subject.
type ClientFunc ¶
type ClientFunc func(*Client)
ClientFunc is a function used to customize the Client.
func WithBasicAuth ¶
func WithBasicAuth(username, password string) ClientFunc
WithBasicAuth sets the credentials to perform http basic auth.
func WithHTTPClient ¶
func WithHTTPClient(client *http.Client) ClientFunc
WithHTTPClient sets the http client to make requests with.
type Decoder ¶ added in v2.6.0
type Decoder struct {
// contains filtered or unexported fields
}
Decoder decodes confluent wire formatted avro payloads.
func NewDecoder ¶ added in v2.6.0
func NewDecoder(client *Client, opts ...DecoderFunc) *Decoder
NewDecoder returns a decoder that will get schemas from client.
func (*Decoder) Decode ¶ added in v2.6.0
Decode decodes data into v. The data must be formatted using the Confluent wire format, otherwise and error will be returned. See: https://docs.confluent.io/3.2.0/schema-registry/docs/serializer-formatter.html#wire-format.
type DecoderFunc ¶ added in v2.6.0
type DecoderFunc func(*Decoder)
DecoderFunc is a function used to customize the Decoder.
func WithAPI ¶ added in v2.6.0
func WithAPI(api avro.API) DecoderFunc
WithAPI sets the avro configuration on the decoder.
type Error ¶
type Error struct {
StatusCode int `json:"-"`
Code int `json:"error_code"`
Message string `json:"message"`
}
Error is returned by the registry when there is an error.
type Registry ¶
type Registry interface {
// GetSchema returns the schema with the given id.
GetSchema(ctx context.Context, id int) (avro.Schema, error)
// DeleteSubject delete subject.
DeleteSubject(ctx context.Context, subject string) ([]int, error)
// GetSubjects gets the registry subjects.
GetSubjects(ctx context.Context) ([]string, error)
// GetVersions gets the schema versions for a subject.
GetVersions(ctx context.Context, subject string) ([]int, error)
// GetSchemaByVersion gets the schema by version.
GetSchemaByVersion(ctx context.Context, subject string, version int) (avro.Schema, error)
// GetLatestSchema gets the latest schema for a subject.
GetLatestSchema(ctx context.Context, subject string) (avro.Schema, error)
// GetSchemaInfo gets the schema and schema metadata for a subject and version.
GetSchemaInfo(ctx context.Context, subject string, version int) (SchemaInfo, error)
// GetLatestSchemaInfo gets the latest schema and schema metadata for a subject.
GetLatestSchemaInfo(ctx context.Context, subject string) (SchemaInfo, error)
// CreateSchema creates a schema in the registry, returning the schema id.
CreateSchema(ctx context.Context, subject, schema string, references ...SchemaReference) (int, avro.Schema, error)
// IsRegistered determines if the schema is registered.
IsRegistered(ctx context.Context, subject, schema string) (int, avro.Schema, error)
// IsRegisteredWithRefs determines if the schema is registered, with optional referenced schemas.
IsRegisteredWithRefs(ctx context.Context, subject, schema string, refs ...SchemaReference) (int, avro.Schema, error)
// IsCompatible determines if the schema is compatible with all schemas in the subject.
IsCompatible(ctx context.Context, subject, schema string) (bool, error)
// IsCompatibleWithRefs determines if the schema is compatible with all schemas in the subject,
// with optional referenced schemas.
IsCompatibleWithRefs(ctx context.Context, subject, schema string, refs ...SchemaReference) (bool, error)
}
Registry represents a schema registry.
type SchemaInfo ¶
type SchemaInfo struct {
Schema avro.Schema
ID int
Version int
Metadata SchemaMetadata
}
SchemaInfo represents a schema and metadata information.
func (SchemaInfo) IDBytes ¶ added in v2.30.0
func (s SchemaInfo) IDBytes() []byte
IDBytes returns 4 bytes that represent the schema ID.
type SchemaMetadata ¶ added in v2.29.0
SchemaMetadata represents the schema metadata.
type SchemaReference ¶
type SchemaReference struct {
Name string `json:"name"`
Subject string `json:"subject"`
Version int `json:"version"`
}
SchemaReference represents a schema reference.