如何在 Neo4j 中获取 json 形式的节点和关系的分层列表

发布于 2025-01-10 08:44:12 字数 4804 浏览 0 评论 0原文

我有一个 Neo4j 数据库,其中存储用户数据和它们之间的关系,最终用户将通过移动应用程序与这些数据进行交互(应用程序位于 Flutter 中,我们在两者之间使用 Nestjs Neo4j 连接器)。现在我们必须启用对数据的离线访问。所以我们的想法是从 neo4j 将用户数据导出为 json 并在离线时使用它,当设备上线时我们将对数据库进行更改。我在获取 json 数据时遇到一些问题

这是我想要做的一个粗略示例 输入图片此处的描述

用于创建这些节点的 cypher 命令

CREATE (c:Computer {name: 'Andy',uid:'123'})
CREATE (d1:Drive {name: 'Drive1',capacity:"2gb",uid:'223'})
CREATE (d2:Drive {name: 'Drive2',capacity:"4gb",uid:'233'})
CREATE (f1:Folder {name: 'desktop',type:"special",uid:'323'})
CREATE (f2:Folder {name: 'mydocuments',type:"special",uid:'333'})
CREATE (f3:Folder {name: 'myprojects',type:"normal",uid:'343'})

CREATE (t1:File {name: 'text1',type:"txt",size:"1kb",uid:'423'})
CREATE (t2:File {name: 'text2',type:"txt",size:"1.5kb",uid:'433'})
CREATE (t3:File {name: 'text3',type:"txt",size:"2kb",uid:'443'})

CREATE (do1:File {name: 'doc1',type:"doc",size:"1mb",uid:'523'})
CREATE (do2:File {name: 'doc2',type:"doc",size:"1.5mb",uid:'533'})
CREATE (do3:File {name: 'doc3',type:"doc",size:"2mb",uid:'543'})


CREATE (c)-[r1:PARTITION{during: 'osinstall'}]->(d1)
CREATE (c)-[r2:PARTITION{during: 'setup'}]->(d2)

CREATE (d1)-[r3:AutoCreated{during: 'osinstall',type:"folder"}]->(f1)
CREATE (d1)-[r4:AutoCreated{during: 'osinstall',type:"folder"}]->(f2)
CREATE (f1)-[r5:Shortcut{type:"folder"}]->(c)
CREATE (f2)-[r6:Shortcut{type:"folder"}]->(c)
CREATE (d2)-[r7:UserCreated{type:"folder"}]->(f3)

CREATE (d2)-[r8:UserCreated{type:"file"}]->(t1)
CREATE (d2)-[r9:UserCreated{type:"file"}]->(t2)
CREATE (f3)-[r10:UserCreated{type:"file"}]->(t3)
CREATE (f3)-[r11:UserCreated{type:"file"}]->(do1)
CREATE (d2)-[r12:UserCreated{type:"file"}]->(do2)
CREATE (do2)-[r13:Shortcut{type:"file"}]->(f1)
CREATE (f3)-[r14:Shortcut{type:"folder"}]->(f1)
CREATE (f1)-[r15:UserCreated{type:"file"}]->(do3)
CREATE (do3)-[r16:Shortcut{type:"file"}]->(f3)

CREATE (c1:Computer {name: 'Randy',uid:'c1-123'})
CREATE (c1d1:Drive {name: 'Drive1',capacity:"1gb",uid:'c1-223'})
CREATE (c1t1:File {name: 'text1',type:"txt",size:"1kb",uid:'c1-423'})
CREATE (c1t2:File {name: 'text2',type:"txt",size:"1.5kb",uid:'c1-433'})
CREATE (c1sh1:SharedDrive {name:"SharedDrive",uid:'c1-s1'})

