Pratyush Kulwal
Pratyush Kulwal

Reputation: 165

Cypher query - creation of graph from csv file

I have two csv files: (clean_data_2.csv : Sample Content as under)

enter image description here

(stationdata.csv : Sample Content as under)

enter image description here

From my cypher query, I want that each station is represented as node and relationship is represented as count.

I did something like this:

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM "file:///stationdata.csv" AS line
CREATE (s:station{id:line.station_id,station_name:line.name});

Loading all station data: it creates all the nodes - source and destination columns

LOAD CSV WITH HEADERS FROM "file:///clean_data_2.csv" AS line
MATCH (src:station),(dst:station)
CREATE (src)-[:TO{ count: [line.count]}]->(dst);

The above part runs, but does not give me count in the relationship between nodes.

I am new to Neo4j - graph databases, thanks!

Upvotes: 0

Views: 216

Answers (1)

cybersam
cybersam

Reputation: 66967

Your second query's MATCH clause does not specify the names of the station nodes for src and dst, so all possible pairs of station nodes would be matched. That would cause the creation of a lot of extra TO relationships with count properties.

Try using this instead of your second query:

LOAD CSV WITH HEADERS FROM "file:///clean_data_2.csv" AS line
MATCH (src:station {name: line.src}), (dst:station {name: line.dst})
CREATE (src)-[:TO {count: TOINTEGER(line.count)}]->(dst);

This query specifies the station names in the MATCH clause, which your query was not doing.

This query also converts the line.count value from a string (which all values produced by LOAD CSV are) into an integer, and assigns it as a scalar value to the count property, as there does not seem a need for it to be an array.

Upvotes: 2

Related Questions