Documentation ¶
Index ¶
- Constants
- Variables
- func Union(name string, datum interface{}) interface{}
- type Codec
- func (c Codec) BinaryFromNative(buf []byte, datum interface{}) ([]byte, error)
- func (c Codec) NativeFromBinary(buf []byte) (interface{}, []byte, error)
- func (c Codec) NativeFromTextual(buf []byte) (interface{}, []byte, error)
- func (c Codec) Schema() string
- func (c Codec) TextualFromNative(buf []byte, datum interface{}) ([]byte, error)
- type Compression
- type ErrInvalidName
- type OCFReader
- type OCFWriter
- type OCFWriterConfig
Examples ¶
Constants ¶
const ( // CompressionNullLabel is used when OCF blocks are not compressed. CompressionNullLabel = "null" // CompressionDeflateLabel is used when OCF blocks are compressed using the // deflate algorithm. CompressionDeflateLabel = "deflate" // CompressionSnappyLabel is used when OCF blocks are compressed using the // snappy algorithm. CompressionSnappyLabel = "snappy" )
Variables ¶
var ( // MaxBlockCount is the maximum number of data items allowed in a single // binary block that will be decoded from a binary stream. This check is to // ensure decoding binary data will not cause the library to over allocate // RAM, potentially creating a denial of service on the system. // // If a particular application needs to decode binary Avro data that // potentially has more data items in a single block, then this variable may // be modified at your discretion. MaxBlockCount = int64(math.MaxInt32) // MaxBlockSize is the maximum number of bytes that will be allocated for a // single block of data items when decoding from a binary stream. This check // is to ensure decoding binary data will not cause the library to over // allocate RAM, potentially creating a denial of service on the system. // // If a particular application needs to decode binary Avro data that // potentially has more bytes in a single block, then this variable may be // modified at your discretion. MaxBlockSize = int64(math.MaxInt32) )
Functions ¶
func Union ¶ added in v0.0.2
func Union(name string, datum interface{}) interface{}
Union wraps a datum value in a map for encoding as a Union, as required by Union encoder.
Example ¶
package main import ( "fmt" "github.com/karrick/goavro" ) func main() { codec, err := goavro.NewCodec(`["null","string","int"]`) if err != nil { fmt.Println(err) } buf, err := codec.TextualFromNative(nil, goavro.Union("string", "some string")) if err != nil { fmt.Println(err) } fmt.Println(string(buf)) }
Output: {"string":"some string"}
Types ¶
type Codec ¶ added in v0.0.6
type Codec struct {
// contains filtered or unexported fields
}
Codec supports decoding binary and text Avro data to Go native data types, and conversely encoding Go native data types to binary or text Avro data. A Codec is created as a stateless structure that can be safely used in multiple go routines simultaneously.
func NewCodec ¶
NewCodec returns a Codec used to translate between a byte slice of either binary or textual Avro data and native Go data.
Creating a `Codec` is fast, but ought to be performed exactly once per Avro schema to process. Once a `Codec` is created, it may be used multiple times to convert data between native form and binary Avro representation, or between native form and textual Avro representation.
A particular `Codec` can work with only one Avro schema. However, there is no practical limit to how many `Codec`s may be created and used in a program. Internally a `Codec` is merely a named tuple of four function pointers, and maintains no runtime state that is mutated after instantiation. In other words, `Codec`s may be safely used by many go routines simultaneously, as your program requires.
codec, err := goavro.NewCodec(` { "type": "record", "name": "LongList", "fields" : [ {"name": "next", "type": ["null", "LongList"], "default": null} ] } `) if err != nil { fmt.Println(err) }
func (Codec) BinaryFromNative ¶ added in v0.2.0
BinaryFromNative appends the binary encoded byte slice representation of the provided native datum value to the provided byte slice in accordance with the Avro schema supplied when creating the Codec. It is supplied a byte slice to which to append the binary encoded data along with the actual data to encode. On success, it returns a new byte slice with the encoded bytes appended, and a nil error value. On error, it returns the original byte slice, and the error message.
func ExampleBinaryFromNative() { codec, err := goavro.NewCodec(` { "type": "record", "name": "LongList", "fields" : [ {"name": "next", "type": ["null", "LongList"], "default": null} ] } `) if err != nil { fmt.Println(err) } // Convert native Go form to binary Avro data binary, err := codec.BinaryFromNative(nil, map[string]interface{}{ "next": map[string]interface{}{ "LongList": map[string]interface{}{ "next": map[string]interface{}{ "LongList": map[string]interface{}{ // NOTE: May omit fields when using default value }, }, }, }, }) if err != nil { fmt.Println(err) } fmt.Printf("%#v", binary) // Output: []byte{0x2, 0x2, 0x0} }
func (Codec) NativeFromBinary ¶ added in v0.2.0
NativeFromBinary returns a native datum value from the binary encoded byte slice in accordance with the Avro schema supplied when creating the Codec. On success, it returns the decoded datum, along with a new byte slice with the decoded bytes consumed, and a nil error value. On error, it returns nil for the datum value, the original byte slice, and the error message.
func ExampleNativeFromBinary() { codec, err := goavro.NewCodec(` { "type": "record", "name": "LongList", "fields" : [ {"name": "next", "type": ["null", "LongList"], "default": null} ] } `) if err != nil { fmt.Println(err) } // Convert native Go form to binary Avro data binary := []byte{0x2, 0x2, 0x0} native, _, err := codec.NativeFromBinary(binary) if err != nil { fmt.Println(err) } fmt.Printf("%v", native) // Output: map[next:map[LongList:map[next:map[LongList:map[next:<nil>]]]]] }
func (Codec) NativeFromTextual ¶ added in v0.2.0
NativeFromTextual converts Avro data in JSON text format from the provided byte slice to Go native data types in accordance with the Avro schema supplied when creating the Codec. On success, it returns the decoded datum, along with a new byte slice with the decoded bytes consumed, and a nil error value. On error, it returns nil for the datum value, the original byte slice, and the error message.
func ExampleNativeFromTextual() { codec, err := goavro.NewCodec(` { "type": "record", "name": "LongList", "fields" : [ {"name": "next", "type": ["null", "LongList"], "default": null} ] } `) if err != nil { fmt.Println(err) } // Convert native Go form to text Avro data text := []byte(`{"next":{"LongList":{"next":{"LongList":{"next":null}}}}}`) native, _, err := codec.NativeFromTextual(text) if err != nil { fmt.Println(err) } fmt.Printf("%v", native) // Output: map[next:map[LongList:map[next:map[LongList:map[next:<nil>]]]]] }
func (Codec) Schema ¶ added in v0.1.7
Schema returns the compact schema used to create the Codec.
func ExampleCodecSchema() { schema := `{"type":"map","values":{"type":"enum","name":"foo","symbols":["alpha","bravo"]}}` codec, err := goavro.NewCodec(schema) if err != nil { fmt.Println(err) } fmt.Println(codec.Schema()) // Output: {"type":"map","values":{"name":"foo","type":"enum","symbols":["alpha","bravo"]}} }
func (Codec) TextualFromNative ¶ added in v0.2.0
TextualFromNative converts Go native data types to Avro data in JSON text format in accordance with the Avro schema supplied when creating the Codec. It is supplied a byte slice to which to append the encoded data and the actual data to encode. On success, it returns a new byte slice with the encoded bytes appended, and a nil error value. On error, it returns the original byte slice, and the error message.
func ExampleTextualFromNative() { codec, err := goavro.NewCodec(` { "type": "record", "name": "LongList", "fields" : [ {"name": "next", "type": ["null", "LongList"], "default": null} ] } `) if err != nil { fmt.Println(err) } // Convert native Go form to text Avro data text, err := codec.TextualFromNative(nil, map[string]interface{}{ "next": map[string]interface{}{ "LongList": map[string]interface{}{ "next": map[string]interface{}{ "LongList": map[string]interface{}{ // NOTE: May omit fields when using default value }, }, }, }, }) if err != nil { fmt.Println(err) } fmt.Printf("%s", text) // Output: {"next":{"LongList":{"next":{"LongList":{"next":null}}}}} }
type Compression ¶ added in v0.0.8
type Compression uint8
Compression are values used to specify compression algorithm used to compress and decompress Avro Object Container File (OCF) streams.
const ( // CompressionNull is used when OCF blocks are not compressed. CompressionNull Compression = iota // CompressionDeflate is used when OCF blocks are compressed using the // deflate algorithm. CompressionDeflate // CompressionSnappy is used when OCF blocks are compressed using the snappy // algorithm. CompressionSnappy )
type ErrInvalidName ¶
type ErrInvalidName struct {
Message string
}
ErrInvalidName is the error returned when one or more parts of an Avro name is invalid.
func (ErrInvalidName) Error ¶
func (e ErrInvalidName) Error() string
type OCFReader ¶ added in v0.0.8
type OCFReader struct {
// contains filtered or unexported fields
}
OCFReader structure is used to read Object Container Files (OCF).
func NewOCFReader ¶ added in v0.0.8
NewOCFReader initializes and returns a new structure used to read an Avro Object Container File (OCF).
func example(ior io.Reader) error { ocfr, err := goavro.NewOCFReader(ior) if err != nil { return err } for ocfr.Scan() { datum, err := ocfr.Read() if err != nil { return err } fmt.Println(datum) } return ocfr.Err() }
func (*OCFReader) CompressionID ¶ added in v0.1.2
func (ocfr *OCFReader) CompressionID() Compression
CompressionID returns the ID of the compression algorithm found within the OCF file.
func (*OCFReader) CompressionName ¶ added in v0.1.2
CompressionName returns the name of the compression algorithm found within the OCF file.
func (*OCFReader) Err ¶ added in v0.0.8
Err returns the last error encountered while reading the OCF file. It does not reset the read error.
func (*OCFReader) Read ¶ added in v0.0.8
Read consumes one data item from the Avro OCF stream and returns it. Read is designed to be called only once after each invocation of the Scan method. See the documentation for goavro.NewOCFReader for an example of how to use Read.
func (*OCFReader) Scan ¶ added in v0.0.8
Scan returns true when there is at least one more data item to be read from the Avro OCF. Scan ought to be called prior to calling the Read method each time the Read method is invoked. See the documentation for goavro.NewOCFReader for an example of how to use Scan.
type OCFWriter ¶ added in v0.0.8
type OCFWriter struct {
// contains filtered or unexported fields
}
OCFWriter is used to create an Avro Object Container File (OCF).
func NewOCFWriter ¶ added in v0.0.8
func NewOCFWriter(config OCFWriterConfig) (*OCFWriter, error)
NewOCFWriter returns a newly created OCFWriter which may be used to create an Avro Object Container File (OCF).
type OCFWriterConfig ¶ added in v0.0.8
type OCFWriterConfig struct { // W specifies the io.Writer to send the encode the data, (required). W io.Writer // Schema specifies the Avro schema for the data to be encoded, (required). Schema string // Codec specifies the compression codec used, (optional). If omitted, // defaults to "null" codec. Compression Compression }
OCFWriterConfig is used to specify creation parameters for OCFWriter.