Aerospike Golang如何使用POLICY_KEY_SEND?

Aerospike Golang how to use POLICY_KEY_SEND?

我正在尝试使用 golang 使用 PK 记录,默认策略阻止 PK 显示,所以我需要使用策略 POLICY_KEY_SEND。我可以使用 PHP 来设置此策略,但我不知道如何在 golang aerospike 库中使用它,这是我的代码(Aerospike 和 PHP)

Golang(不知道如何放置策略 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(使用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";
}

这是结果样本,最后两条记录由 Golang 生成(PK 未显示,因为未使用 POLICY_KEY_SEND),第一条记录由 PHP(PK 显示,因为 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:我正在使用这个 golang aerospike:https://github.com/aerospike/aerospike-client-go

尝试添加:

writePolicy.SendKey = true

调用前:

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

根据 Aerospike Go 客户端文档: SendKey 选项是 BasePolicy 的一部分(默认为 SendKey = false),它是 WritePolicy.

的基本策略

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