英文:
How can I import a simple json dataset into memgraph
问题
I have read all information about importing json but find that documentation poor. The memgraph docs say, just do this:
CALL json_util.load_from_url("https://download.memgraph.com/asset/mage/data.json")
YIELD objects
UNWIND objects AS o
RETURN o.first_name AS name, o.last_name AS surname;
But that does not do it, because a typical json graph dataset has several properties holding nodes and links with arbitrary properties:
{
nodes: [ ... ],
links: [ ... ],
...
}
So we need to access these properties and we need to create nodes with all properties of the imported objects. I cant find any documentation on how to do this. I finally managed to create nodes inside memgraph with the correct type ("ENTITY") but do not see how I can assign those nodes all properties from the imported json data:
CALL json_util.load_from_path("/data.json")
YIELD objects // "objects" is the named result from the function above
UNWIND objects as o // create a list of the properties of the imported result, which is a single object in my case and in most cases
UNWIND o.nodes as n // select the nodes and return them as list
CREATE (e:ENTITY { id:n.id }) // all nodes will have 1 property, "id"
RETURN e
All nodes will have 1 property, "id", but I need all properties. How is this done. Where is the documentation or the book that explains that?
In javascript I would write
CREATE (e:ENTITY { ...n })
How is this done in Cypher? Or how can this done in memgraph with another language or tool?
英文:
I have read all information about importing json but find that documentation poor. The memgraph docs say, just do this:
CALL json_util.load_from_url("https://download.memgraph.com/asset/mage/data.json")
YIELD objects
UNWIND objects AS o
RETURN o.first_name AS name, o.last_name AS surname;
But that does not do it, because a typical json graph dataset has several properties holding nodes and links with arbitrary properties:
{
nodes: [ ... ],
links: [ ... ],
...
}
So we need to access these properties and we need to create nodes with all properties of the imported objects. I cant find any documentation on how to do this. I finally managed to create nodes inside memgraph with the correct type ("ENTITY") but do not see how I can assign those nodes all properties from the imported json data:
CALL json_util.load_from_path("/data.json")
YIELD objects // "objects" is the named result from the function above
UNWIND objects as o // create a list of the properties of the imported result, which is a single object in my case and in most cases
UNWIND o.nodes as n // select the nodes and return them as list
CREATE (e:ENTITY { id:n.id }) // all nodes will have 1 property, "id"
RETURN e
All nodes will have 1 property, "id", but I need all properties. How is this done. Where is the documentation or the book that explains that?
In javascript I would write
CREATE (e:ENTITY { ...n })
How is this done in Cypher? Or how can this done in memgraph with another language or tool?
答案1
得分: 2
如果您使用 json_util.load_from_path
过程,那么您必须像在 Memgraph 文档 示例中那样指定要写入数据库的每个属性:
CALL json_util.load_from_path("path/to/data.json")
YIELD objects
UNWIND objects AS o
CREATE (:Person {first_name: o.first_name, last_name: o.last_name, pets: o.pets});
上述过程期望经典的 JSON,没有特定的结构。
另一方面,import_util.json()
过程期望 JSON 以特定格式呈现,结构与 export_util.json()
过程生成的 JSON 文件相同。请参考 Memgraph 文档 中的示例。
如果您希望按照当前结构导入数据,您需要使用 json_util.load_from_path()
过程并写下您想在数据库中创建的所有属性。否则,您可以为 import_util.json()
过程适当地构造数据并以此方式导入。您在 JavaScript 中提到的构造在 Cypher 中不受支持。
假设您在 JSON 文件中还有 name
和 last_name
属性,那么您会运行:
CALL json_util.load_from_path("/data.json")
YIELD objects // "objects" 是上述函数的命名结果
UNWIND objects as o // 创建导入结果属性的列表,在我这里和大多数情况下,这是一个单一对象
UNWIND o.nodes as n // 选择节点并将它们作为列表返回
CREATE (e:ENTITY { id:n.id, name: n.name, last_name: n.last_name })
RETURN e
我们在 下一个版本 中修复了导入文档,如果现在更清楚了,请告诉我。这里也有 JSON 导入子页面。
英文:
If you use json_util.load_from_path
procedure, then you have to specify each of the properties you want to write to the database, just like in the example in Memgraph docs:
CALL json_util.load_from_path("path/to/data.json")
YIELD objects
UNWIND objects AS o
CREATE (:Person {first_name: o.first_name, last_name: o.last_name, pets: o.pets});
The above procedure is expecting classic JSON, not structured in any particular way.
On the other hand, the import_util.json()
procedure expects JSON to be in a specific format, structured the same as the JSON file that the export_util.json()
procedure generates. Check the example in Memgraph docs.
If you wish to import your data in a way they are structured now, you need to use json_util.load_from_path()
procedure and write down all of the properties you want to create in the database. Otherwise, you can structure your data appropriately for the import_util.json()
procedure and import it that way. The construct you mentioned from the JavaScript is not supported in Cypher.
Let's suppose other properties you have in the JSON file are name
and last_name
. Then you would run this:
CALL json_util.load_from_path("/data.json")
YIELD objects // "objects" is the named result from the function above
UNWIND objects as o // create a list of the properties of the imported result, which is a single object in my case and in most cases
UNWIND o.nodes as n // select the nodes and return them as list
CREATE (e:ENTITY { id:n.id, name: n.name, last_name: n.last_name })
RETURN e
We fixed the import docs in the next version, and let me know if that's more clear now. Here is also the JSON import subpage.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论