英文:
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 <my_dir>
to do what you want, there is this option:
etcdctl set myobject '{"a":["b":1, "c":2],"x":["y":3, "z":4]}'
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论