Aerospike Golang如何使用POLICY_KEY_SEND?

huangapple go评论82阅读模式
英文:

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 &quot;fmt&quot;
import aero &quot;github.com/aerospike/aerospike-client-go&quot;

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

		namespace := &quot;test&quot;
		setName := &quot;test_golang_set&quot;

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

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

PHP (using POLICY_KEY_SEND)

&lt;?php

/*blablah connection stuff*/

$name_space = &quot;test&quot;;
$sets  = &quot;test_golang_set&quot;;
$pk_sets = &quot;HIJK4869&quot;;
$key = $aeroDB-&gt;initKey($name_space,$sets,$pk_sets);

$option = [
    Aerospike::OPT_POLICY_KEY =&gt; Aerospike::POLICY_KEY_SEND
];

$bins = [
    &#39;game&#39;   =&gt; &#39;ELDEN RING&#39;,
    &#39;genre&#39;   =&gt; &#39;Relaxing&#39;,
    &#39;price&#39;    =&gt; 59.9
];

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

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

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&gt; select * from test.test_golang_set
+------------+--------------+------------+-------+
| PK         | game         | genre      | price |
+------------+--------------+------------+-------+
| &quot;HIJK4869&quot; | &quot;ELDEN RING&quot; | &quot;Relaxing&quot; | 59.9  |
|            | &quot;P5&quot;         | &quot;RPG&quot;      | 59.9  |
|            | &quot;P4&quot;         | &quot;RPG&quot;      | 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

huangapple
  • 本文由 发表于 2022年4月11日 15:57:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/71824413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定