Giter Club home page Giter Club logo

Comments (4)

mcgrawia avatar mcgrawia commented on May 20, 2024 1

Awesome, thank you @nrwiersma! If anyone else sees this thread, I was able to implement it with:

buffer := &bytes.Buffer{}
_, err = buffer.Write([]byte{0})
if err != nil {...}

// write id in big-endian
err = binary.Write(buffer, binary.BigEndian, uint32(ID))
if err != nil {...}

// encode data
enc := avro.NewEncoderForSchema(schema, buffer)
err = enc.Encode(data)
if err != nil {...}

from avro.

nrwiersma avatar nrwiersma commented on May 20, 2024

Hi,

in the map[string]interface{} record case, the union must be a map[string]interface{} with the key as the named type. So the following works:

package main

import (
	"fmt"

	"github.com/hamba/avro"
)

func main() {
	schema, err := avro.Parse(`
{
	"type": "record",
	"name": "test", 
	"fields": [
		{"name": "PET", "type": ["null", {"type": "enum", "name": "pet", "symbols": ["dog", "cat"]}]}
	]
}`)
	if err != nil {
		fmt.Print(err)
		return
	}

	inf := map[string]interface{}{
		"PET": map[string]interface{}{
			"pet": "dog",
		},
	}

	_, err = avro.Marshal(schema, inf)
	if err != nil {
		fmt.Printf("could not encode: %v\n", err)
	}
}

I will point out that using maps if a fair bit slower than using structs in the decode case, and more painful in the encode case. I obviously dont understand your use case, but in most cases, structs are the way forward.

Also for kafka I would suggest using a schema registry, the avro/registry client and encoding the schema id into the message (there is a specific format for that). This makes evolving schemas trivial in the future. This use case is the reason I wrote the library to start with.

from avro.

mcgrawia avatar mcgrawia commented on May 20, 2024

thanks @nrwiersma! that did the trick. Also thank you for pointing me to the avro/registry client. I was about to implement my own and that will save me a lot of time.

Does this library have functions for serializing the schema id with the avro body? I couldn't find any methods to do that. If there isn't, do you know of any docs that are helpful to understand the binary format?

Thanks

from avro.

nrwiersma avatar nrwiersma commented on May 20, 2024

No worries.

The lib does not handle this step for you, as it is done in different way depending on the use case. The info you need can be found here: https://docs.confluent.io/current/schema-registry/serdes-develop/index.html#wire-format. Really simple to implement.

from avro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.