Aerospike Golang如何使用POLICY_KEY_SEND?

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

Aerospike Golang how to use POLICY_KEY_SEND?

问题

我正在尝试使用Golang将带有主键(PK)的记录放入Aerospike数据库。默认策略会隐藏主键,所以我需要使用POLICY_KEY_SEND策略。我已经知道如何在PHP中使用该策略,但是我不知道如何在Golang的Aerospike库中使用它。以下是我的代码(Aerospike和PHP):

Golang(不知道如何使用POLICY_KEY_SEND策略):

  1. package main
  2. import "fmt"
  3. import aero "github.com/aerospike/aerospike-client-go"
  4. func main() {
  5. client, err := aero.NewClientWithPolicyAndHost(aero.NewClientPolicy(),
  6. aero.NewHost("192.168.7.241", 3000),
  7. aero.NewHost("192.168.7.243", 3000),
  8. aero.NewHost("192.168.7.244", 3000),
  9. aero.NewHost("192.168.7.245", 3000),
  10. )
  11. if err != nil {
  12. fmt.Println("AEROSPIKE CON ERR :", nil)
  13. } else {
  14. fmt.Println("SUCCESS AEROSPIKE")
  15. namespace := "test"
  16. setName := "test_golang_set"
  17. key, err := aero.NewKey(namespace, setName, "ASDF1234")
  18. if err != nil {
  19. fmt.Println("AEROSPIKE KEY ERR :", nil)
  20. } else {
  21. // define some bins
  22. bins := aero.BinMap{
  23. "game" : "P4", // you can pass any supported type as bin value
  24. "genre" : "RPG",
  25. "price" : 59.9,
  26. }
  27. writePolicy := aero.NewWritePolicy(0, 0)
  28. err = client.Put(writePolicy, key, bins)
  29. if err != nil {
  30. fmt.Println("AEROSPIKE PUT ERR :", nil)
  31. } else {
  32. fmt.Println("AEROSPIKE PUT SUCCESS")
  33. }
  34. }
  35. }
  36. }

PHP(使用POLICY_KEY_SEND策略):

  1. <?php
  2. /*连接相关的代码*/
  3. $name_space = "test";
  4. $sets = "test_golang_set";
  5. $pk_sets = "HIJK4869";
  6. $key = $aeroDB->initKey($name_space, $sets, $pk_sets);
  7. $option = [
  8. Aerospike::OPT_POLICY_KEY => Aerospike::POLICY_KEY_SEND
  9. ];
  10. $bins = [
  11. 'game' => 'ELDEN RING',
  12. 'genre' => 'Relaxing',
  13. 'price' => 59.9
  14. ];
  15. $putStatus = $aeroDB->put($key, $bins, 0, $option);
  16. if ($putStatus == Aerospike::OK) {
  17. echo "OK";
  18. } else {
  19. echo "ERR";
  20. }

这是结果的示例,最后两条记录是由Golang生成的(因为没有使用POLICY_KEY_SEND策略,所以主键未显示),第一条记录是由PHP生成的(因为使用了POLICY_KEY_SEND策略,所以主键显示):

  1. aql> select * from test.test_golang_set
  2. +------------+--------------+------------+-------+
  3. | PK | game | genre | price |
  4. +------------+--------------+------------+-------+
  5. | "HIJK4869" | "ELDEN RING" | "Relaxing" | 59.9 |
  6. | | "P5" | "RPG" | 59.9 |
  7. | | "P4" | "RPG" | 59.9 |
  8. +------------+--------------+------------+-------+
  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)

  1. package main
  2. import &quot;fmt&quot;
  3. import aero &quot;github.com/aerospike/aerospike-client-go&quot;
  4. func main() {
  5. client, err := aero.NewClientWithPolicyAndHost(aero.NewClientPolicy(),
  6. aero.NewHost(&quot;192.168.7.241&quot;, 3000),
  7. aero.NewHost(&quot;192.168.7.243&quot;, 3000),
  8. aero.NewHost(&quot;192.168.7.244&quot;, 3000),
  9. aero.NewHost(&quot;192.168.7.245&quot;, 3000),
  10. )
  11. if err != nil {
  12. fmt.Println(&quot;AEROSPIKE CON ERR :&quot;,nil)
  13. } else {
  14. fmt.Println(&quot;SUCCESS AEROSPIKE&quot;)
  15. namespace := &quot;test&quot;
  16. setName := &quot;test_golang_set&quot;
  17. key,err := aero.NewKey(namespace,setName,&quot;ASDF1234&quot;)
  18. if err != nil {
  19. fmt.Println(&quot;AEROSPIKE KEY ERR :&quot;,nil)
  20. } else {
  21. // define some bins
  22. bins := aero.BinMap{
  23. &quot;game&quot; : &quot;P4&quot;, // you can pass any supported type as bin value
  24. &quot;genre&quot; : &quot;RPG&quot;,
  25. &quot;price&quot; : 59.9,
  26. }
  27. writePolicy := aero.NewWritePolicy(0, 0)
  28. err = client.Put(writePolicy, key, bins)
  29. if err != nil {
  30. fmt.Println(&quot;AEROSPIKE PUT ERR :&quot;,nil)
  31. } else {
  32. fmt.Println(&quot;AEROSPIKE PUT SUCCESS&quot;)
  33. }
  34. }
  35. }
  36. }

PHP (using POLICY_KEY_SEND)

  1. &lt;?php
  2. /*blablah connection stuff*/
  3. $name_space = &quot;test&quot;;
  4. $sets = &quot;test_golang_set&quot;;
  5. $pk_sets = &quot;HIJK4869&quot;;
  6. $key = $aeroDB-&gt;initKey($name_space,$sets,$pk_sets);
  7. $option = [
  8. Aerospike::OPT_POLICY_KEY =&gt; Aerospike::POLICY_KEY_SEND
  9. ];
  10. $bins = [
  11. &#39;game&#39; =&gt; &#39;ELDEN RING&#39;,
  12. &#39;genre&#39; =&gt; &#39;Relaxing&#39;,
  13. &#39;price&#39; =&gt; 59.9
  14. ];
  15. $putStatus = $aeroDB-&gt;put($key,$bins,0,$option);
  16. if($putStatus == Aerospike::OK) {
  17. echo &quot;OK&quot;;
  18. } else {
  19. echo &quot;ERR&quot;;
  20. }

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) :

  1. aql&gt; select * from test.test_golang_set
  2. +------------+--------------+------------+-------+
  3. | PK | game | genre | price |
  4. +------------+--------------+------------+-------+
  5. | &quot;HIJK4869&quot; | &quot;ELDEN RING&quot; | &quot;Relaxing&quot; | 59.9 |
  6. | | &quot;P5&quot; | &quot;RPG&quot; | 59.9 |
  7. | | &quot;P4&quot; | &quot;RPG&quot; | 59.9 |
  8. +------------+--------------+------------+-------+
  9. 3 rows in set (0.508 secs)

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

答案1

得分: 4

尝试添加以下内容:

  1. writePolicy.SendKey = true

在调用之前:

  1. 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:

  1. writePolicy.SendKey = true

before calling:

  1. 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:

确定