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
541 views
in Technique[技术] by (71.8m points)

cypher - How to create nodes and relationships from csv in neo4j

I am loading a csv file with cypher query. The csv file is having 4 columns. 2 for columns and 2 for tables as shown below.

enter image description here

I want to create 2 types of nodes database and tables from those 4 columns. How can I create unique nodes for database and columns and to have relationships between them?

As per logisima's answer I added below query to create nodes for database and columns and added relationships. But there is some duplication in nodes.

        `LOAD CSV WITH HEADERS FROM 'file:///test1.csv' AS row
        MERGE (source:Database { source: row.Source_DB})
        MERGE (target:Database { target: row.Target_DB})
        MERGE (source_table:Table { source_table: row.Source_Table})
        MERGE (target_table:Table { source_table: row.Target_Table})
        MERGE (source)-[:LINKED_TO]-> (target)
        MERGE (source)-[:LINKED_TO]-> (source_table)
        MERGE (source)-[:LINKED_TO]-> (target_table)
        MERGE (target)-[:LINKED_TO]-> (target_table)`

enter image description here

Please bear with me I'm new to Neo4j.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Something like that ?

LOAD CSV WITH HEADERS FROM 'file:///MASTER.csv' AS row

MERGE (source:Database { db: row.Source_DB, table: row.Source_Table})
MERGE (target:Database { db: row.Target_DB, table: row.Target_Table})
MERGE (source)-[:LINKED_TO]->(target)

Update for the comment

CREATE CONSTRAINT ON (n:Database) ASSERT n.db IS UNIQUE;
CREATE CONSTRAINT ON (n:Table) ASSERT n.id IS UNIQUE;

LOAD CSV WITH HEADERS FROM 'file:///MASTER.csv' AS row

MERGE (sourceDb:Database { db: row.Source_DB})
MERGE (sourceTable:Table { id: row.Source_DB + "-" + row.Source_Table, table: row.Source_Table})
MERGE (sourceDb)-[:HAS_TABLE]->(sourceTable)

MERGE (targetDb:Database { db: row.Target_DB})
MERGE (targetTable:Table { id: row.Target_DB + "-" + row.Target_Table, table: row.Target_Table})
MERGE (targetDb)-[:HAS_TABLE]->(targetTable)

MERGE (sourceTable)-[:LINKED_TO]->(targetTable);

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

...