CREATE (c1)-[c1r1:PARTITION{during: 'osinstall'}]->(c1d1)
CREATE (c1d1)-[c1r2:UserCreated{type:"file"}]->(c1t1)
CREATE (c1d1)-[c1r3:UserCreated{type:"file"}]->(c1t2)
CREATE (c1t1)-[c1r4:Share{type:"file"}]->(c1sh1)
CREATE (c1)-[c1r5:SHAREDPARTITION{during: 'osinstall'}]->(c1sh1)
CREATE (c1)-[common:Network]->(c)

我想使用用户 uid 查询根节点(例如 Andy),并以

    {
    "name": "Andy",
    "uid":"123",
    "PARTITION":[
        {
            "name": "Drive1",
            "capacity":"2gb",
            "uid":"223",
            "Folder":[
               { "name": "desktop","type":"special","uid":"323",
               "File":[
                   "... Detail about file doc3 here.."
               ],
               "Shortcut":[
                   "... Detail about file doc2 here.."
               ]
               
               },
               {"name": "mydocuments","type":"special","uid":"333"}
            ]
        },
        {
            "name": "Drive2",
            "capacity":"4gb",
            "uid":"233",
            "Folder":[
               { "name": "myprojects","type":"normal","uid":"343",
                "File":[
                    "...Detail about Files doc1, text3 here..."
                ],
                "Shortcut":[
                    "... Detail about file doc3 here.."
                ]
                
               }
            ],
            "File":[
                "...Detail about Files text1,text2,doc 2 here..."
            ]

        }
    ],
    "Shortcut":[
        {
            "name": "desktop","type":"special","uid":"323"
        },{
            "name": "mydocuments","type":"special","uid":"333"
        }
    ],
    "Network":[
        {
            "name": "Randy",
            "uid":"c1-123",
            "SHAREDPARTITION":["...HERE ONLY NEED THE files and folders from shareddrive other drives should not show up..."]

        }
    ]
}

我想将节点之间的关系添加为键的 格式获取数据以及增值连接到它的节点列表(及其相关属性)并移至下一个。我不知道该怎么做。到目前为止,我已经尝试过

match (n:Computer{uid:"123"})-[r:PARTITION]->(x)
match b=(x)-[*]->(y)
with collect(b) as c
call apoc.convert.toTree(c) yield value
return value

,但这并没有正确返回快捷方式文件路径,即,如果我添加从 doc3(在桌面)到 myproject 的快捷方式,我找不到带有 myproject 快捷方式的文件详细信息,我在两个地方都需要它桌面(在文件下)和myproject(在快捷方式下)文件夹。此外,不会获取共享计算机的详细信息(不得仅获取共享分区的所有驱动器)。除此之外,返回数据不是预期的格式,我必须在获取数据后在应用程序中对其进行处理。

有人可以帮我解决这个问题吗?

我也对 Neo4j Flutter 离线的不同解决方案持开放态度。

I have a neo4j DB in which user data and relations between them would be stored, the end user will interact with this data from a mobile app (app is in Flutter, we use a nestjs neo4j connector in between). Now we have to enable offline access to data. So the idea was to export the data of the user from neo4j as json and use it when offline, when the device gets online we will make the changes to the DB. I have some problem getting the data as json

This is a rough sample what I am trying to do
enter image description here

The cypher commands to create these nodes

CREATE (c:Computer {name: 'Andy',uid:'123'})
CREATE (d1:Drive {name: 'Drive1',capacity:"2gb",uid:'223'})
CREATE (d2:Drive {name: 'Drive2',capacity:"4gb",uid:'233'})
CREATE (f1:Folder {name: 'desktop',type:"special",uid:'323'})
CREATE (f2:Folder {name: 'mydocuments',type:"special",uid:'333'})
CREATE (f3:Folder {name: 'myprojects',type:"normal",uid:'343'})

CREATE (t1:File {name: 'text1',type:"txt",size:"1kb",uid:'423'})
CREATE (t2:File {name: 'text2',type:"txt",size:"1.5kb",uid:'433'})
CREATE (t3:File {name: 'text3',type:"txt",size:"2kb",uid:'443'})

CREATE (do1:File {name: 'doc1',type:"doc",size:"1mb",uid:'523'})
CREATE (do2:File {name: 'doc2',type:"doc",size:"1.5mb",uid:'533'})
CREATE (do3:File {name: 'doc3',type:"doc",size:"2mb",uid:'543'})


CREATE (c)-[r1:PARTITION{during: 'osinstall'}]->(d1)
CREATE (c)-[r2:PARTITION{during: 'setup'}]->(d2)

CREATE (d1)-[r3:AutoCreated{during: 'osinstall',type:"folder"}]->(f1)
CREATE (d1)-[r4:AutoCreated{during: 'osinstall',type:"folder"}]->(f2)
CREATE (f1)-[r5:Shortcut{type:"folder"}]->(c)
CREATE (f2)-[r6:Shortcut{type:"folder"}]->(c)
CREATE (d2)-[r7:UserCreated{type:"folder"}]->(f3)

CREATE (d2)-[r8:UserCreated{type:"file"}]->(t1)
CREATE (d2)-[r9:UserCreated{type:"file"}]->(t2)
CREATE (f3)-[r10:UserCreated{type:"file"}]->(t3)
CREATE (f3)-[r11:UserCreated{type:"file"}]->(do1)
CREATE (d2)-[r12:UserCreated{type:"file"}]->(do2)
CREATE (do2)-[r13:Shortcut{type:"file"}]->(f1)
CREATE (f3)-[r14:Shortcut{type:"folder"}]->(f1)
CREATE (f1)-[r15:UserCreated{type:"file"}]->(do3)
CREATE (do3)-[r16:Shortcut{type:"file"}]->(f3)

CREATE (c1:Computer {name: 'Randy',uid:'c1-123'})
CREATE (c1d1:Drive {name: 'Drive1',capacity:"1gb",uid:'c1-223'})
CREATE (c1t1:File {name: 'text1',type:"txt",size:"1kb",uid:'c1-423'})
CREATE (c1t2:File {name: 'text2',type:"txt",size:"1.5kb",uid:'c1-433'})
CREATE (c1sh1:SharedDrive {name:"SharedDrive",uid:'c1-s1'})

CREATE (c1)-[c1r1:PARTITION{during: 'osinstall'}]->(c1d1)
CREATE (c1d1)-[c1r2:UserCreated{type:"file"}]->(c1t1)
CREATE (c1d1)-[c1r3:UserCreated{type:"file"}]->(c1t2)
CREATE (c1t1)-[c1r4:Share{type:"file"}]->(c1sh1)
CREATE (c1)-[c1r5:SHAREDPARTITION{during: 'osinstall'}]->(c1sh1)
CREATE (c1)-[common:Network]->(c)

I want to query a root node(say Andy) with the users uid and get the data in the format

    {
    "name": "Andy",
    "uid":"123",
    "PARTITION":[
        {
            "name": "Drive1",
            "capacity":"2gb",
            "uid":"223",
            "Folder":[
               { "name": "desktop","type":"special","uid":"323",
               "File":[
                   "... Detail about file doc3 here.."
               ],
               "Shortcut":[
                   "... Detail about file doc2 here.."
               ]
               
               },
               {"name": "mydocuments","type":"special","uid":"333"}
            ]
        },
        {
            "name": "Drive2",
            "capacity":"4gb",
            "uid":"233",
            "Folder":[
               { "name": "myprojects","type":"normal","uid":"343",
                "File":[
                    "...Detail about Files doc1, text3 here..."
                ],
                "Shortcut":[
                    "... Detail about file doc3 here.."
                ]
                
               }
            ],
            "File":[
                "...Detail about Files text1,text2,doc 2 here..."
            ]

        }
    ],
    "Shortcut":[
        {
            "name": "desktop","type":"special","uid":"323"
        },{
            "name": "mydocuments","type":"special","uid":"333"
        }
    ],
    "Network":[
        {
            "name": "Randy",
            "uid":"c1-123",
            "SHAREDPARTITION":["...HERE ONLY NEED THE files and folders from shareddrive other drives should not show up..."]

        }
    ]
}

