简体   繁体   中英

Create Relationship between (not yet) existing nodes

I'm trying to ingest data from Kafka to Neo4j via the neo4j-stream plugin. Currently we are experimenting with the CUD strategy.

I wonder how we should deal with interconnected data. Let's assume we stream 100 persons from Kafka to Neo4j. When person(id:1) has a relationship is_friends_with with person(id:50) we can not create this relationship until the node for person(id:50) is created.

However, we have no control about the order of how the events are coming from Kafka.

So when the person data arrives, we would create the following CUD data:

  • create node for person(id:1)
  • create relationship is_friends_with(person1, person50) .
  • ...
  • create node for person(id:50)

Unsurprisingly, when we ingest the events in this order the relationship is missing.

How can we deal with this?

I think this is a typical case to use MERGE.

Every time you have a relationship (which has start and end nodes) you do

MERGE (n:Person {id:foo})
MERGE (m:Person {id:bar})
MERGE (n)-[:is_friends_with]-(m)

The merge for the relationship does NOT have a direction, in order to avoid bi-directional patterns in case n and m arrive in different order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM