0
votes

I'm very new to Neo4j, been playing around with it for a couple of days now.

I'm trying to use Neo4j to map our company's database by showing how one table is related to another (data is pulled to or pushed from one table to another) and what scripts are used to do this pulling and pushing. To do this, I'm using three different properties: TableName, ScriptName, and TableTouch.

  • TableName: Table node which corresponds to the name of a table
  • ScriptName: Script Node which corresponds to the script which updatesa table
  • TableTouch: Used to show which table affects another table

Here is an example of the .CSV I'm importing:

TableName    ScriptName    TableTouch
Source       ScriptA       Water/Oil
Water        ScriptB       Source
Oil          ScriptC       Source

Here is the code I have thus far:

CREATE CONSTRAINT ON (c:Table) ASSERT c.TableName IS UNIQUE;
CREATE CONSTRAINT ON (c:Scripts) ASSERT c.ScriptName IS UNIQUE;

LOAD CSV WITH HEADERS FROM 
"file:///C:\\NeoTest.CSV" AS line

MERGE (table:Table {TableName: UPPER(line.TableName)})
SET table.TableTouch = UPPER(line.TableTouch)
MERGE (script:Scripts {ScriptName: UPPER(line.ScriptName)})

MERGE (table) - [:UPDATED_BY] -> (script)

This will relate scripts to their appropriate tables and load in all the table and script nodes.

Now an example of what I need is for Node "Source" to connect to Node "Water" because "Source" = Water.TableTouch and "Water" = Source.TableTouch.

Assume any given table could have multiple tables listed in the TableTouch property.

I want the TableName nodes to connect to other TableName nodes where the TableName of one node is found in the TableName.TableTouch of another node. How would I go about doing this? Do I need to have my .CSV formatted differently for this?

Thanks, -Andrew

Edit: This may make things more clear

What I have:

enter image description here What I'd like to have (red arrows):enter image description here

1

1 Answers

1
votes

[UPDATED]

If I understand your scenario, you want to represent the Script that is used to generate each Table, and what other table was used by that Script.

And, if I understand the meaning of your CSV file and your pictures, it looks like the Source table is generated by ScriptA without using data from any other tables. If so, you can create your CSV file to look something like this (where the Source table row's TableTouch column has the special value NOTHING -- you can use some other name -- to indicate that column actually has no value):

TableName,ScriptName,TableTouch
Source,ScriptA,NOTHING
Water,ScriptB,Source
Oil,ScriptC,Source

Data model:

(src:Table {name: 'Source'})<-[:USES]-(s:Script {name: 'ScriptC'})-[:MAKES]->(dest:Table {name: 'Oil'})

Note: This data model allows a single Script to "use" any number of source Tables and "make" any number of destination Tables.

Create Constraints

CREATE CONSTRAINT ON (t:Table) ASSERT t.name IS UNIQUE;
CREATE CONSTRAINT ON (s:Script) ASSERT s.name IS UNIQUE;

Import data

LOAD CSV WITH HEADERS FROM "file:///C:\\NeoTest.CSV" AS line
MERGE (src:Table     {name: line.TableTouch})
MERGE (dest:Table    {name: line.TableName})
MERGE (script:Script {name: line.ScriptName})
MERGE (script)-[:USES]->(src)
MERGE (script)-[:MAKES]->(dest)

Note: To keep the query simple, we just go ahead and create (at most) one NOTHING node to represent the absence of a source Table.

Results

enter image description here