Creating relationships while importing data

Hi team Neo4J,


Cypers used:

//loading Customer data
LOAD CSV WITH HEADERS FROM 'file:///Customer_MOCK_DATA.csv' AS row
CREATE (c:Customer {customerId: row.customerId, firstName: row.first_name, lastName: row.last_name, dateOfBirth: row.dateOfBirth, customerSegment: row.customerSegment});

//loading and creating relationship
LOAD CSV WITH HEADERS FROM 'file:///Account_MOCK_DATA.csv' AS row
MERGE (a:Account {accountId: row.AccountId})
ON CREATE SET a.accountType = row.accountType, a.accountBalance = toInteger(row.accountBalance), a.openingDate = row.openingDate
WITH row, a
MATCH (c:Customer {customerId: row.customerId})
CREATE (c)-[:OWNS]->(a);

For second cypher : Node creation is working fine but not relations are craeting (Customer)--> (Account). Also, no error or exception was thrown.

Please suggest whats wrong with second cypher. Sample data snippet is attached

Thanks

Please keep the following things in mind:

  1. did you search for what you want to ask before posting?
  2. please use tags for additional info
  3. use a self-descriptive title

Please format code + Cypher statements with the code </> icon, it's much easier to read.

Please provide the following information if you ran into a more serious issue:

  • neo4j version, desktop version, browser version
  • what kind of API / driver do you use
  • screenshot of PROFILE or EXPLAIN with boxes expanded (lower right corner)
  • a sample of the data you want to import
  • which plugins / extensions / procedures do you use
  • neo4j.log and debug.log

You can try like this:

MATCH (c:Customer {customerId: row.customerId})
MERGE (c)-[:OWNS]->(a:Account {accountId: row.AccountId})
  ON CREATE SET a.accountType = row.accountType, a.accountBalance = toInteger(row.accountBalance), a.openingDate = row.openingDate;

Thanks Josh.

But getting below error

Variable row not defined (line 1, column 32 (offset: 31))
"MATCH (c:Customer {customerId: row.customerId})"

When trying along with load:
LOAD CSV WITH HEADERS FROM 'file:///Account_MOCK_DATA.csv' AS row

MATCH (c:Customer {customerId: row.customerId})

MERGE (c)-[:OWNS]->(a:Account {accountId: row.AccountId})

ON CREATE SET a.accountType = row.accountType, a.accountBalance = toInteger(row.accountBalance), a.openingDate = row.openingDate;

result: (no changes, no records)

Maybe its becuase there is not match between customerId and accountId. Is it necessary to have matching values among two nodes . How to draw relationshiots when there are diffent customerId and accountIdvalues.

Thanks

You removed the first part of your import ... here with MERGE:

LOAD CSV WITH HEADERS FROM 'file:///Customer_MOCK_DATA.csv' AS row
MERGE (c:Customer {customerId: row.customerId, firstName: row.first_name, lastName: row.last_name, dateOfBirth: row.dateOfBirth, customerSegment: row.customerSegment});

LOAD CSV WITH HEADERS FROM 'file:///Account_MOCK_DATA.csv' AS row
MATCH (c:Customer {customerId: row.customerId})
MERGE (c)-[:OWNS]->(a:Account {accountId: row.AccountId})
  ON CREATE SET a.accountType = row.accountType, a.accountBalance = toInteger(row.accountBalance), a.openingDate = row.openingDate;

Thanks again Josh

I tried your suggestions one after another in an empty dB

LOAD CSV WITH HEADERS FROM 'file:///Customer_MOCK_DATA.csv' AS row
MERGE (c:Customer {customerId: row.customerId, firstName: row.first_name, lastName: row.last_name, dateOfBirth: row.dateOfBirth, customerSegment: row.customerSegment});

result: Added 100 labels, created 100 nodes, set 500 properties, completed after 47 ms.

than

LOAD CSV WITH HEADERS FROM 'file:///Account_MOCK_DATA.csv' AS row

MATCH (c:Customer {customerId: row.customerId})

MERGE (c)-[:OWNS]->(a:Account {accountId: row.AccountId})

ON CREATE SET a.accountType = row.accountType, a.accountBalance = toInteger(row.accountBalance), a.openingDate = row.openingDate;

result: (no changes, no records)

Is there something missed.

Thanks

The problem would be here. If no such :Customer node exists in your graph, then rows would go to 0 and any subsequent operations would not execute.

Double-check that row.customerId returns what you are expecting from the LOAD CSV row, since these are separate CSV files, so it's possible that either column names or values may differ between them.

1 Like