cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Create individual node

How to create individual node for each record of columnand as well as create relationship b/w the one among other fields in the same row .

For example.
A csv file with contains 20 columns like below,
Inc_number,description,priority,type,support_name and so on with many records.
How should we create each record/field as a individual node with relationship like below,
INC3506875->HARDWARE->Low->Justin

13 REPLIES 13

ameyasoft
Graph Maven

Please post couple of records with column headers for me to offer any solutions.
Thanks.

Hi ,
Please find below records

Incident_Number Summary Service Type Priority Status Submit Date Last Resolved Date Status Start Date Status End Date Group Assign Date Group Re-Assign Date Reopen Pending Pending Time TTR with Pending TTR excluding Pending Support Organization Assigned Group VP (L3) Manager
INC000006475856 PHARMACY HARDWARE RELOCATION User Service Request Low Assigned 1/11/2019 13:14 3/2/2020 11:45 1/11/2019 13:14 2/17/2020 10:25 1/11/2019 13:14 2/21/2020 9:35 0 0 0 9982.5067 9982.5067 Application BOS Technology Worksite NULL Peter
INC000006311774 PHARMACY HARDWARE RELOCATION/REMOVAL REQUEST - WORKSITE PHARMACY User Service Request Low In Progress 1/11/2019 13:14 3/2/2020 11:45 2/17/2020 10:25 2/21/2020 9:35 1/11/2019 13:14 2/21/2020 9:35 0 0 0 9982.5067 9982.5067 Application BOS Technology Worksite NULL Coyle

Try this:
LOAD CSV with headers from 'file:///test.csv' as row FIELDTERMINATOR '\t'
MERGE (i:Incident {number: row.Incident_Number})
CREATE (p:Priority {priority: row.Priority})
MERGE (m:AssinedTo {assignedTo: row.Manager})

CREATE (i)-[:HARDWARE]->(p)
CREATE (p)-[:ASSIGNED_TO]->(m)

Result:

Your data has lot more information and you should think of a better data model.

Thank you so much. Am getting the expected result using this query.Yes,I've to find out is their any options to iterate the whole dataset and define the model in a single attempt.

Thank you for your reply.
Yes We can load csv with referenced link. My question is how to create individual node of each record of column in same csv file.
Below cypher query will create only Node from node.Name column and then remaining of NoteType,modelLevel and so on are all propertyType of Entity . But am expecting all the fields of row value should be an individual node.
LOAD CSV WITH HEADERS FROM 'file:///EntityNodes.csv' AS node
CREATE (:Entity {name: node.Name, nodeType : node.NodeType, modelLevel : node.ModelLevel, modelRegion : node.ModelRegion, creator : node.Creator, dateTime : node.DateTime, isRole : node.isRole})

Sorry, I'm a little bit tired and didn't correctly read your question.

I don't have my Neo4j desktop to test it, usually I test all my answer before bothering someone with a false answer. But can you test this:

LOAD CSV WITH HEADERS FROM 'file:///EntityNodes.csv' AS row
FOREACH (columnName IN keys(row)| CREATE (:columnName {value:row.columnName})

Wow, for the first time since I'm using Cypher, I found a limitation.
Sorry @mr.gsk0509, my query was wrong after testing it this after noon, it's not possible to use a variable as a label, and I guess, property name with the pure Cypher language.

I still need to confirm this in the manual, but still, this limitation is kind of sad.
For security purpose i guess.

though not directed specifically to the question posted initial on this thread but more so to your last post...

A variable can not be used as for a label not s much for security but rather because it would become impossible to determine a query plan.
For example if I had

match (n:$var1)-[:FOLLOWS]-(n2:Person) where n.age=20 return n;

then if $var1 was to represent :Person nodes then the planner could then check to see if :Person contained an index in property age but also could determine cardinality on n and n2 and determine what is the most efficient. But what if $var was not a static value for example

unwind ':Person, :Athlete, :Politician' as var
match (n:$var1)-[:FOLLOWS]-(n2:Person) where n.age=20 return n;

then here again planning would be a challenge.

However you could do this using apoc. For example if I had a .csv defined as

label,id,name
P1,1,Dana
P2,2,Tard

and then

load csv with headers from 'file:///dana.csv' as row call apoc.create.node([row.label],{name: row.name}) yield node return count(node);

would return

2

and

neo4j@dana> match (n:P1) return n;
+----------------------+
| n                    |
+----------------------+
| (:P1 {name: "Dana"}) |
+----------------------+

1 row available after 6 ms, consumed after another 2 ms
neo4j@dana> match (n:P2) return n;
+-----------------------+
| n                     |
+-----------------------+
| (:P2 {name: "Tard"}) |
+-----------------------+

I'm glad to know it, and as the planning is a huge part of Cypher there is no reason to expect that functionally soon or later.

Just to be sure, the planning is what turns the Cypher abstraction language to the graph database procedures.

Hi dana.canzano ,
Its working for single arugment like above sample which u provided. But am getting error if i added more than one argument itself. Pls see below error msg.

Procedure call provides too many arguments: got 3 expected no more than 2.

Procedure apoc.create.node has signature: apoc.create.node(label :: LIST? OF STRING?, props :: MAP?) :: node :: NODE?
meaning that it expects at least 2 arguments of types LIST? OF STRING?, MAP?
Description: apoc.create.node(['Label'], {key:value,...}) - create node with dynamic labels (line 1, column 55 (offset: 54))
"load csv with headers from 'file:///test1.csv' as row call apoc.create.node([row.label],{name: row.name},{project: row.project}) yield node return count(node);"

Is there any other option to add more number of individual field node items for each columns in csv spreadsheet.

apoc.create.node - APOC Documentation provides an example of the syntax to use when creating a node with multiple properties and in short detail effectively

:param labels =>  (["Human", "MovieStar"]);
:param properties => ({name: "Tom Cruise", placeOfBirth: "Syracuse, New York, United States"});
CALL apoc.create.node($labels, $properties);

admittedly that not with a load csv statement but it should be transferrable to LOAD CSV and thus

load csv with headers from 'file:///test1.csv' as row 
call apoc.create.node([row.label],{name: row.name,   project: row.project}) 
yield node return count(node);

clem
Graph Steward

It seems to me if you used a language like Python, you could construct the query with the value calculated so that you could construct the query string in Python before passing it onto Cypher.