I have 3 json files in which there is information about set of nodes I wanted to know if I could dynamically create nodes and relationship dynamically by writing a query using loops
If possible can you please explain with an example "like consider there are 3 json files
file1 contain all information of an individual
file2 contain information about previous job experience
file2 contain information about his education
we have to create a graphdb where file 1 will have relationship with file 2 and file 3 based on similarity" Not an issue if you consider any other example
I have 3 JSON files "DataStore.json", "Network.json", "VMDetails.json"
I wanted to create nodes and relationships dynamically by importing data
from JSON using apoc.load.json function.
I used the below-mentioned queries
To Create DataStore nodes
CALL apoc.load.json("file:///DataStore.json") YIELD value AS data
MERGE (d:UserD{DataStore: data.DataStore})
SET d.Name = data.Name,
d.Type = data.Type,
d.Capacity = data.Capacity,
d.Free = data.Free
To create Network nodes
CALL apoc.load.json("file:///Network.json") YIELD value AS net
MERGE(n:UserN{Network:net.Network})
SET n.Name =net.Name
Everything works as expected till here
I wanted to create a relationship with DataStore node and VM node and
another relationship with Network node and VM node while creating VM node
I used the below-mentioned query
This query is creating new DataStore and Network nodes instead of creating
a relationship with the existing nodes. Can anyone tell me where I am going
wrong? It would be very helpful. Thanks in advance
as it's not really clear what's contained in the JSON files, it's hard to tell.
Odd that you use UserN etc. as labels instead of Network, DataStore,VM.
You don't need MERGE for the user and datastore as you created them already upfront.
Did you check that your row contains the correct data?
If not your lookup data is wrong and MERGE will create new data.
Sometimes there are spelling errors or surrounding spaces or casing.
i.e.
CALL apoc.load.json("file:///VmDetails.json") YIELD value AS vm
return vm
limit 1
Thank you very much for your reply
If I didn't MERGE the datastore and network node in while creating VM node some mal(new and unwanted)nodes get createdMy row contains correct data.
I have attached the JSON files in this mail.
please go through it and let me know what me the mistake is
Thank you
Try this:
Replace these two lines:
MERGE(n:UserN{Network:vm.Network})
MERGE(d:UserD{DataStore:vm.DataStore})
with:
MATCH(n:UserN)
WHERE n.Network in vm.Network
MATCH(d:UserD)
WHERE d.DataStore in vm.DataStore
Add WITH condition:
WITH is required between MATCH and MERGE.
MATCH(n:UserN)
WHERE n.Network in vm.Network
MATCH(d:UserD)
WHERE d.DataStore in vm.DataStore
WITH n, d
MERGE (n)-[:connected]->(v)
MERGE (v)-[:related]->(d)
Thank you for your time
But even this does not work
Hence I came up with this query
CALL apoc.load.json("file:///DataStore.json") YIELD value AS data
MERGE (d:DataStore{DataStore: data.DataStore})
SET d.Name = data.Name,
d.Type = data.Type,
d.Capacity = data.Capacity,
d.Free = data.Free
WITH data as data
CALL apoc.load.json("file:///Network.json")YIELD value AS net
MERGE(n:Network{Network:net.Network})
SET n.Name =net.Name
WITH net AS nw
CALL apoc.load.json("file:///VmDetails.json") YIELD value AS vm
MERGE(n:Network{Name:vm.Network})
MERGE(d:DataStore{Datastore:vm.DataStore})
MERGE(v:UserV{vmName:vm.vmName})
SET v.CPU= vm.CPU,
v.ConnectionState = vm.ConnectionState,
v.DataStore = vm.DataStore,
v.Hostname = vm.Hostname,
v.InstanceUuid = vm.InstanceUuid,
v.IpAddress = vm.IpAddress,
v.Network = vm.Network,
v.NumEthernetCards = vm.NumEthernetCards,
v.NumVirtualDisks = vm.NumVirtualDisks,
v.OverallStatus = vm.OverallStatus,
v.PowerState = vm.PowerState,
v.RAM = vm.RAM,
v.Uuid = vm.Uuid
MERGE (n)-[:connected]->(v)
MERGE (v)-[:related]->(d)
Here in this query, I am able to create a relationship with vmnode & network node correctly but in the case of datastore node it is creating a new node and creating a relationship with them instead of creating the relationship with the existing nodes
Check to see if data.DataStore = vm.DataStore. Looks like they are not matching. Also check to see the Datastore value of the blue nodes with 'related' connections.
Update: the property names are not matching.
MERGE (d:DataStore{DataStore: data.DataStore})
MERGE(d:DataStore{Datastore:vm.DataStore})
's' is lower case in vm and is upper case in data.
Correct this in your vm code and this should work fine!
This is the output I am getting if I used this query
CALL apoc.load.json("file:///DataStore.json") YIELD value AS data
MERGE (d:DataStore{DataStore: data.DataStore})
SET d.Name = data.Name,
d.Type = data.Type,
d.Capacity = data.Capacity,
d.Free = data.Free
WITH data as data
CALL apoc.load.json("file:///Network.json")YIELD value AS net
MERGE(n:Network{Network:net.Network})
SET n.Name =net.Name
WITH net AS nw
CALL apoc.load.json("file:///VmDetails.json") YIELD value AS vm
MERGE(n:Network{Name:vm.Network})
MERGE(d:DataStore{DataStore:vm.DataStore})
MERGE(v:UserV{vmName:vm.vmName})
SET v.CPU= vm.CPU,
v.ConnectionState = vm.ConnectionState,
v.DataStore = vm.DataStore,
v.Hostname = vm.Hostname,
v.InstanceUuid = vm.InstanceUuid,
v.IpAddress = vm.IpAddress,
v.Network = vm.Network,
v.NumEthernetCards = vm.NumEthernetCards,
v.NumVirtualDisks = vm.NumVirtualDisks,
v.OverallStatus = vm.OverallStatus,
v.PowerState = vm.PowerState,
v.RAM = vm.RAM,
v.Uuid = vm.Uuid
MERGE (n)-[:connected]->(v)
MERGE (v)-[:related]->(d)