如何在 neo4j 中为动态数据创建动态节点关系?

How to create dynamic node relation in neo4j for dynamic data?

我能够直接从 json 文件创建作者节点。但挑战在于我们必须在什么基础上或如何 link 数据。将“作者”链接到“组织”。由于数据是动态的,我们不能一概而论。我尝试过使用 csv 文件,但是当动态数据到来时它无法满足条件。例如一条 json 记录包含 2 个组织和 3 个作者,下一条记录将不同。不同的 json 记录与 link 有不同的作者和组织。 organization/1 代表组织 1,organization/2 代表组织 2。任何帮助或提示都将非常有用。谢谢你。请在下面找到 json 文件。

"Author": [
{
  "seq": "3",
  "type": "abc",
  "identifier": [
    {
      "idtype:auid": "10000000"
    }
  ],
  "familyName": "xyz",
  "indexedName": "MI",
  "givenName": "T",
    "preferredName": {
    "familyName": "xyz1",
    "givenName": "a",
    "initials": "T.",
    "indexedName": "bT."
  },
  "emailAddressList": [],
  "degrees": [],
  "@id": "https:abc/2009127993/author/person/3",
  "hasAffiliation": [
    "https:abc/author/organization/1"
  ],
  "organization": [
[
  {
    "identifier": [
      {
        "@type": "idtype:uuid",
        "@subtype": "idsubtype:affiliationInstanceId",
        "@value": "aff2"
      },
      {
        "@type": "idtype:OrgDB",
        "@subtype": "idsubtype:afid",
        "@value": "12345"
      },
      {
        "@type": "idtype:OrgDB",
        "@subtype": "idsubtype:dptid"
      }
    ],
    "organizations": [],
    "addressParts": [],
    "sourceText": "",
    "text": " Medical University School of Medicine",
    "@id": "https:abc/author/organization/1"
  }
  ],
  
  [
  {
    "identifier": [
      {
        "@type": "idtype:uuid",
        "@subtype": "idsubtype:affiliationInstanceId",
        "@value": "aff1"
      },
      {
        "@type": "idtype:OrgDB",
        "@subtype": "idsubtype:afid",
        "@value": "7890"
      },
      {
        "@type": "idtype:OrgDB",
        "@subtype": "idsubtype:dptid"
      }
    ],
    "organizations": [],
    "addressParts": [],
    "sourceText": "",
    "text": "K  University",
    "@id": "https:efg/author/organization/2"
  }
]
  
  

您好,我看到 Organization 是 Author 数据的一部分,因此您必须明智地对其进行建模。例如 (Author)-[:AFFILIATED_WITH]->(Organisation)

当您使用支持作者对象流的 apoc.load.json 时,您可以加载数据。

我用这个密码查询对你的 JSON 结构做了一些检查:

call apoc.load.json("file:///Users/keesv/work/check.json") yield value
unwind value as record
WITH record.Author as author
WITH author.identifier[0].`idtype:auid` as authorId,author,  author.organization[0] as organizations
return authorId, author, organizations

为了使这个工作正常,您需要在插件目录中创建 include apoc,并在 apoc.conf 文件中添加以下两行(如果不存在则创建一个)在 'conf'目录。

apoc.import.file.enabled=true
apoc.import.file.use_neo4j_config=false

我还在输出中看到组织的嵌套数组,这是为什么,它的含义是什么?

最后我还在 JSON 中看到一个组织可以参考其他组织。

说明 在我的查询中,我使用 UNWIND 展开基本 Author 数组。这意味着您可以为每位作者提供 'record' 合作机会。

您现在可以使用 MERGE 或 CREATE 语句创建具有正确属性的作者节点。使用 FOREACH 构造,您可以遍历所有组织条目和 create/merge 组织节点并创建作者和组织之间的关系。

这里有一个 'psuedo' 例子

call apoc.load.json("file:///Users/keesv/work/check.json") yield value
unwind value as record
WITH record.Author as author
WITH author.identifier[0].`idtype:auid` as authorId,author,  author.organization[0] as organizations
// creating the Author node
MERGE (a:Author { id: authorId })
SET a.familyName = author.familyName
...
// walk over the organizations
// determine 
FOREACH (org in organizations | 
  MERGE (o:Organization { id: ... })
  SET o.name = org.text
  ...
  MERGE (a)-[:AFFILIATED_WITH]->(o)
  // if needed you can also do a nested FOREACH here to process the Org Org relationship
)

这是我使用的 JSON 文件 我必须在开始和结束时更改一些内容

[
   {
      "Author":{
         "seq":"3",
         "type":"abc",
         "identifier":[
            {
               "idtype:auid":"10000000"
            }
         ],
         "familyName":"xyz",
         "indexedName":"MI",
         "givenName":"T",
         "preferredName":{
            "familyName":"xyz1",
            "givenName":"a",
            "initials":"T.",
            "indexedName":"bT."
         },
         "emailAddressList":[
            
         ],
         "degrees":[
            
         ],
         "@id":"https:abc/2009127993/author/person/3",
         "hasAffiliation":[
            "https:abc/author/organization/1"
         ],
         "organization":[
            [
               {
                  "identifier":[
                     {
                        "@type":"idtype:uuid",
                        "@subtype":"idsubtype:affiliationInstanceId",
                        "@value":"aff2"
                     },
                     {
                        "@type":"idtype:OrgDB",
                        "@subtype":"idsubtype:afid",
                        "@value":"12345"
                     },
                     {
                        "@type":"idtype:OrgDB",
                        "@subtype":"idsubtype:dptid"
                     }
                  ],
                  "organizations":[
                     
                  ],
                  "addressParts":[
                     
                  ],
                  "sourceText":"",
                  "text":" Medical University School of Medicine",
                  "@id":"https:abc/author/organization/1"
               }
            ],
            [
               {
                  "identifier":[
                     {
                        "@type":"idtype:uuid",
                        "@subtype":"idsubtype:affiliationInstanceId",
                        "@value":"aff1"
                     },
                     {
                        "@type":"idtype:OrgDB",
                        "@subtype":"idsubtype:afid",
                        "@value":"7890"
                     },
                     {
                        "@type":"idtype:OrgDB",
                        "@subtype":"idsubtype:dptid"
                     }
                  ],
                  "organizations":[
                     
                  ],
                  "addressParts":[
                     
                  ],
                  "sourceText":"",
                  "text":"K  University",
                  "@id":"https:efg/author/organization/2"
               }
            ]
         ]
      }
   }
]

重要为 Author.id 和 Organization.id 创建唯一约束!!

通过这种方式,您可以处理任何 json 具有未知数量的作者元素和未知数量的附属组织的文件