Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

cypher - How to create unique nodes and relationships by csv file imported in neo4j?

Hello I was trying to import some data in csv file to neo4j in my ubuntu 12.04.

The csv file is a two column data file with no header its format is like:

12ffew3213,232rwe13
12ffew3213,5yur2ru2r
rwerwerw3,432rwe13
rwerwerw3,5yur2ru2r

the thing is the data in row 0 and row 1 is not unique, for example the data may be 3000 lines and only has 100 unique row0 value and 300 unique row1 value.

And I want to build a graph with unique 100 row0 nodes and 300 row1 nodes and 3000 relationships between those nodes(if 12ffew3213,232rwe13 appears twice there are 2 edges).

Since I am new to neo4j and Cypher. After I tried with CREATE and MERGE for a while I still cannot build UNIQUE nodes. I used something like

LOAD CSV FROM 'file:///home/nate/Downloads/file.csv' AS line
MERGE (:A { number: toString(line[0])})-[:LIKES]->(:B { ID: toString(line[1])})

Any ideas??Thanks ahead!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's what you do.

LOAD CSV FROM 'file:///home/nate/Downloads/file.csv' AS line
MERGE (n:A {number : line[0]})
WITH line, n
MERGE (m:B {ID : line[1]})
WITH m,n
MERGE (n)-[:LIKES]->(m);

You first create or match the :A node, then create or match the :B node, then create or match the relationship. The WITH clauses collect the results at each point in the sequence to use in the next. To find out more about WITH clauses, read Section 9.5 in the Neo4j Manual.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...