如何使用golang etcd客户端创建一个目录节点?

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

How to create a directory node using the golang etcd client?

问题

我更希望在JSON中有这样的结构:

{
"a":{"b":1, "c":2},
"x":{"y":3, "z":4}
}

我可以将"a"和"x"作为目录,并在它们下面有节点来存储数据。我在文档或示例中找不到如何完成这个任务的说明。

编辑:我刚刚通过调用/a/b、/a/c、/x/y和/x/z来将其创建为目录。这将创建所需的结构,但我正在寻找一种更简化的方法来完成相同的任务,而不是进行4次etcd调用。

英文:

I would preferably like to have a structure like this in json:

{
"a":["b":1, "c":2],
"x":["y":3, "z":4]
}

I can work with "a" & "x" as directories & have nodes under them to store data. I cannot find it in the documentation or example of how to get this done.

EDIT: I just created it as a directory by calling the /a/b, /a/c, /x/y & /x/z for Set. This creates the necessary structure, but am looking for a simplified version perhaps to do the same, instead of 4 etcd calls.

答案1

得分: -2

创建一个目录

etcdctl mkdir <my_dir>

要实现你想要的功能,可以使用以下选项:

etcdctl set myobject '{ "a": ["b":1, "c":2],"x":["y":3, "z":4]}'

将 JSON 保存为一个对象,你可以通过一次调用(使用 get)将其取回。

键是一个字符串,值也是一个字符串:所以你可以放入任何你想要的内容,只要它是一个字符串...所以 JSON 是一个字符串,你可以像其他任何字符串一样将你的 JSON 字符串放在那里。

当你需要取回它时,你可以提取 JSON 字符串并解析它。

英文:

to create a directory

etcdctl mkdir &lt;my_dir&gt;

to do what you want, there is this option:

etcdctl set myobject &#39;{&quot;a&quot;:[&quot;b&quot;:1, &quot;c&quot;:2],&quot;x&quot;:[&quot;y&quot;:3, &quot;z&quot;:4]}&#39;

will save the json as an object that you can pull back in one call (with get)

the key is a string, the value is a string: so you can put anything you want there as long as it's a string... so JSON is a string, and you can put your json string there like anything else.

when you need it back you pull the json string and parse it.

huangapple
  • 本文由 发表于 2016年1月17日 14:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/34835836.html
匿名

发表评论

匿名网友

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

确定