I want to add the relations from and to the node as key and for the value add a list of nodes(with their related properties) connected to it and move to the next one. I don't know how to do so. So far I have tried

match (n:Computer{uid:"123"})-[r:PARTITION]->(x)
match b=(x)-[*]->(y)
with collect(b) as c
call apoc.convert.toTree(c) yield value
return value

but this does not return the shortcut file paths properly ie., if I add a shortcut from doc3(at desktop) to myproject, I don't find the file detail with myproject shortcuts I need it at both places at desktop(under files) and myproject(under shortcut) folder. Also the shared computer details are not fetched(all drives must not be fetched just the sharedpartition). Apart from this the return data is not in the expected format and I have to process it in the app after fetching it.

Can someone help me with this?

I am also open to different solutions for neo4j flutter offline.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2025-01-17 08:44:12

您可以从根节点创建连接,然后将它们全部收集在一起。只需确保您正在使用要提取的关系即可。下面的格式与您描述的不完全一样,但最接近您的 json 格式。

match b=(n:Computer{uid:"123"})-[r:PARTITION]->(x:Drive)-[]-(y:Folder)-[]-(z:File) 
match c=(n)-[:Shortcut]-()
match d=(n)-[:Network]-()-[:SHAREDPARTITION]-()
with collect(b) + collect(c) + collect(d) as t
call apoc.convert.toTree(t) yield value
return value

Result:

输入图片此处描述

 {
  "name": "Andy",
  "uid": "123",
  "_type": "Computer",
  "_id": 298,
  "partition": [
    {
      "uid": "233",
      "_type": "Drive",
      "name": "Drive2",
      "_id": 300,
      "partition.during": "setup",
      "capacity": "4gb",
      "usercreated": [
        {
          "uid": "343",
          "shortcut": [
            {
              "uid": "543",
              "size": "2mb",
              "shortcut.type": "file",
              "_type": "File",
              "name": "doc3",
              "_id": 309,
              "type": "doc"
            }
          ],
          "_type": "Folder",
          "name": "myprojects",
          "usercreated": [
            {
              "uid": "443",
              "size": "2kb",
              "_type": "File",
              "name": "text3",
              "_id": 306,
              "type": "txt",
              "usercreated.type": "file"
            },
            {
              "uid": "523",
              "size": "1mb",
              "_type": "File",
              "name": "doc1",
              "_id": 307,
              "type": "doc",
              "usercreated.type": "file"
            }
          ],
          "_id": 303,
          "type": "normal",
          "usercreated.type": "folder"
        }
      ]
    },
    {
      "uid": "223",
      "_type": "Drive",
      "name": "Drive1",
      "_id": 299,
      "partition.during": "osinstall",
      "capacity": "2gb",
      "autocreated": [
        {
          "autocreated.during": "osinstall",
          "autocreated.type": "folder",
          "uid": "323",
          "shortcut": [
            {
              "uid": "533",
              "size": "1.5mb",
              "shortcut.type": "file",
              "_type": "File",
              "name": "doc2",
              "_id": 308,
              "type": "doc"
            }
          ],
          "_type": "Folder",
          "name": "desktop",
          "usercreated": [
            {
              "uid": "543",
              "size": "2mb",
              "_type": "File",
              "name": "doc3",
              "_id": 309,
              "type": "doc",
              "usercreated.type": "file"
            }
          ],
          "_id": 301,
          "type": "special"
        }
      ]
    }
  ],
  "shortcut": [
    {
      "uid": "333",
      "shortcut.type": "folder",
      "_type": "Folder",
      "name": "mydocuments",
      "_id": 302,
      "type": "special"
    },
    {
      "uid": "323",
      "shortcut.type": "folder",
      "_type": "Folder",
      "name": "desktop",
      "_id": 301,
      "type": "special"
    }
  ],
  "network": [
    {
      "_type": "Computer",
      "name": "Randy",
      "uid": "c1-123",
      "_id": 310,
      "sharedpartition": [
        {
          "_type": "SharedDrive",
          "name": "SharedDrive",
          "uid": "c1-s1",
          "_id": 473,
          "sharedpartition.during": "osinstall"
        }
      ]
    }
  ]
}

