· neo4j

Neo4j vs Relational: Refactoring - Add a new field/property

This is what the two data models looked like after the initial data import and before any refactoring/migration had been done:

Relational

2016 05 22 09 49 23

Graph

2016 05 22 09 52 16

I wanted to add a 'nationality' property to the players table in the SQL schema and to the nodes with the 'Player' label in the graph.

This refactoring is quite easy in both models. In the relational database we first run a query to add the 'nationality' field to the table:

ALTER TABLE players
ADD COLUMN nationality varying(30);

And then we need to generate UPDATE statements from our data dump to update all the existing records:

UPDATE players
SET nationality = 'Brazil'
WHERE players.id = '/aldair/profil/spieler/4151';

...

In the graph we can do this in a single step by processing our data dump using the LOAD CSV command and then setting a property on each player:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///transfers.csv" AS row
MATCH (player:Player {id: row.playerUri})
SET player.nationality = row.playerNationality

If we wanted to make the nationality field non nullable we could go back and run the following queries:

ALTER TABLE players
ALTER COLUMN nationality SET NOT NULL
CREATE CONSTRAINT ON (player:Player)
ASSERT exists(player.nationality)

And we’re done!

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket