Facing problem to create relationship between two different node from csv

Hello, For testing purpose I am trying to make relationship two nodes . One node has Customer label and other node as Device label

Node creation is done. I am. facing problem to make relationship between two nodes

RELATIONSHIP,IDENTITY_ID,DEVICE_ID
HAS_DEVICE,22q,1010k
HAS_DEVICE,q22,1020k
HAS_DEVICE,ac23,1030k
HAS_DEVICE,dfd,1050k
HAS_DEVICE,22q,1070k
HAS_DEVICE,ac23,1090k

This is my relationship device.csv file . The identity_id values exists in the CUSTOMER labeled nodes, and DEVICE_ID values exist in DEVICE labelled node

|IDENTITY_ID|DIVISION|
|---|---|
|22q|KHULNA|
|q22|DHAKA|
|ac23|RANGPUR|
|dfd|KHULNA|
|123g|MYMENSINGH|
|eo89|CHITTAGONG|
|ere3|SYLHET|
|lo1o3|DHAKA|
|90de|CHITTAGONG|
|er02|CHITTAGONG|
|22q|KHULNA|
|DEVICE_ID|DEVICE_NAME|
|---|---|
|1010k|SAMSUNG|
|1020k|APPLE|
|1030k|SONY|
|1050k|OPPO|
|1070k|ONEPLUS|
|1090k|SYMPHONY|
|1080k|NOKIA|
|1015k|ZTE|
|1025k|XIAOMI|
|1030k|XIAOMI pro|

so these are my values of customer.csv and device.csv

so the idea is I will create relationship between two nodes based on IDENTITY_ID and DEVICE_ID in relationship files.
I have tried several way to do this

since it is test case but this code will be applied in big size of data

LOAD CSV WITH HEADERS FROM "file:///relationship_device.csv" AS row
MATCH (f:CUSTOMER), (s:DEVICE)
WHERE f.IDENTITY_ID = row.IDENTITY_ID
AND s.DEVICE_ID = row.DEVICE_ID
CALL apoc.create.relationship(f, row.RELATIONSHIP,{}, s) YIELD rel
RETURN rel

using apoc library

CALL apoc.periodic.iterate('
load csv with headers from "file:///relationship_device.csv" AS row return row ','
MATCH (c:CUSTOMER {IDENTITY_ID: row.IDENTITY_ID})
MATCH (d:DEVICE{DEVICE_ID: row.DEVICE_ID})
MERGE(c)-[r:HAS_DEVICE]->(d)

                     ',{batchSize:1000, iterateList:true, parallel:true})
LOAD CSV WITH HEADERS FROM "file:///relationship_device.csv" AS line
MATCH (customer:CUSTOMER {IDENTITY_ID:line.IDENTITY_ID}),(device:DEVICE {DEVICE_ID:line.DEVICE_ID})
CREATE (customer)-[ :HAS_DEVICE{RELATIONSHIP: line.RELATIONSHIP}]->(device)

so far no relationship is created by this two nodes.What thing I am missing here.Using apoc solution will be good for future development. Any help will be highly appreciated

Hello @kalyan.b.aninda :slight_smile:

Can you try this?

LOAD CSV WITH HEADERS FROM "file:///relationship_device.csv" AS row
MATCH (f:CUSTOMER{IDENTITY_ID:row.IDENTITY_ID})
MATCH (s:DEVICE{DEVICE_ID:row.DEVICE_ID})
CALL apoc.create.relationship(f, row.RELATIONSHIP, {}, s) YIELD rel
RETURN rel

Can you show us how are the nodes in your database? (which properties and labels) :slight_smile:

Regards,
Cobra

Thanks you for your reply .it is showing after query
(no changes, no records)

MATCH (n:CUSTOMER) RETURN n [Table view]
{
"IDENTITY_ID": "22q",
"DIVISION": "KHULNA"

}

{
"IDENTITY_ID": "q22",
"DIVISION": "DHAKA"
}

MATCH (n:DEVICE) RETURN n [Table view]

{
"DEVICE_ID": "1010k",
"DEVICE_NAME": "SAMSUNG",
}

and so. on ....

How did you create your nodes?
Which version of Neo4j are you using?
How many rows do there is in the file?

Try with MERGE instead of MATCH:

LOAD CSV WITH HEADERS FROM "file:///relationship_device.csv" AS row
MERGE (f:CUSTOMER{IDENTITY_ID:row.IDENTITY_ID})
MERGE (s:DEVICE{DEVICE_ID:row.DEVICE_ID})
CALL apoc.create.relationship(f, row.RELATIONSHIP, {}, s) YIELD rel
RETURN rel

current version is 3.5.8 in my pc. didnt update it yet. I have posted whole rows in my first post. but it will increase later.just doing a POC with these dummy rows.

CALL apoc.periodic.iterate('CALL apoc.load.csv("file:///customer_account.csv") yield map as row return row','CREATE (p:CUSTOMER) SET p = row', {batchSize:1000000, iterateList:true, parallel:true})

This is my actually node creation query

and I tried your recent given query here.
it is showing

Neo.ClientError.Statement.SyntaxError: WITH is required between MERGE and CALL (line 4, column 1 (offset: 158))
"CALL apoc.create.relationship(f, row.RELATIONSHIP, {}, s) YIELD rel"

You should update to last version of Neo4j :slight_smile:

To create your nodes, use:

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///customer_account.csv" AS row
MERGE (p:CUSTOMER{IDENTITY_ID:row.IDENTITY_ID})
SET p += row

To create relationships:

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///relationship_device.csv" AS row
MATCH (f:CUSTOMER{IDENTITY_ID:row.IDENTITY_ID})
MATCH (s:DEVICE{DEVICE_ID:row.DEVICE_ID})
CALL apoc.create.relationship(f, row.RELATIONSHIP, {}, s) YIELD rel
RETURN rel

Did you put UNIQUE CONSTRAINTS on IDENTITY_ID and DEVICE_ID?

I also encourage you to follow the naming rules :slight_smile:

Thanks it worked. Yeh constraint will be needed for performance optimisation. I had bad experience before without giving constraint .It took too much time to create nodes and relationship for big volumes of data. then apoc module and setting constraint on id gave me relief.

A question to ask for knowledge gaining. apoc has parallel feature. so it runs parallelism for optimising query performance. Does this Using Periodic commit works for big volume data processing. I am not good at neo4j yet but learning :)

and question number 2 can you give a short explanation for this Node creation and relationship. like my previous code worked before apart from this relationship code

1 Like

Yeah Periodic commit works for big volume data processing :slight_smile:, it's done for that.

To be honest, I don't really know where was the bug since your code looked good, that's why I asked you to do a fresh start with a new database and a clean query :slight_smile: