LOAD CSV with dynamic relations and missing properties

Hi!

I'm struggling with importing "dynamic" relations. The relations are the results from join-operations in an RDBMS. The export works as intended but I struggle to import it into Neo4j. The "dynamic" relations are solved by using the apoc.merge.relationship function but I'm running into problems with the properties for the relations as they're sometimes NULL. Is it possible to perform some sort of "CASE" operation to solve this? My current Cypher approach looks like this:

LOAD CSV WITH HEADERS FROM 'http://hostname/exports/relation_person_organisation_position.txt' AS row FIELDTERMINATOR '\t'
MATCH (o:Organisation {id: row.organisation_id})
MATCH (p:Person {id: row.person_id})
CALL apoc.merge.relationship(p, row.relation_in_neo4j, {since: row.since, notes: row.notes}, {}, o) YIELD rel RETURN rel;

I'm far from a Cypher guru so please bare with my Cypher code above :)

You could use a few solutions:

  1. As you stated, use 'case' to set a default value to avoid null
    Expressions - Cypher Manual
  2. Use coalesce to set a default value to avoid null
    Scalar functions - Cypher Manual
  3. Use apoc.map.clean to remove the null properties (this avoids needing to set default values)
    apoc.map.clean - APOC Extended Documentation
LOAD CSV WITH HEADERS FROM 'http://hostname/exports/relation_person_organisation_position.txt' AS row FIELDTERMINATOR '\t'
MATCH (o:Organisation {id: row.organisation_id})
MATCH (p:Person {id: row.person_id})
CALL apoc.merge.relationship(p, row.relation_in_neo4j, apoc.map.clean({since: row.since, notes: row.notes}, [], []), {}, o) YIELD rel RETURN rel;

Hi Gary!

Many thanks for your reply! I think I find your apoc.map.clean the most appealing so will test that :slight_smile:

Cheers