1
votes

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.

2

2 Answers

3
votes

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);
1
votes

Your query needs to create Database nodes with a consistent property for the name of the DB. You are using 2 different property names, so you are creating 2 nodes sometimes for the same DB.

Instead of:

MERGE (source:Database { source: row.Source_DB})
MERGE (target:Database { target: row.Target_DB})

use something like:

MERGE (source:Database {name: row.Source_DB})
MERGE (target:Database {name: row.Target_DB})

You will also have to fix the rest of the query to use that property name.