英文:
Aerospike Golang how to use POLICY_KEY_SEND?
问题
我正在尝试使用Golang将带有主键(PK)的记录放入Aerospike数据库。默认策略会隐藏主键,所以我需要使用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
/*连接相关的代码*/
$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生成的(因为没有使用POLICY_KEY_SEND策略,所以主键未显示),第一条记录是由PHP生成的(因为使用了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)
注意:我使用的是这个Golang Aerospike库:https://github.com/aerospike/aerospike-client-go
英文:
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
答案1
得分: 4
尝试添加以下内容:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论