GithubHelp home page GithubHelp logo

Example for slices about avro HOT 2 CLOSED

hamba avatar hamba commented on May 19, 2024
Example for slices

from avro.

Comments (2)

nrwiersma avatar nrwiersma commented on May 19, 2024

You only need to register types with Register for a union type, so it knows what the type looks like.

It looks like the issue you are having is that schema does not contain an array. I suggest you read the spec to fully understand schemas.

This is the correct schema and code:

type SimpleRecord struct {
		A int64  `avro:"a"`
		B string `avro:"b"`
	}

	schema, err := avro.Parse(`{"type": "array", "items":
{
    "type": "record",
    "name": "simple",
    "namespace": "org.hamba.avro",
    "fields" : [
        {"name": "a", "type": "long"},
        {"name": "b", "type": "string"}
    ]
}}`)
	if err != nil {
		log.Fatal(err)
	}

	in := []SimpleRecord{{A: 27, B: "foo"}}

	data, err := avro.Marshal(schema, in)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(data)
	// Outputs: [1 10 54 6 102 111 111 0]

	out := []SimpleRecord{}
	err = avro.Unmarshal(schema, data, &out)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(out)
	// Outputs: [{27 foo}]

from avro.

yusufozturk avatar yusufozturk commented on May 19, 2024

I think this is worth to be added on documentation.. Probably many people will work with slices and they will have the same issue.

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.