Frank
Frank

Reputation: 129

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:

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

How can we deal with this?

Upvotes: 0

Views: 140

Answers (1)

Graphileon
Graphileon

Reputation: 5385

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.

Upvotes: 2

Related Questions