Neo4j Cypher - create nodes and set labels using LOAD CSV

I am trying to use LOAD CSV to create nodes with set value labels from CSV. Is it possible? Im trying something like:

LOAD CSV WITH HEADERS FROM 'file:///testfile.csv' AS line
CREATE (x:line.label)

... but I am getting an invalid syntax error. Is there any way to do this?

+6
source share
4 answers

bicpence,

Firstly, it’s pretty easy to do with a Java package import application, and it's easy to write. See this example of insertion input . You can use opencsv to read your CSV file.

Cypher, , - :

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM 'file:///testfile.csv' AS LINE
CREATE (n:load {lab:line.label, prop:line.prop});

CREATE INDEX ON :load(lab);

MATCH (n:load {lab:'label1'})
SET n:label1
REMOVE n:load
REMOVE n.lab;

MATCH (n:load {lab:'label2'})
SET n:label2
REMOVE n:load
REMOVE n.lab;

,

+6

, ,

+2

- , ,

LOAD CSV WITH HEADERS FROM 'file:///testfile.csv' AS line
CREATE (tmp:line[1])
WITH tmp
CREATE (x:Person {name: labels(tmp)[0]})
WITH tmp
REMOVE tmp

http://console.neo4j.org, :

LOAD CSV 
WITH HEADERS FROM "http://docs.neo4j.org/chunked/2.1.2/csv/import/persons.csv" AS csvLine
CREATE (p:tmp { id: toInt(csvLine.id), name: csvLine.name })
WITH p
CREATE (pp:Person { name: labels(p)[0]})
WITH p, pp
DELETE p
RETURN pp
+2

, , LOAD CSV, - CSV. Cypher, / Neo4j. , , .

I personally used Java, given that I am most comfortable with Java. I read each CSV line into a custom object that represents a line in my CSV file. Then I printed a line to the file that reflects the desired Cypher statement. And then all I had to do was cut and paste these commands into the command line of the Neo4j browser.

Thus, you can create your own teams the way you want, and you can completely avoid restrictions on LOAD CSV commands with Cypher.

0
source

All Articles