Home > Blockchain >  Aerospike Golang how to use POLICY_KEY_SEND?
Aerospike Golang how to use POLICY_KEY_SEND?

Time:04-12

Im trying to put a record with PK using golang, the default policy prevent PK to shows so i need to use policy POLICY_KEY_SEND. im able to put this policy using PHP but i have no clue how to use it in golang aerospike library, this is my code (Aerospike and PHP)

Golang (no clue how to put policy POLICY_KEY_SEND)

package main

import "fmt"
import aero "github.com/aerospike/aerospike-client-go"

func main() {
    
    client, err := aero.NewClientWithPolicyAndHost(aero.NewClientPolicy(), 
        aero.NewHost("192.168.7.241", 3000), 
        aero.NewHost("192.168.7.243", 3000), 
        aero.NewHost("192.168.7.244", 3000), 
        aero.NewHost("192.168.7.245", 3000),
    )
    
    if err != nil {
        fmt.Println("AEROSPIKE CON ERR :",nil)
    } else {
        fmt.Println("SUCCESS AEROSPIKE")

        namespace := "test"
        setName := "test_golang_set"

        key,err := aero.NewKey(namespace,setName,"ASDF1234")
        if err != nil {
            fmt.Println("AEROSPIKE KEY ERR :",nil)
        } else {
            // define some bins
            bins := aero.BinMap{
                "game"  : "P4", // you can pass any supported type as bin value
                "genre" : "RPG",
                "price" : 59.9,
            }

            writePolicy := aero.NewWritePolicy(0, 0)
            err = client.Put(writePolicy, key, bins)
            
            if err != nil {
                fmt.Println("AEROSPIKE PUT ERR :",nil)
            } else {
                fmt.Println("AEROSPIKE PUT SUCCESS")
            }
        }
    }
}

PHP (using POLICY_KEY_SEND)

<?php

/*blablah connection stuff*/

$name_space = "test";
$sets  = "test_golang_set";
$pk_sets = "HIJK4869";
$key = $aeroDB->initKey($name_space,$sets,$pk_sets);

$option = [
    Aerospike::OPT_POLICY_KEY => Aerospike::POLICY_KEY_SEND
];

$bins = [
    'game'   => 'ELDEN RING',
    'genre'   => 'Relaxing',
    'price'    => 59.9
];

$putStatus = $aeroDB->put($key,$bins,0,$option);

if($putStatus == Aerospike::OK) {
    echo "OK";
} else {
    echo "ERR";
}

this is sample of result, two last records generated by Golang (PK not shown because not using POLICY_KEY_SEND) , the first record by PHP (PK shown, because of POLICY_KEY_SEND) :

aql> select * from test.test_golang_set
 ------------ -------------- ------------ ------- 
| PK         | game         | genre      | price |
 ------------ -------------- ------------ ------- 
| "HIJK4869" | "ELDEN RING" | "Relaxing" | 59.9  |
|            | "P5"         | "RPG"      | 59.9  |
|            | "P4"         | "RPG"      | 59.9  |
 ------------ -------------- ------------ ------- 
3 rows in set (0.508 secs)

ps : im using this golang aerospike : https://github.com/aerospike/aerospike-client-go

CodePudding user response:

Try adding:

writePolicy.SendKey = true

before calling:

err = client.Put(writePolicy, key, bins)

According to Aerospike Go Client documentation: SendKey option is a part of BasePolicy (default is SendKey = false) which is a base policy of WritePolicy.

https://pkg.go.dev/github.com/aerospike/aerospike-client-go#BasePolicy https://pkg.go.dev/github.com/aerospike/aerospike-client-go#WritePolicy

  • Related