Pavlos Baritakis
Pavlos Baritakis

Reputation: 31

Labels on Nodes and Relationships from a CSV file

I have problem when i want to add a label on a Node or to a Relatioship.

I do this in Neo4j with Cypher:

LOAD CSV WITH HEADERS FROM "file:c:/Users/Test/test.csv" AS line
CREATE (n:line.FROM)

and i get this error:

    Invalid input '.': expected an identifier character, whitespace, NodeLabel, a property map, ')' or a relationship pattern (line 2, column 15 (offset: 99))
"CREATE (n:line.FROM)"

If there is not a possible way of doing this with the Cypher Language, can you recommend me an other way to do my job? It is very important to find a solution on this problem even with a Cypher solution or any Java thing to do this job...

Upvotes: 1

Views: 1939

Answers (3)

Mohammad Ibrahim
Mohammad Ibrahim

Reputation: 31

enter image description hereenter image description hereBelow is the way for two csv files MIP_nodes.csv and MIP_edges.csv:

//Load csv data into the database - with dynamic label(s)
WITH "file:///MIP_nodes.csv" AS uri
LOAD CSV WITH HEADERS FROM uri AS row
WITH * WHERE row.label <> ""
call apoc.merge.node ([row.label],{nodeId:row.nodeId, name: row.name, type: row.type, created: row.created, property1: row.property1, property2: row.property2})
YIELD node as n1
//RETURN n1
WITH * WHERE row.label = ""
call apoc.merge.node (['DefaultNode'],{nodeId:row.nodeId, name: row.name, type: row.type, created: row.created, property1: row.property1, property2: row.property2})
YIELD node as n2
RETURN n1, n2

//Load csv data into the database - with dynamic relationship(s)
//:auto USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM 'file:///MIP_edges.csv' AS row
MATCH (s)  
WHERE s.nodeId = row.sourceId
//RETURN s
MATCH (d)
WHERE d.nodeId = row.destinationId
//RETURN d
CALL apoc.merge.relationship(s, row.label,{type:row.type, created: row.created, property1: row.property1, property2: row.property2},{}, d,{}) 
YIELD rel
//REMOVE rel.noOp;
RETURN rel;

Upvotes: 2

Pavlos Baritakis
Pavlos Baritakis

Reputation: 31

Thank you all for your answers but none of them helped me to solve my problem.

I found a solution to do exactly what i wanted. The solution was the Neo4jImporter tool (Link from official manual: Neo4jImporter tool Manual ) and not Cypher language nor Java.

So here is an example of what i have done and worked for me

A test.csv file contains the "PropertyTest" and ":LABEL". Firstly it creates one node with the label "TEST" and after the creation it adds the "proptest" property on the "TEST" node. So to add a Label on your node you use :LABEL and to add a Property on the same node you add any name you want as a header in .csv file.

Example of test.csv file:

PropertyTest,:LABEL
proptest,TEST

For windows i've done the Neo4jImport.bat command as it is described in the manual page of Neo4j.You can found the Neo4jImport.bat in Windows at "C:\Program Files\Neo4j Community\bin" and you run it from command line (cmd).

In details i opened the cmd, i followed the path to Neo4jImport.bat and finaly i wrote:

Neo4jImport.bat --into path-to-save-your-neo4j-database --nodes path-to-your-csv\test.csv 
--delimiter ","

The default delimiter of Neo4jImporter is the "," but you can change it. For example if your information in .csv file is seperated with tab you can do the following:

Neo4jImport.bat --into path-to-save-your-neo4j-database --nodes path-to-your-csv\test.csv 
--delimiter "TAB"

That was the way that i loaded dynamically a whole model of almost 2.000 nodes with different Labels and Properties.

Keep in mind from the manual that you can add as many labels and as many properties you want on a node by adding to your csv more headers

Example of two Labels in a node:

PropertyTest,:LABEL,:LABEL
proptest,TEST,SECOND_LABEL

Example of Neo4jImport.bat for two Labels and comma seperated CSV file:

Neo4jImport.bat --into path-to-save-your-neo4j-database --nodes path-to-your-csv\test.csv 
--delimiter ","

I hope that you will find it useful to this certain problem of Labels from .csv files and please read the official manual, it helped me a lot to find a solution for my problem.

Upvotes: 2

Michael Hunger
Michael Hunger

Reputation: 41706

Depends on how dynamic you need it to be, for small variability:

LOAD CSV WITH HEADERS FROM "file:c:/Users/Test/test.csv" AS line
WHERE line.FROM = "Foo"
CREATE (n:Foo)

From Java you can use node.addLabel(DynamicLabel.label(line.from))

Otherwise you can look into my neo4j-shell-tools, which allow dynamic labels and rel-types: with #{FROM}.

see: https://github.com/jexp/neo4j-shell-tools#cypher-import

Upvotes: 2

Related Questions