cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Adding properties from one dataset to a previously created node from another dataset in Neo4j

James8
Node Link

I also posted this at SO (which has pictures that couldn't be uploaded on this site): https://stackoverflow.com/questions/74736940/adding-properties-from-one-dataset-to-a-previously-crea...

I've created a dataset of nodes from census tract polygon areas. I want to attach census tract data (from another dataset) either as properties of each tract's corresponding node, or as relationship (tract_IDs) - [:Belongs_to] -> tract_IDs holding geometries. In this case, I think the properties makes more sense, but I'd also like to know how to do the latter. Because neo4j tutorials deal so much with creating a single node of data and not really with imported data, I can't figure out much on this. This will be attaching roughly 300 rows of tract data with IDs to the nodes containing spatial point geometry data.

This is the tract data being imported:

LOAD CSV WITH HEADERS
FROM "file:///Data-Census-clean_cen.csv" AS row
MERGE (geo_id: GEOID {GEOID: row.GEOID})
    ON CREATE SET  geo_id.Total_population = row.Total_population, geo_id.housing_units = row.Housing_unit, geo_id.number_families = row.Number_Families, geo_id.household_income = row.Household_Income, geo_id.per_black = row.Per_Black, geo_id.per_asian = row.Per_Asian, geo_id.per_white = row.Per_White;

 

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

Something like this, where 'CensusTrack' is the label representing the existing census track node. 

LOAD CSV WITH HEADERS
FROM "file:///Data-Census-clean_cen.csv" AS row
MATCH (n:CensusTrack {GEOID: row.GEOID})
MERGE (geo_id: GEOID {GEOID: row.GEOID})
    ON CREATE SET  geo_id.Total_population = row.Total_population, geo_id.housing_units = row.Housing_unit, geo_id.number_families = row.Number_Families, geo_id.household_income = row.Household_Income, geo_id.per_black = row.Per_Black, geo_id.per_asian = row.Per_Asian, geo_id.per_white = row.Per_White
MERGE (geo_id)-[:Belongs_to]->(n)

 

View solution in original post

7 REPLIES 7

ameyasoft
Graph Maven

MATCH (geo_id: GEOID {GEOID: row.GEOID})
where geo_id.GEOID = "06073008331"

//Assuming you are adding new properties to the existing node.....
SET geo_id.AWATER = row.AWATER, geo_id.STATEFP = row.STATEFP.......geo_id.TRACTCE = row.TRACTCE

Maybe I'm misunderstanding your solution, but in using your "where" statement, aren't you just limiting this to joining one tract node? Given that there ~300 tract_ids, this would take forever, no?

This seems to do the trick:

LOAD CSV WITH HEADERS
FROM "file:///Data-Census-clean_cen.csv" AS row
MATCH (geo_id: GEOID {GEOID: row.GEOID})
SET geo_id.Total_population = row.Total_population, geo_id.housing_units = row.Housing_unit,geo_id.number_families = row.Number_Families, geo_id.household_income = row.Household_Income

glilienfield
Ninja
Ninja

Something like this, where 'CensusTrack' is the label representing the existing census track node. 

LOAD CSV WITH HEADERS
FROM "file:///Data-Census-clean_cen.csv" AS row
MATCH (n:CensusTrack {GEOID: row.GEOID})
MERGE (geo_id: GEOID {GEOID: row.GEOID})
    ON CREATE SET  geo_id.Total_population = row.Total_population, geo_id.housing_units = row.Housing_unit, geo_id.number_families = row.Number_Families, geo_id.household_income = row.Household_Income, geo_id.per_black = row.Per_Black, geo_id.per_asian = row.Per_Asian, geo_id.per_white = row.Per_White
MERGE (geo_id)-[:Belongs_to]->(n)

 

First, thanks so much for this...it does exactly what I want.

Still, in trying to gain a better understanding of what I'm missing, I have a follow-up question. In reading through another post: https://community.neo4j.com/t5/neo4j-graph-platform/how-to-avoid-multiples-nodes-for-same-value/td-p... it seems like this should've solved my issue, but it didn't, and it also seemed like it was necessary to have a third dataset that contains the relationship information between GEOID and CensusTract (as you give them in your example), but it wasn't (given that your solution works). So what am I missing here?

The error in the solution posted in the link is that the merge contained more key/values in the match other than the unique identifier. What happens with a merge is that all the key/values pairs have to match, otherwise a new node will be created. As such, their solution should have merged just on the 'code', and then set the other properties in the 'set' clause. 

Got it. Thanks!