You can create a connection from the root node then collect them all together. Just make sure you are using the relationship that you want to extract. Below is not exactly you described but closest to your json format.

match b=(n:Computer{uid:"123"})-[r:PARTITION]->(x:Drive)-[]-(y:Folder)-[]-(z:File) 
match c=(n)-[:Shortcut]-()
match d=(n)-[:Network]-()-[:SHAREDPARTITION]-()
with collect(b) + collect(c) + collect(d) as t
call apoc.convert.toTree(t) yield value
return value

Result:

enter image description here

 {
  "name": "Andy",
  "uid": "123",
  "_type": "Computer",
  "_id": 298,
  "partition": [
    {
      "uid": "233",
      "_type": "Drive",
      "name": "Drive2",
      "_id": 300,
      "partition.during": "setup",
      "capacity": "4gb",
      "usercreated": [
        {
          "uid": "343",
          "shortcut": [
            {
              "uid": "543",
              "size": "2mb",
              "shortcut.type": "file",
              "_type": "File",
              "name": "doc3",
              "_id": 309,
              "type": "doc"
            }
          ],
          "_type": "Folder",
          "name": "myprojects",
          "usercreated": [
            {
              "uid": "443",
              "size": "2kb",
              "_type": "File",
              "name": "text3",
              "_id": 306,
              "type": "txt",
              "usercreated.type": "file"
            },
            {
              "uid": "523",
              "size": "1mb",
              "_type": "File",
              "name": "doc1",
              "_id": 307,
              "type": "doc",
              "usercreated.type": "file"
            }
          ],
          "_id": 303,
          "type": "normal",
          "usercreated.type": "folder"
        }
      ]
    },
    {
      "uid": "223",
      "_type": "Drive",
      "name": "Drive1",
      "_id": 299,
      "partition.during": "osinstall",
      "capacity": "2gb",
      "autocreated": [
        {
          "autocreated.during": "osinstall",
          "autocreated.type": "folder",
          "uid": "323",
          "shortcut": [
            {
              "uid": "533",
              "size": "1.5mb",
              "shortcut.type": "file",
              "_type": "File",
              "name": "doc2",
              "_id": 308,
              "type": "doc"
            }
          ],
          "_type": "Folder",
          "name": "desktop",
          "usercreated": [
            {
              "uid": "543",
              "size": "2mb",
              "_type": "File",
              "name": "doc3",
              "_id": 309,
              "type": "doc",
              "usercreated.type": "file"
            }
          ],
          "_id": 301,
          "type": "special"
        }
      ]
    }
  ],
  "shortcut": [
    {
      "uid": "333",
      "shortcut.type": "folder",
      "_type": "Folder",
      "name": "mydocuments",
      "_id": 302,
      "type": "special"
    },
    {
      "uid": "323",
      "shortcut.type": "folder",
      "_type": "Folder",
      "name": "desktop",
      "_id": 301,
      "type": "special"
    }
  ],
  "network": [
    {
      "_type": "Computer",
      "name": "Randy",
      "uid": "c1-123",
      "_id": 310,
      "sharedpartition": [
        {
          "_type": "SharedDrive",
          "name": "SharedDrive",
          "uid": "c1-s1",
          "_id": 473,
          "sharedpartition.during": "osinstall"
        }
      ]
    }
  ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文