Sending a map in a bin from java to go

hello everyone, i have spent the last few days trying to fix a small issue and i am running out of ideas.

currently there is some code in java that writes and reads a map into a bin via Map<Value, Value> ( new HashMap<>() ). this works fine.

i am now trying to implement the same functionality in go. reading the map in go will return a MapPair which is fine.

but no matter what i try to write i cant get the map to end up in the bin the same way as from java.

anybody any idea what i can try?

I am not a developer but we do have an example of creating a map bin using Go in the Sandbox under the ‘Create’ tab. Specifically:

report := as.NewBin("report", map[string]interface{}{
    "city": "Ann Arbor",
    "state": "Michigan",
    "shape": []string{"circle", "flash", "disc"},
    "duration": "5 minutes",
    "summary": "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!"})

The equivalent for Java seems to be:

Map reportMap = new TreeMap<String, Object>();
reportMap.put("city", "Ann Arbor");
reportMap.put("state", "Michigan");
reportMap.put("shape", shape);
reportMap.put("duration", "5 minutes");
reportMap.put("summary", "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!");

Does this help by any chance?

thank you for your help!

last night i had some sort of break through - after comparing the operations line by line i realised the java code does a operate() while the go does a putBins(). the main difference is the operate accepts a MapPolicy parameter while go code doesnt have it.

if i add something like:

MapPolicy mapObsBinPolicy = new MapPolicy(MapOrder.KEY_ORDERED, MapWriteFlags.DEFAULT);

Record changeOrder = client.operate(null, key, MapOperation.setMapPolicy(mapObsBinPolicy, mapObsBinName))```

(java code found online - ive added the equivalent in go)

then it seems to work both ways however i am not sure how i could merge the put bins and set map policy in a single call… i will make sure to update if i ever work it out.

You could accomplish that in a single call using the client.Operate method.

Using the example from Meher above, if you wanted to store as a key ordered map:

import (
    "fmt"
    "github.com/aerospike/aerospike-client-go/v6" 
)

// Establishes a connection to the server
client, err := aerospike.NewClient("127.0.0.1", 3000)

// Create the record key
key, err := aerospike.NewKey("sandbox", "ufodata", 5001)

// Create the bin
report := aerospike.NewBin("report", map[string]interface{}{
    "city": "Ann Arbor",
    "state": "Michigan",
    "shape": []string{"circle", "flash", "disc"},
    "duration": "5 minutes",
    "summary": "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!"})

// Create the map policy
mapPolicy := aerospike.NewMapPolicyWithFlags(aerospike.MapOrder.KEY_ORDERED, aerospike.MapWriteFlagsDefault)

// Write the record
record, err := client.Operate(nil, key, 
    aerospike.PutOp(report),
    aerospike.MapSetPolicyOp(mapPolicy, "report"))

// Close the connection to the server
client.Close()

That should write the record, then apply the policy. You can test it out in the sandbox, just copy and paste the code sample and run it, then run from the aql prompt in the terminal:

select * from sandbox.ufodata where pk=5001

to see the result.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.